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 '', "\n"; # How much distinct you decide with this factor my $distinct_factor = 10; for( 1 .. 30 ) { printf( "
" . ' ' x 20 . "
\n", getColor($distinct_factor * $_) ); } print ''; # Or you can choose getColor() parameter at random...