How to know if you have a light or a dark color

There is a simple way to know this. The following code takes white (#ffffff) divided it by two as our reference color. If the comparing color is “lower” than the reference color, then you have a dark color, otherwise you have a light color.

function isLightOrDark($hexcolor) {
 return (hexdec($hexcolor) > 0xffffff/2) ? 'light color' : 'dark color';

Leave a Reply