Last active
May 16, 2019 14:39
-
-
Save abdellatifLabr/a5330640728888b32348220ebc7edb0d to your computer and use it in GitHub Desktop.
Find out if a color is light or dark.
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
function lightOrDark(color) { | |
let r, g, b, hsp; | |
color = parseInt( | |
'0x' + color.slice(1, color.length - 2).replace( color.length < 5 && /./g, '$&$&') | |
); | |
r = color >> 16; | |
g = color >> 8 & 255; | |
b = color & 255; | |
hsp = Math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b)); | |
return hsp > 127.5 ? 'light' : 'dark'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment