Last active
October 8, 2019 19:58
-
-
Save sanusart/78937a0eb8bce2b21a64acd921d2a55d to your computer and use it in GitHub Desktop.
Light or dark hex color #color #hex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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