Last active
August 29, 2015 14:15
-
-
Save aozisik/9718be69fcb3b05e2221 to your computer and use it in GitHub Desktop.
Ideal Text Color
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
/* | |
* Finds the ideal text color for legibility on the background color provided | |
* Example i/o: idealTextColor('#000000') -> #ffffff | |
*/ | |
function idealTextColor(background) { | |
var r = background.substring(1, 3); | |
var g = background.substring(3, 5); | |
var b = background.substring(5, 7); | |
var components = {R: parseInt(r, 16),G: parseInt(g, 16),B: parseInt(b, 16)}; | |
var nThreshold = 105; | |
var bgDelta = (components.R * 0.299) + (components.G * 0.587) + (components.B * 0.114); | |
return ((255 - bgDelta) < nThreshold) ? "#000000" : "#ffffff"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment