Skip to content

Instantly share code, notes, and snippets.

@BigRaj
Created April 3, 2019 18:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigRaj/acdcc26f83869eb314c5e65099bd683e to your computer and use it in GitHub Desktop.
Save BigRaj/acdcc26f83869eb314c5e65099bd683e to your computer and use it in GitHub Desktop.
Flips text color based on background luminosity
HTMLElement.prototype.setBgColor = function(r,g,b){
var el = this;
var generateLuminance = function(r,g,b){
return (0.299*r)+(0.587*g)+(0.114*b);
}
el.style.backgroundColor = 'rgb('+r+','+g+','+b+')';
if(generateLuminance > 138){
el.style.color = 'rgb(0,0,0)';
}
else{
el.style.color = 'rgb(255,255,255)';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment