Skip to content

Instantly share code, notes, and snippets.

@m4scosta
Last active February 14, 2017 11:39
Show Gist options
  • Save m4scosta/7857017349112b1edd08356ba75d4392 to your computer and use it in GitHub Desktop.
Save m4scosta/7857017349112b1edd08356ba75d4392 to your computer and use it in GitHub Desktop.
// http://stackoverflow.com/a/5624139/6111717
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
// http://stackoverflow.com/a/3943023/6111717
function chooseBlackWhiteFontColor(backgroundColor) {
var rgb = hexToRgb(backgroundColor);
var luminance = 0.2126 * rgb.r + 0.7152 * rgb.g + 0.0722 * rgb.b;
return luminance > 0.179 ? '#000000' : '#FFFFFF';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment