Skip to content

Instantly share code, notes, and snippets.

@MadeByMike
Created February 14, 2014 04:23
Show Gist options
  • Save MadeByMike/8995759 to your computer and use it in GitHub Desktop.
Save MadeByMike/8995759 to your computer and use it in GitHub Desktop.
function text_contrast(hexcolor) {
//parse hex
var rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(color);
// Convert to RGB value between 0 and 1, and retrieve luminance
// Alternative methods: (0.2126*R) + (0.7152*G) + (0.0722*B) or (0.299*R + 0.587*G + 0.114*B)
var L = (0.3 * parseInt(rgb[1], 16)/255) + (0.59 * parseInt(rgb[2], 16)/255) + (0.11 * parseInt(rgb[3], 16)/255);
console.log(L);
return L > 0.5 ? "#000": "#FFF";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment