Skip to content

Instantly share code, notes, and snippets.

@sanusart
Last active October 8, 2019 19:58
Show Gist options
  • Save sanusart/78937a0eb8bce2b21a64acd921d2a55d to your computer and use it in GitHub Desktop.
Save sanusart/78937a0eb8bce2b21a64acd921d2a55d to your computer and use it in GitHub Desktop.
Light or dark hex color #color #hex
export const lightOrDark = (color, ratio = 155) => {
const hex = color.replace('#', '');
const red = parseInt(hex.substr(0, 2), 16);
const green = parseInt(hex.substr(2, 2), 16);
const blue = parseInt(hex.substr(4, 2), 16);
const brightness = (red * 299 + green * 587 + blue * 114) / 1000;
return brightness > ratio ? 'light' : 'dark';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment