Created
August 11, 2014 06:55
-
-
Save liquorice/052294c8237f0b47b48e to your computer and use it in GitHub Desktop.
Determine if a colour appears light or dark
This file contains hidden or 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 light_or_dark = function(hex) { | |
var rgb = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex), | |
luma = Math.sqrt( | |
0.299 * parseInt(rgb[1], 16) + | |
0.587 * parseInt(rgb[2], 16) + | |
0.144 * parseInt(rgb[3], 16) | |
); | |
return (luma > 13) ? "light" : "dark"; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful for picking between light/dark text to go over a block colour.