tags:

views:

229

answers:

3

I need an algorithm or function to map each wavelength of visible range of spectrum to its equivalent RGB values. Is there any structural relation between the RGB System and wavelength of a light? like this image: alt text sorry if this was irrelevant :-]

+1  A: 

w - wavelength, R, G and B - color components

this one for example:

if w >= 380 and w < 440:
    R = -(w - 440.) / (440. - 350.)
    G = 0.0
    B = 1.0
elif w >= 440 and w < 490:
    R = 0.0
    G = (w - 440.) / (490. - 440.)
    B = 1.0
elif w >= 490 and w < 510:
    R = 0.0
    G = 1.0
    B = -(w - 510.) / (510. - 490.)
elif w >= 510 and w < 580:
    R = (w - 510.) / (580. - 510.)
    G = 1.0
    B = 0.0
elif w >= 580 and w < 645:
    R = 1.0
    G = -(w - 645.) / (645. - 580.)
    B = 0.0
elif w >= 645 and w <= 780:
    R = 1.0
    G = 0.0
    B = 0.0
else:
    R = 0.0
    G = 0.0
    B = 0.0
Andrey
This would give 6 discrete RGB values. Anything for the in-between colours?
FrustratedWithFormsDesigner
@FrustratedWithFormsDesigner no, it is gradient. w is inside the expressions, so they are f(w)
Andrey
Aren't the `R=-(w - 440.) / (440. - 350.)` bits for the in-between values? R,G,B are floating-point here, not ints.
Rup
What's the source of the algorithm - your own from frequency tables for some colours or is this a standard computation?
Rup
@FrustratedWithFormsDesigner: there is not just five discrete values. (w stands for the wavelenght.) it sounds to work. i'll try it.
Sorush Rabiee
This answer false for reasons noted in my answer.
msw
@msw My answer gives acceptable approximation. it is almost a conversion from hue (wavelength) to RBG. what is wrong? author needs formula first
Andrey
@Rup it is not mine, but i think it corresponds well with http://en.wikipedia.org/wiki/File:Computer_color_spectrum.svg (see RGB decomposition below the spectrum)
Andrey
The biggest error is in the completely arbitrary choices of the brackets for `w`. As I noted, one can only make gross approximations, but this example implies 3 significant digits of precision thus giving the appearance of accuracy where there is none.
msw
I assume that's wavelength in nm, nanometers (_not nautical miles_). This appears to be a fairly simple (i.e. "crude" for many purposes) approximation for light (not pigment) that I believe could be compared to using only the Hue and ignoring the Saturation and Level / Value in a HSV/HSL colorspace, that may or may not be suitable depending on application (which the OP didn't specify).
mctylr
@msw depends on application. if you write scientific app then yes it is bad, but if you want to create rolling rainbow it is fine.
Andrey
@mctylr yes yes yes, still hsl/hsv -> rgb breaks function into 6 regions, this is not very precise, neither is my function.
Andrey
+3  A: 

There is a relationship between frequency and what is known as Hue, but for complicated reasons of perception, monitor gamut, and calibration, the best you can achieve outside of expensive lab equipment is a gross approximation.

See http://en.wikipedia.org/wiki/HSL_and_HSV for the math, and note that you'll have to come up with your best guess for the Hue ⇔ Frequency mapping. I expect this empirical mapping to be anything but linear.

msw
the whole math for Hue -> RGB mapping is approximation (linear). Hue -> freq can be mapped as wavelength 390 to 750 nm maps to hue [0, 1]. freq = c / wavelength
Andrey
Note my use of the word "empirical" and then try it for yourself whilst remembering that our perception is trichromat and distinctly non-linear and that monitors are a different trichromatic and are also extremely non-linear. There is an entire calibration industry built around these effects.
msw
+2  A: 

This is most of what color profiles deal with. Basically, for a given device (scanner, camera, monitor, printer, etc.) a color profile tells what actual colors of light will be produced by a specific set of inputs.

Also note that for most real devices, you only deal with a few discrete wavelengths of light, and intermediate colors are produced not by producing that wavelength directly, but by mixing varying amounts of the two neighboring wavelengths that are available. Given that we perceive color in the same way, that's not really a problem, but depending on why you care, it may be worth knowing anyway.

Without a color profile (or equivalent information) you lack the information necessary to map RGB value to colors. An RGB value of pure red will normally map to the reddest color that device is capable of producing/sensing (and likewise, pure blue to the bluest color) -- but that "reddest" or "bluest" can and will vary (widely) based on the device.

Jerry Coffin