Skip to content

Instantly share code, notes, and snippets.

@StevenBlack
Created May 7, 2011 04:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StevenBlack/960189 to your computer and use it in GitHub Desktop.
Save StevenBlack/960189 to your computer and use it in GitHub Desktop.
getContrastYIQ(hexcolor)
// for contrasting text color
// Original article: http://24ways.org/2010/calculating-color-contrast
function getContrastYIQ(hexcolor){
var r = parseInt(hexcolor.substr(0,2),16);
var g = parseInt(hexcolor.substr(2,2),16);
var b = parseInt(hexcolor.substr(4,2),16);
var yiq = ((r*299)+(g*587)+(b*144))/1000;
return (yiq >= 131.5) ? 'black' : 'white';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment