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