Skip to content

Instantly share code, notes, and snippets.

@eeslom
Forked from ramziddin/index.js
Created January 12, 2025 19:17
Show Gist options
  • Save eeslom/2a7ba4b16cec0acf939c7f5ba394d0f2 to your computer and use it in GitHub Desktop.
Save eeslom/2a7ba4b16cec0acf939c7f5ba394d0f2 to your computer and use it in GitHub Desktop.
Check if hex color dark or light
const isColorDark = color => {
const c = color.substring(1); // strip #
const rgb = parseInt(c, 16); // convert rrggbb to decimal
const r = (rgb >> 16) & 0xff; // extract red
const g = (rgb >> 8) & 0xff; // extract green
const b = (rgb >> 0) & 0xff; // extract blue
const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b; // per ITU-R BT.709
const isDark = luma < 40;
return isDark;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment