Created
April 3, 2019 18:20
-
-
Save BigRaj/acdcc26f83869eb314c5e65099bd683e to your computer and use it in GitHub Desktop.
Flips text color based on background luminosity
This file contains 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
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