Created
July 7, 2014 12:26
-
-
Save Xordal/9bf24bc6cbc5a39f62cd to your computer and use it in GitHub Desktop.
Invert RGB color js
This file contains 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
function componentToHex(c) { | |
var hex = c.toString(16); | |
return hex.length == 1 ? "0" + hex : hex; | |
} | |
function rgbToHex(r, g, b) { | |
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b); | |
} | |
function invert(rgb) { | |
rgb = [].slice.call(arguments).join(",").replace(/rgb\(|\)|rgba\(|\)|\s/gi, '').split(','); | |
for (var i = 0; i < rgb.length; i++) rgb[i] = (i === 3 ? 1 : 255) - rgb[i]; | |
return rgbToHex(rgb[0], rgb[1], rgb[2]); | |
} | |
// invert elem background | |
var color = elem.css("background-color"); | |
elem.css('border-color', invert(color)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍