Skip to content

Instantly share code, notes, and snippets.

@abdel-ships-it
Last active October 9, 2017 09:08
Show Gist options
  • Save abdel-ships-it/bfc9a4b0f78936ff88c753d392408ece to your computer and use it in GitHub Desktop.
Save abdel-ships-it/bfc9a4b0f78936ff88c753d392408ece to your computer and use it in GitHub Desktop.
Color contrast
/**
* @author David Halford
* @see https://codepen.io/davidhalford/pen/ywEva?editors=0010
* Finds an appropriate contrasting color depending on an rgb color, contains hex to rgb conversion :)
*/
/* about half of 256. Lower threshold equals more dark text on dark background */
const threshold = 130;
const cutHex = (h) => (h.charAt(0) === '#') ? h.substring(1, 7) : h;
const red = parseInt((cutHex(hexColor)).substring(0, 2), 16);
const green = parseInt((cutHex(hexColor)).substring(2, 4), 16);
const blue = parseInt((cutHex(hexColor)).substring(4, 6), 16);
const cBrightness = ( (red * 299) + ( green * 587 ) + ( blue * 114) ) / 1000;
if (cBrightness > threshold) {
return //dark color;
} else {
return // light color;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment