BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:
I want to represent a range of numerical values (approx: 0 -> 1000) using colors such that there is an visually obvious gradient between low and high values, with similar values being visually similar. Kind of like astronomers use to show differences and similarities on IR or UV photographs.
Should I use a lookup table (and is there one available)?
Or can I get away with a formula to map them (and does anyone know such a formula)?
Update: I finally found a reference for what astronomers use--it's call a Color Ramp, and there is a standard one.
The reference for anyone following along is Color Ramp and there is an implementation in C at C source.
I've translated this into Perl as:
sub colorRamp { my( $v, $vmin, $vmax ) = @_; my( $r, $g, $b ) = (1) x 3; $v = $vmax if $v > $vmax; $v = $vmin if $v < $vmin; ## Uncomment below to invert the color range ## so that small numbers are hot (red) ## and high numbers are cold (blue) ## $v = $vmax + $vmin - $v; my $dv = $vmax - $vmin; if( $v < ( $vmin + 0.25*$dv ) ) { $r = 0; $g = 4 * ($v - $vmin) / $dv; } elsif( $v < ( $vmin + 0.5 * $dv ) ) { $r = 0; $b = 1 + 4 * ($vmin + 0.25 * $dv - $v) / $dv; } elsif( $v < ( $vmin + 0.75 * $dv ) ) { $r = 4 * ($v - $vmin - 0.5 * $dv) / $dv; $b = 0; } else { $g = 1 + 4 * ($vmin + 0.75 * $dv - $v) / $dv; $b = 0; } ## Convert the 0->1 range rgb values to 0->255 ## and output as a web/tk style "#rrggbb" hex string return sprintf "#%02x%02x%02x", $r*255, $g*255, $b*255; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: A hierarchy of color (intensity)?
by creamygoodness (Curate) on Nov 22, 2005 at 06:53 UTC | |
by BrowserUk (Patriarch) on Nov 22, 2005 at 09:47 UTC | |
by creamygoodness (Curate) on Nov 22, 2005 at 14:58 UTC | |
Re: A hierarchy of color (intensity)?
by Zaxo (Archbishop) on Nov 22, 2005 at 06:56 UTC | |
Re: A hierarchy of color (intensity)?
by Perl Mouse (Chaplain) on Nov 22, 2005 at 10:18 UTC | |
Re: A hierarchy of color (intensity)?
by inman (Curate) on Nov 22, 2005 at 10:01 UTC | |
Re: A hierarchy of color (intensity)?
by YuckFoo (Abbot) on Nov 22, 2005 at 20:55 UTC | |
Re: A hierarchy of color (intensity)?
by renodino (Curate) on Nov 22, 2005 at 16:18 UTC |