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:
Ie. If you pass in ( 150, 100, 200 ), the $v becomes 0.5
Continuing the above example, $v now becomes 1785 * 0.5 = 892.5.
Rather than having a huge 1785 entry lookup table, it uses a hash of subs to perform the mapping.
$v = 0 => rgb(0,0,0); 1 => rgb(0,0,1); ... 255 => rgb(0,0,255)
The color transitions from black to blue.
$v = 256 => rgb(0,0,255); 257 => rgb(0,1,255); ... 511 => rgb(0,255,255)
The color transitions from blue to cyan.
$v = 512 => rgb(0,255,255); 513 => rgb(0,255,254); ... 783 => rgb(0,255,0)
The color transitions from cyan to green.
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: visualizing data in a table
by punkish (Priest) on Mar 27, 2010 at 23:45 UTC | |
by BrowserUk (Patriarch) on Mar 28, 2010 at 01:15 UTC | |
|
Re^4: visualizing data in a table
by spx2 (Deacon) on Mar 31, 2010 at 15:01 UTC | |
by BrowserUk (Patriarch) on Mar 31, 2010 at 17:53 UTC |