Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* @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