I usually use something like this to generate colors for sections of histogram or pie charts.
sub getColor
{
my $index = shift; # Numerical index (integer from 0 to ..
+.)
my @color; # Color components
my @hue_matrix = (
#
# R G B Component to modify(0=R, 1=G, 2=B)
# | | | |
# V V V V Add or subtract the offset
# | from (r,g,b) triplet
# V
[ 255, 0, 0, 2, 1 ], # Sector 1 R -> R+B
[ 255, 0, 255, 0, -1 ], # Sector 2 R+B -> B
[ 0, 0, 255, 1, 1 ], # ... B -> Cyan
[ 0, 255, 255, 2, -1 ], # ... Cyan-> G
[ 0, 255, 0, 0, 1 ], # ... ...
[ 255, 255, 0, 1, -1 ],
[ 255, 0, 0, 2, 1 ]
);
# Select a "spectrum sector", according to my hue matrix
my $sector = $hue_matrix[ int($index / 42) % @hue_matrix ];
# Calculate an offset to be applied to starting color
my $offset = ($index % 42) * 6;
@color = @$sector[0..2];
# Modify selected component to generate a "continuous gradient"
$color[$sector->[3]] += ($sector->[4] > 0 ? $offset : -$offset);
return(@color);
}
# Generate visually distinct colors...
print '<HTML><BODY>', "\n";
# How much distinct you decide with this factor
my $distinct_factor = 10;
for( 1 .. 30 )
{
printf(
"<TABLE BGCOLOR=#%02X%02X%02X><TR><TD>" . ' ' x 20 . "</T
+D></TR></TABLE>\n",
getColor($distinct_factor * $_)
);
}
print '</BODY></HTML>';
# Or you can choose getColor() parameter at random...
The final result is:
... that of course is not perfect, but adjusting the $distinct_factor can provide satisfying results.
Update: Sorry, it was missing the last piece of code...