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 ); }