Skip to content

Instantly share code, notes, and snippets.

@ilguzin
Last active August 29, 2015 14:08
Show Gist options
  • Save ilguzin/602d8c527c92d92c83fc to your computer and use it in GitHub Desktop.
Save ilguzin/602d8c527c92d92c83fc to your computer and use it in GitHub Desktop.
Derive text color depending on background brightness
#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