Skip to content

Instantly share code, notes, and snippets.

@bildepunkt
Last active January 21, 2016 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bildepunkt/0c311db23dcf947d1344 to your computer and use it in GitHub Desktop.
Save bildepunkt/0c311db23dcf947d1344 to your computer and use it in GitHub Desktop.
background color fishing
(function () {
'use strict';
function getRgbArray(rgb) {
return rgb.match(/[0-9]+/g);
}
function getDarkenedRgbArray(rgb) {
let rgbs = getRgbArray(rgb);
return [Math.round(rgbs[0] / 2), Math.round(rgbs[1] / 2), Math.round(rgbs[2] / 2)];
}
function getRgbTotal(rgb) {
let rgbs = getRgbArray(rgb);
return parseInt(rgbs[0], 10) +
parseInt(rgbs[1], 10) +
parseInt(rgbs[2], 10);
}
for (let el of [].slice.call(document.querySelectorAll('*'))) {
let bg = el.style.backgroundColor;
if (bg) {
if (getRgbTotal(bg) > 255 * 3 / 2) {
let rgbs = getDarkenedRgbArray(bg);
let newBg = `rgb(${rgbs[0]}, ${rgbs[1]}, ${rgbs[2]})`;
console.log(newBg);
el.style.backgroundColor = newBg;
}
} else {
el.style.backgroundColor = '#555';
}
}
}())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment