Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Generating Visually Distinct Colors

by cosimo (Hermit)
on Mar 23, 2005 at 12:18 UTC ( #441747=note: print w/replies, xml ) Need Help??


in reply to Generating Visually Distinct Colors

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>" . '&nbsp;' 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...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://441747]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2020-07-07 09:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?