I'm assembling line graphs with a varying number of series (lines), and I need to generate hexadecimal colors for them all. With 5 or 10 lines, this is easy enough, just with a random number generator (and a quick conversion to hex):
function random_palette($count) {
// where we'll store our generated colors
$palette = array();
// keep going until we fill the palette
while(count($palette) < $count) {
// randomly create a color
$color
= sprintf( '%03s', dechex(mt_rand(0,255)) )
. sprintf( '%03s', dechex(mt_rand(0,255)) )
. sprintf( '%03s', dechex(mt_rand(0,255)) );
// insert into palette if not already there
if(! in_array($color, $palette) ) $palette[] = $color;
}
return $palette;
}
For a much larger number of lines, however, I'm at a loss. The above function generates colors of a relatively close range (consistently blueish/greenish), and for 20 to 30 lines, that quickly becomes unreadable. 40 or 50 looks like a train wreck.
So what I need is to generate N number of colors from all across the spectrum, but ensure that they're all (mostly) visually distinct enough that someone looking at the graph can actually figure out what they're looking at. Truth be told, I have no idea where to even start...