XP is just a number PerlMonks

### comment on

 Need Help??
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...

In reply to Re: Generating Visually Distinct Colors by cosimo
in thread Generating Visually Distinct Colors by japhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Are you posting in the right place? Check out Where do I post X? to know for sure.
• Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
• Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
• Want more info? How to link or How to display code and escape characters are good places to start.

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2023-12-02 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What's your preferred 'use VERSION' for new CPAN modules in 2023?

Results (18 votes). Check out past polls.

Notices?