Skip to content

Instantly share code, notes, and snippets.

@amitpatelx
Last active September 15, 2022 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitpatelx/5e3b3921ad2029a013ac325e2092cee0 to your computer and use it in GitHub Desktop.
Save amitpatelx/5e3b3921ad2029a013ac325e2092cee0 to your computer and use it in GitHub Desktop.
// https://stackoverflow.com/questions/12043187/how-to-check-if-hex-color-is-too-black
function luma(color){
var c = color.substring(1); // strip #
var rgb = parseInt(c, 16); // convert rrggbb to decimal
var r = (rgb >> 16) & 0xff; // extract red
var g = (rgb >> 8) & 0xff; // extract green
var b = (rgb >> 0) & 0xff; // extract blue
return 0.2126 * r + 0.7152 * g + 0.0722 * b; // per ITU-R BT.709
}
// > 240 close to white
// < 40 close to black
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment