Skip to content

Instantly share code, notes, and snippets.

@jczaplew
Last active December 21, 2015 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jczaplew/9223f6e0fa9c22289723 to your computer and use it in GitHub Desktop.
Save jczaplew/9223f6e0fa9c22289723 to your computer and use it in GitHub Desktop.
Best color for text on a background of a given hex color
/*
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