function brightness(color) {
var rgba, y;
rgba = hexToRgb(color, 1).match(/^rgb(?:a)?\(([0-9]{1,3}),\s([0-9]{1,3}),\s([0-9]{1,3})(?:,\s)?([0-9]{1,3})?\)$/);
if (rgba != null) {
y = (299 * rgba[1] + 587 * rgba[2] + 114 * rgba[3]) / 1000;
return y;
}
};
function hexToRgb(hex, alpha) {
hex = hex.replace('#', '');
var r = parseInt(hex.length == 3 ? hex.slice(0, 1).repeat(2) : hex.slice(0, 2), 16);
var g = parseInt(hex.length == 3 ? hex.slice(1, 2).repeat(2) : hex.slice(2, 4), 16);
var b = parseInt(hex.length == 3 ? hex.slice(2, 3).repeat(2) : hex.slice(4, 6), 16);
if (alpha) {
return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + alpha + ')';
} else {
return 'rgb(' + r + ', ' + g + ', ' + b + ')';
}
}