Created
April 13, 2019 20:12
-
-
Save BenOsodrac/e0ceac5d3303144cebca7c1137480e57 to your computer and use it in GitHub Desktop.
getBrightness color function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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