Skip to content

Instantly share code, notes, and snippets.

@Heydon
Last active June 21, 2018 10:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Heydon/05487a78928ced20338a5640bcca1870 to your computer and use it in GitHub Desktop.
Save Heydon/05487a78928ced20338a5640bcca1870 to your computer and use it in GitHub Desktop.
Function to determine if a hex color is light (crude)
function isLight(hex) {
let vals = hex.substring(1).split('').map((h, i) => {
if (!/^\d+$/.test(h)) {
h = parseInt(h, 16)
}
return parseInt(i % 2 < 1 ? h * 16 : h)
})
return vals.reduce((n, x) => n + x) > (765 / 2)
}
/* Usage
const isLight = isLight('#fffccc');
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment