Skip to content

Instantly share code, notes, and snippets.

@rodmcnew
Last active November 23, 2021 13:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodmcnew/df8c753020ad0f33bca5baa034fbfd63 to your computer and use it in GitHub Desktop.
Save rodmcnew/df8c753020ad0f33bca5baa034fbfd63 to your computer and use it in GitHub Desktop.
// Returns black or white foreground color depending on the background
var textColor = function (backgroundColor) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(backgroundColor);
if (!result) {
return '#000000';//Happens when not given hex
}
var shade = (parseInt(result[1], 16) + parseInt(result[2], 16) + parseInt(result[3], 16)) / 3;
return shade > 128 ? '#000000' : '#FFFFFF';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment