Andres Jaimes

How to know if a hex color is light or dark

By Andres Jaimes

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 the given color is dark, otherwise it’s light color.

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