Skip to content

Instantly share code, notes, and snippets.

@friedemannsommer
Last active December 5, 2016 11:41
Show Gist options
  • Save friedemannsommer/e2923a6e0c5a865e5ae410d427b61624 to your computer and use it in GitHub Desktop.
Save friedemannsommer/e2923a6e0c5a865e5ae410d427b61624 to your computer and use it in GitHub Desktop.
/**
* @url http://stackoverflow.com/questions/1855884/determine-font-color-based-on-background-color
* @url http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color
**/
function perceptiveLuminance(color) {
return 1 - (0.299 * color.r + 0.587 * color.g + 0.114 * color.b) / 255
}
function getFontColor(backgroundColor) {
if (perceptiveLuminance(backgroundColor) < 0.5) {
return {
r: 0,
g: 0,
b: 0
}
} else {
return {
r: 255,
g: 255,
b: 255
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment