Skip to content

Instantly share code, notes, and snippets.

@caryfuk
Created July 26, 2018 20:28
Show Gist options
  • Save caryfuk/4890508b1bce306bb7552048cac1f3e8 to your computer and use it in GitHub Desktop.
Save caryfuk/4890508b1bce306bb7552048cac1f3e8 to your computer and use it in GitHub Desktop.
Return either white or black to achieve greatest constrast.
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