in reply to Re^2: visualizing data in a table
in thread visualizing data in a table

Would be nice to have comments/annotation in that code.
BEGIN { my %map = ( 255 => sub{ 0, 0, $_[0] * 255 }, 510 => sub{ 0, $_[0]*255, 255 }, 765 => sub{ 0, 255, (1-$_[0])*255 }, 1020 => sub{ $_[0]*255, 255, 0 }, 1275 => sub{ 255, (1-$_[0])*255, 0 }, 1530 => sub{ 255, 0, $_[0]*255 }, 1785 => sub{ 255, $_[0]*255, 255 }, ); my @map = sort{ $::a <=> $::b } keys %map; sub colorRamp1785 { my( $v, $vmin, $vmax ) = @_; ## Peg $v to $vmax if it is greater than $vmax $v = $vmax if $v > $vmax; ## Or peg $v to $vmin if it is less tahn $vmin. $v = $vmin if $v < $vmin; ## Normalise $v relative to $vmax - $vmin $v = ( $v - $vmin ) / ( $vmax - $vmin ); ## Scale it to the range 0 .. 1784 $v *= 1785; ## And look up the appropriate rgb value ## And pack that into a 32-bit integer compatible with GD true +color $v < $_ and return rgb2n( $map{ $_ }->( $v % 255 / 256 ) ) for + @map; } }

The code provides a single function colorRamp1785() which takes 3 parameters:

  1. $v: is the numeric value to map to a color on the color ramp.
  2. $vmin is the minimum value $v will take. This will be mapped to the color black (rgb:0,0,0).
  3. $vmax is the maximum value $v will take. This will be mapped to the color white (rgb:255,255,255)

As a user, all you need to do is supply the numeric value + minimum and maximum and use the return to draw your plot.

Eg. If your minimum rainfall value is 0.5" and maximum 10", then to get the right color to plot the value 2.5",

my $plotColor = colorRamp1785( 2.5, 0.5, 10 );

If the drawing package you use needs discrete RGBs rather than packed, just remove the rgb2n() call from the return line.

Does that help?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^4: visualizing data in a table
by punkish (Priest) on Mar 27, 2010 at 23:45 UTC
    Does that help?

    Oh, absolutely! I have taken the code for colorRamp and am adapting it to my use. I have not just precip, but many different ecophysical variables, so I can use it for creating visualizations of all of them. My colorRamp itself is going to be customizable so that I will be able to specify starting and ending colors (kinda like I linked to at http://colorbrewer.org). This is truly great, and thanks and attribution goes to you, of course.

    By the way, if you don't mind answering -- why do you have the colorRamp definition itself inside a BEGIN {} block?

    --

    when small people start casting long shadows, it is time to go to bed
      why do you have the colorRamp definition itself inside a BEGIN {} block?

      Just to ensure that the lookup table is built before the sub can be called.

      That would take care of itself if it was in a module. But so far, everyone who's contacted me about the snippet has had a different set of requirements of it. Some want discrete rgbs; some rgba; some as html-style 3 or 6 hex digits; some want cmyk. Everyone seems to need to adapt it some way or another.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^4: visualizing data in a table
by spx2 (Deacon) on Mar 31, 2010 at 15:01 UTC
    Hi, I was using Acme::AsciiArt2HtmlTable which makes tables with coloured cells but has some problems such as limited colours and it's a bit slow(I guess because it's in Acme namespace it's not really supposed to be used for serious stuff). anyway got your colourramp1785 and modified it a bit to work with almut's GD snippet:
    my @map=( sub{( 0, 0, $_[0] * 255 )}, sub{( 0, $_[0]*255, 255 )}, sub{( 0, 255, (1-$_[0])*255 )}, sub{( $_[0]*255, 255, 0 )}, sub{( 255, (1-$_[0])*255, 0 )}, sub{( 255, 0, $_[0]*255 )}, sub{( 255, $_[0]*255, 255 )}, ); sub ramp { my( $v, $vmin, $vmax ) = @_; ## Peg $v to $vmax if it is greater than $vmax $v = $vmax if $v > $vmax; ## Or peg $v to $vmin if it is less tahn $vmin. $v = $vmin if $v < $vmin; ## Normalise $v relative to $vmax - $vmin $v = ( $v - $vmin ) / ( $vmax - $vmin ) ; ## Scale it to the range 0 .. 1784 $v *= 1785; my @a = map { int } $map[$v/255]->( ($v % 255) / 256 ); #print join(',',@a)."\n"; return ($a[2]<<8 ) | ($a[1]<<4 ) | ($a[0] ) ; };
    I'm using it in a loop like so: $img->setPixel($x, $y, $some_val_from_a_table ,  1,16 )) ; it works ok but sometime produces colours which are too near to each other to be distinguished easily. Browser_Uk , how di dyou write your colour ramp ? I read about some colour ramps <-- here, but not sure if they fit the common usage.

      The idea behind the code started from here.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.