Skip to content

Instantly share code, notes, and snippets.

@azzlack
Last active August 29, 2015 14:14
Show Gist options
  • Save azzlack/0ff3af2973fbddeb3728 to your computer and use it in GitHub Desktop.
Save azzlack/0ff3af2973fbddeb3728 to your computer and use it in GitHub Desktop.
Gets the contrast color for a given color
var getContrastColor = function(hexcolor) {
var hex = hexcolor.replace(/[^0-9a-f]/gi, '');
var r = parseInt(hex.substr(0, 2), 16);
var g = parseInt(hex.substr(2, 2), 16);
var b = parseInt(hex.substr(4, 2), 16);
var yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000;
return (yiq >= 128) ? 'black' : 'white';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment