Skip to content

Instantly share code, notes, and snippets.

@aozisik
Last active August 29, 2015 14:15
Show Gist options
  • Save aozisik/9718be69fcb3b05e2221 to your computer and use it in GitHub Desktop.
Save aozisik/9718be69fcb3b05e2221 to your computer and use it in GitHub Desktop.
Ideal Text Color
/*
* 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