Created
July 26, 2018 20:28
-
-
Save caryfuk/4890508b1bce306bb7552048cac1f3e8 to your computer and use it in GitHub Desktop.
Return either white or black to achieve greatest constrast.
This file contains 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
function whiteOrBlack(color) { | |
const colorArr = color.length === 4 ? color.match(/[\da-fA-F]{1}/g) : color.match(/[\da-fA-F]{2}/g); | |
return colorArr | |
.map(s => s.length > 1 ? parseInt(s, 16) : parseInt(s, 16) * 16) | |
.reduce((a, c) => a + c) > 381 ? '#000' : '#FFF'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment