Skip to content

Instantly share code, notes, and snippets.

@majman
Created July 2, 2012 18:59
Show Gist options
  • Select an option

  • Save majman/3034946 to your computer and use it in GitHub Desktop.

Select an option

Save majman/3034946 to your computer and use it in GitHub Desktop.
Reverse Page Font & Background Colors
function invertColor(color){
var r,g,b;
if(color.indexOf('rgb') >= 0){
if(color == 'rgba(0, 0, 0, 0)'){
color = 'rgb(255,255,255)';
}
color = color.substring(color.indexOf('(')+1, color.indexOf(')')).split(',');
r = color[0];
g = color[1];
b = color[2];
}else{
if(color.length != 6) {
r = color.charAt(0);
r = r+r;
g = color.charAt(1);
g = g+g;
b = color.charAt(2);
b = b+b;
}else{
r = color.substring(0,2);
g = color.substring(2,4);
b = color.substring(4,6);
}
r = parseInt(r,16);
g = parseInt(g,16);
b = parseInt(b,16);
}
r2 = 255 - r;
g2 = 255 - g;
b2 = 255 - b;
return 'rgb('+r2+','+g2+','+b2+')';
}
function getStyle(oElm, prop){
var strValue = "";
if(document.defaultView && document.defaultView.getComputedStyle){
strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(prop);
}
else if(oElm.currentStyle){
prop = prop.replace(/\-(\w)/g, function (strMatch, p1){
return p1.toUpperCase();
});
strValue = oElm.currentStyle[prop];
}
return strValue;
}
function invertElementColors(el){
var fontColor = getStyle(el, 'color');
var bgColor = getStyle(el, 'background-color');
newFontColor = invertColor(fontColor);
newBgColor = invertColor(bgColor);
el.style.color = newFontColor;
el.style['background-color'] = newBgColor;
}
function traverseReverse(el) {
if(el.nodeType == 1){
for(var child = el.lastChild; child; child = child.previousSibling) {
traverseReverse(child);
}
invertElementColors(el);
}
}
traverseReverse(document.getElementsByTagName("body")[0]);
@majman

majman commented Jul 2, 2012

Copy link
Copy Markdown
Author

gist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment