Once I tried to come up with a solution to mark some stuff (on a HTML page) with dynamic values from red to green based on these values. The positive values green and the negative ones red.
Today I stumbled over this piece of code again. I thought it might be optimizable and did some benchmarking. After I found nothing else to optimize in this snippet (suggestions?), I decided to post it. Maybe, someone will some day need a piece of code like this, too.
sub calcColor ($$$)
{
my ($min, $max, $value) = @_; # min = the minimal (possible)
+value
# max = the maximal (possible)
+value
# value = the value you want a
+color for
my $color; # be strict
my $all = ($value < 0 ? $min : $max);# calculate with minimal or max
+imal value
$all || return 'FFFF00'; # avoid divisions by zero
$color = sprintf('%.2X', 255 - int($value/$all*255)); #calculate and
+ make hex
return ($value < 0 # return
? 'FF'.$color.'00' # either a red
: $color.'FF00' # or a green
);
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.