phpcsscolorshex

Given a background color, black or white text?


I'm trying to find a method for determining whether to use black or white text, given a background color (as a hex value). Has anyone dealt with this before? Is there an effective way to do this?

In my case, I would be using PHP to implement the logic (though any experience with this in other languages is welcome).


Solution

  • Take a look at this page: Calculating Color Contrast with PHP

    Keep in mind that if black and white are your only choices you're bound to have cases where neither of them works particularly great.