Last active
January 21, 2016 00:22
-
-
Save bildepunkt/0c311db23dcf947d1344 to your computer and use it in GitHub Desktop.
background color fishing
This file contains hidden or 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 () { | |
'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