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! ;)