Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
getBrightness color function
var getBrightness = function(hex) {
var rgb = 'rgb(' + (hex = hex.replace('#', '')).match(new RegExp('(.{' + hex.length/3 + '})', 'g')).map(function(l) { return parseInt(hex.length%2 ? l+l : l, 16); }).join(',') + ')';
// Get array of RGB values
rgb = rgb.replace(/[^\d,]/g, '').split(',');
var r = rgb[0], g = rgb[1], b = rgb[2];
var brightness = Math.floor((r * 299 + g * 587 + b * 114) / 1000);
return brightness;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.