PDA

View Full Version : Need algorithm that will map floats to RGB



CelticBlues
07-30-2009, 09:20 AM
I am looking for an algorithm that will map a range of floats to RGB. My google skills are apparently lacking as I can't find exactly what I need. Here is what I want to do:

I have a range of floats: fmin <= f <= fmax
I want to map f to RGB such that

fmin = RGB(0,0,0)
fmax = RGB(255,255,255)

For example see this image:

http://www.maxmax.com/images/Cameras/TIR2/CatColdToHotBlue.jpg

Any suggestions? Pointers to reading info? etc?
CD

Bruce Wheaton
07-30-2009, 09:47 AM
In what? GLSL, C?

I don't think Google is what you need, it's just a C book, or a simple mathematics course - possibly just a cup of tea and a piece of paper.

Anyway, hoping I'm not doing your homework...

Work out the range. Take each float and subtract the min, then divide by the range. That gives you the position of the float in a 0-1 range. Set your color so that red, green and blue are that position, and alpha is 1.0. Scale it to 255 if you need to - OpenGL is 0-1.0.

Bruce

dletozeun
07-30-2009, 12:44 PM
If you don't want to interpolate between colors from black to white (grayscale) but an arbitrary spectrum, you can compute the "range position" as Bruce explained and then use it as a texture coordinate in a 1D texture that represents the wanted color spectrum.

Dark Photon
07-31-2009, 05:15 AM
If you don't want to interpolate between colors from black to white (grayscale) but an arbitrary spectrum, you can compute the "range position" as Bruce explained and then use it as a texture coordinate in a 1D texture that represents the wanted color spectrum.
This is pretty standard fare in scientific visualization. The OpenDX (http://www.opendx.org/OpenDX) package for instance uses the AutoColor (http://www.research.ibm.com/dx/docs/legacyhtml/pages/refgu016.htm#HDRAUTOCOL) module to do this. You can change it, but in the default colormap it uses "the minimum data value is blue, the maximum data value is red, and data values between are blue to red through cyan, green, and yellow" (example here (http://www-lsp.ujf-grenoble.fr/recherche/a3t2/a3t2a2/bahram/OpenDX/index.html)). There's your 1D texture.

Of course you can use whatever color ramp you want.