Skip to content

Instantly share code, notes, and snippets.

@kellishaver
Created December 3, 2009 16:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kellishaver/248307 to your computer and use it in GitHub Desktop.
Save kellishaver/248307 to your computer and use it in GitHub Desktop.
feed it a hex code and it returns the inverse
function invert_color(color) {
color = color.replace('#', '');
r = color.substr(0,2);
g = color.substr(2,2);
b = color.substr(4,2);
inverted_r = 255 - parseInt(r,16);
inverted_g = 255 - parseInt(g,16);
inverted_b = 255 - parseInt(b,16);
var hexbase = "0123456789ABCDEFabcdef";
inverted_r_hex = hexbase.charAt((inverted_r>> 4)& 0xf)+ hexbase.charAt(inverted_r& 0xf);
inverted_g_hex = hexbase.charAt((inverted_g>> 4)& 0xf)+ hexbase.charAt(inverted_g& 0xf);
inverted_b_hex = hexbase.charAt((inverted_b>> 4)& 0xf)+ hexbase.charAt(inverted_b& 0xf);
return '#' + inverted_r_hex + inverted_g_hex + inverted_b_hex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment