November 28, 2006 17:37 | Bits / URLs / WebTech

Updated string 2 color

I needed to update my string to hexcolor function to also check the luminosity before returning a color cause I was often getting colors too bright to be legible when applied to text on a white background.

It now takes an optional $luminosity parameter. The default is 150 (0-255, the lower the darker).
It returns the first hexcolor that passes the test. If it runs thorugh all 5 possibilities without finding one, tough luck, it returns the first one in the array. (Ideally it should then test for the closest hex to the luminosity cutoff and return that but eh... I have other things to do! ;)


> Ideally it should then test for the closest hex to the luminosity cutoff

I was just doing something like this yesterday, for selecting random colors on my blog from an photo.

I used the PHP function imagecolorclosest, which finds the closest color to a specific color. In my case I ask for #eee (light background color), #333, and #666 (two darker text colors).
I'd be likely to end up with grayish results, but I transform the JPG image to an indexed palette (of 64 colors, with imagetruecolortopalette), so only the image's most used colors remain.

So I thought you could perhaps plot your 5 colors on an image and use imageclosestcolor. But still, do you really want to go that far with this script? :)

Ohhh very interesting! Merci!
But yeah, totally overkill for what I need it for. The function in question is already choking on the 1500 feed URLs it needs to aggregate and process... ;)