Last active
August 29, 2015 14:08
-
-
Save ilguzin/602d8c527c92d92c83fc to your computer and use it in GitHub Desktop.
Derive text color depending on background brightness
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
#Color text change | |
rgb = [ | |
"255" | |
"0" | |
"0" | |
] | |
setInterval (-> | |
c = "rgb(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")" | |
#http://www.w3.org/TR/AERT#color-contrast | |
o = Math.round(((parseInt(rgb[0]) * 299) + (parseInt(rgb[1]) * 587) + (parseInt(rgb[2]) * 114)) / 1000) | |
#console.log(o); | |
if o > 125 | |
$("#bg").css "color", "black" | |
else | |
$("#bg").css "color", "white" | |
$("#bg").css "background-color", c | |
r = Math.round(Math.random() * 255) | |
g = Math.round(Math.random() * 255) | |
b = Math.round(Math.random() * 255) | |
rgb[0] = r | |
rgb[1] = g | |
rgb[2] = b | |
return | |
), 500 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment