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

In reply to Re^6: Mapping function values to colors by gone2015
in thread Mapping function values to colors by spx2

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.