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 an
First you consult a CIE 1964 Supplementary Standard Colorimetric Observer chart (archive)
https://imgur.com/a/JDatZNm
and look up the CIE color matching function values for the wavelength you want.
For example, i want to get the color of 455 nm light:
For our desired wavelength:
| nm | CIE color matching functions | Chromacity coordinates |
| nm | X | Y | Z | x | y | z |
|-----|----------|----------|---------|---------|---------|---------|
| 455 | 0.342957 | 0.106256 | 1.90070 | 0.14594 | 0.04522 | 0.80884 |
Note: The chromacity coordinates are simply calculated from the CIE color matching functions:
x = X / (X+Y+Z)
y = Y / (X+Y+Z)
z = Z / (Z+Y+Z)
Given that:
X+Y+Z = 0.342257+0.106256+1.90070 = 2.349913
we calculate:
x = 0.342257 / 2.349913 = 0.145945
y = 0.106256 / 2.349913 = 0.045217
z = 1.900700 / 2.349913 = 0.808838
You have your 455 nm light specified using two different color spaces:
We can also add a third color space: xyY
x = x = 0.145945
y = y = 0.045217
Y = y = 0.045217
We now have the 455 nm light specified in 3 different color spaces:
So we've converted a wavelength of pure monochromatic emitted light into a XYZ color. Now we want to convert that to RGB.
XYZ, xyz, and xyY are absolute color spaces that describe colors using absolute physics.
Meanwhile, every practical color spaces that people use:
depends some whitepoint. The colors are then described as being relative to that whitepoint.
For example,
But there is no such color as white. How do you define white? The color of sunlight?
Some people use the white of their (horribly orange) incandescent bulbs to mean white. Some people use the color of their florescent lights. There is no absolute physical definition of white - white is in our brains.
We have to pick a white. (Really you have to pick a white.) And there are plenty of whites to choose from:
I will pick a white for you. The same white that sRGB uses:
D65 (which has a color close to 6504K, but not quite because of the Earth's atmosphere), has a color of:
With that, you can convert your XYZ
into Lab
(or Luv
) - a color-space equally capable of expressing all theoretical colors. And now we have a 4th color space representation of our 445 nm monochromatic emission of light:
Lab
(and Luv
) are color spaces that are relative to some white-point. Even though you were forced to pick an arbitrary white-point, you can still represent every possible color.
RGB is not like that. With RGB:
If you specify an RGB color of (255, 0, 0), you are saying you want "just red". But there is no definition of red. There is no such thing as "red", "green", or "blue". The rainbow is continuous, and doesn't come with an arrow saying:
This is red
And again this means we have to pick three pick three primary colors. You have to pick your three primary colors to say what "red", "green", and "blue" are. And again you have many different defintions of Red,Green,Blue to choose from:
I'll pick for you. I'll pick these three colors:
Those were also the primaries chosen for by an international committee in 1996.
They created a standard that said everyone should use:
And they called that standard sRGB
.
Now that we have chosen our
we can now convert you XYZ color into RGB:
Unfortunately there are some problems with that RGB value:
So we have to round:
And now we have the closest approximation sRGB of wavelength 455 nm of light: