Skip to content

Instantly share code, notes, and snippets.

@arpruss
Last active June 6, 2021 14:57
Show Gist options
  • Save arpruss/9a50db9c4b431e4fda1b3510cfdff729 to your computer and use it in GitHub Desktop.
Save arpruss/9a50db9c4b431e4fda1b3510cfdff729 to your computer and use it in GitHub Desktop.
Reader
(function(){
function getColor(tag) {
m = tag.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
if ( m )
return [parseInt(m[1]),parseInt(m[2]),parseInt(m[3]),255];
m = tag.match(/^rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
return m ? [parseInt(m[1]),parseInt(m[2]),parseInt(m[3]),parseInt(m[4])] : null;
}
function realBackgroundColor(elem) {
if (!elem) return [0,0,0,0];
var style = getComputedStyle(elem);
if (style.backgroundImage !== 'none')
return undefined;
var color = getColor(style.backgroundColor);
if (!color)
return undefined;
if (color[3])
return color;
else
return realBackgroundColor(elem.parentElement);
}
var nodes=document.body.getElementsByTagName('*');
var pageBack=getColor(getComputedStyle(document.body).backgroundColor);
if (pageBack[3] < 255) {
for (var j=0;j<3;j++) {
pageBack[j] = Math.floor((pageBack[j] * pageBack[3] + 255 * (255-pageBack[3])) / 255);
pageBack[3] = 255;
}
}
if (pageBack[0]+pageBack[1]+pageBack[2] < 150) {
document.body.style.backgroundColor = "rgb(0,0,0)";
}
if (pageBack[0]+pageBack[1]+pageBack[2] > 255*3-150) {
document.body.style.backgroundColor = "rgb(255,255,255)";
}
for (var i=0; i<nodes.length; i++) {
if (nodes[i].style) {
var style = getComputedStyle(nodes[i]);
nodes[i].style.fontFamily="Georgia, Utopia, \"Palatino Linotype\", Palatino, serif";
if (style.textAlign == "left" || style.textAlign == "start") {
nodes[i].style.textAlign = "justify";
}
var fore = getColor(style.color);
var back = realBackgroundColor(nodes[i]);
if (back === undefined) {
back = [255,255,255,255];
}
if (! fore) {
fore = [0,0,0,255];
}
if (fore[0] + fore[1] + fore[2] <= back[0] + back[1] + back[2]) {
for (var j=0; j<3; j++) {
if (fore[j] < 200)
fore[j] = 0;
}
if(nodes[i].href)
nodes[i].style.color = "rgb(0,0,128)";
else
nodes[i].style.color = "rgb("+fore[0]+","+fore[1]+","+fore[2]+")";
}
else {
if(nodes[i].href)
nodes[i].style.color = "rgb(0,0,255)";
else
nodes[i].style.color = "rgb(255,255,255)";
}
}
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment