-
-
Save zeke/a8b81c70e606b0b683e5 to your computer and use it in GitHub Desktop.
Best color for text on a background of a given hex color
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
/* | |
Variation of http://stackoverflow.com/a/11508164/1956065 | |
Follow up to https://twitter.com/johnjcz/status/670365691329970176 | |
Inspired by UI of http://www.colorhexa.com/ | |
*/ | |
function bestTextColor(hex) { | |
hex = hex.replace('#', ''); | |
var bigint = parseInt(hex, 16); | |
var g = (bigint >> 8) & 255; | |
return (g > 50) ? '#333333' : '#FFFFFF'; | |
} | |
// Or if you're really good at reading one liners | |
function bestTextColor(hex) { | |
return (((parseInt(hex.replace('#', ''), 16) >> 8) & 255) > 50) ? '#333333' : '#FFFFFF'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment