Skip to content

Instantly share code, notes, and snippets.

@abdellatifLabr
Last active May 16, 2019 14:39
Show Gist options
  • Save abdellatifLabr/a5330640728888b32348220ebc7edb0d to your computer and use it in GitHub Desktop.
Save abdellatifLabr/a5330640728888b32348220ebc7edb0d to your computer and use it in GitHub Desktop.
Find out if a color is light or dark.
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