in reply to Re^5: Mapping function values to colors
in thread Mapping function values to colors

Indeed.

You may, nevertheless, wish to fix the fact that when $v >= $vmax both the original and the updated code give (r=  0 g=  0 b=  0) cf the expected (r=255 g=255 b=255).

As to performance, I ran a little benchmark over 1,000,000 random values -200..+200:

                  s/iter colorRamp1785_u colorRamp1785_2 colorRamp1785_3
  colorRamp1785_u   7.26              --            -19%            -65%
  colorRamp1785_2   5.85             24%              --            -57%
  colorRamp1785_3   2.54            186%            130%              --
YMMV, of course. (colorRamp1785_3 is, of course, a direct look-up, after normalising $vmin..$vmax to 0..1785.)

[Colour reproduction and colour perception being what they are, I imagine there's a cunning way of mapping a linear scale to something which appears linear. I just wish I knew what that was :-(]

Update: FWIW the fastest I can make a direct look-up is the code below, which benchmarks:

                s/iter colorRamp1785_u colorRamp1785_1 colorRamp1785_2 colorRamp1785_d
colorRamp1785_u   7.23              --             -8%            -20%            -67%
colorRamp1785_1   6.62              9%              --            -13%            -64%
colorRamp1785_2   5.79             25%             14%              --            -59%
colorRamp1785_d   2.37            205%            179%            144%              --

BEGIN { my @map = ( ( map rgb2n( 0, 0, $_), ( 0.. 255) ), # => ( + 0, 0, 0..255) ( map rgb2n( 0, $_- 255, 255), ( 256.. 510) ), # => ( + 0, 1..255, 255) ( map rgb2n( 0, 255, 765-$_), ( 511.. 765) ), # => ( + 0, 255, 254..0) ( map rgb2n($_- 765, 255, 0), ( 766..1020) ), # => (1..2 +55, 255, 0) ( map rgb2n( 255, 1275-$_, 0), (1021..1275) ), # => ( 2 +55, 254..0, 0) ( map rgb2n( 255, 0, $_-1275), (1276..1530) ), # => ( 2 +55, 0, 1..255) ( map rgb2n( 255, $_-1530, 255), (1531..1785) ), # => ( 2 +55, 1..255, 255) ) ; sub colorRamp1785_d { my ($v, $vmin, $vmax) = @_; return $map[(1785 + 1) * ($v - $vmin) / ($vmax - $vmin)] if ($v > $vmin) && ($v < $vmax) ; return $map[ 0] if ($v <= $vmin) ; return $map[1785] ; } ; } ;