RGB values of visible spectrum

53,365

Solution 1

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.

Solution 2

I recently found out that my spectral colors don't work properly because they were based on nonlinear and shifted data. So I did little research and data compilation and found out that most spectrum images out there are incorrect. Also, the color ranges do not match to each other, so I used from this point only linearized real spectroscopy data like this (original link now dead):

sun real linear spectra

Here is the rectified output of mine:

spectral colors

  • the first spectrum is the best rendered spectrum I found but still way off the real thing
  • the second one is linearized Spectrum of our Sun taken from Earth
  • the last one is my current color output

Below are the RGB graphs:

This is the merge of both graphs:

graph merge

Now the code:

void spectral_color(double &r,double &g,double &b,double l) // RGB <0,1> <- lambda l <400,700> [nm]
    {
    double t;  r=0.0; g=0.0; b=0.0;
         if ((l>=400.0)&&(l<410.0)) { t=(l-400.0)/(410.0-400.0); r=    +(0.33*t)-(0.20*t*t); }
    else if ((l>=410.0)&&(l<475.0)) { t=(l-410.0)/(475.0-410.0); r=0.14         -(0.13*t*t); }
    else if ((l>=545.0)&&(l<595.0)) { t=(l-545.0)/(595.0-545.0); r=    +(1.98*t)-(     t*t); }
    else if ((l>=595.0)&&(l<650.0)) { t=(l-595.0)/(650.0-595.0); r=0.98+(0.06*t)-(0.40*t*t); }
    else if ((l>=650.0)&&(l<700.0)) { t=(l-650.0)/(700.0-650.0); r=0.65-(0.84*t)+(0.20*t*t); }
         if ((l>=415.0)&&(l<475.0)) { t=(l-415.0)/(475.0-415.0); g=             +(0.80*t*t); }
    else if ((l>=475.0)&&(l<590.0)) { t=(l-475.0)/(590.0-475.0); g=0.8 +(0.76*t)-(0.80*t*t); }
    else if ((l>=585.0)&&(l<639.0)) { t=(l-585.0)/(639.0-585.0); g=0.84-(0.84*t)           ; }
         if ((l>=400.0)&&(l<475.0)) { t=(l-400.0)/(475.0-400.0); b=    +(2.20*t)-(1.50*t*t); }
    else if ((l>=475.0)&&(l<560.0)) { t=(l-475.0)/(560.0-475.0); b=0.7 -(     t)+(0.30*t*t); }
    }
//--------------------------------------------------------------------------

Where

  • l is the wavelength in [nm] usable valueas are l = < 400.0 , 700.0 >
  • r,g,b are returning color components in range < 0.0 , 1.0 >

Solution 3

To convert a wavelength into an RGB color

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:

enter image description here

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:

  • XYZ: (0.342957, 0.106256, 1.900700)
  • xyz: (0.145945, 0.045217, 0.808838)

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:

  • XYZ: (0.342957, 0.106256, 1.900700)
  • xyz: (0.145945, 0.045217, 0.808838)
  • xyY: (0.145945, 0.045217, 0.045217)

So we've converted a wavelength of pure monochromatic emitted light into a XYZ color. Now we want to convert that to RGB.

How to convert XYZ into RGB?

XYZ, xyz, and xyY are absolute color spaces that describe colors using absolute physics.

Meanwhile, every practical color spaces that people use:

  • Lab
  • Luv
  • HSV
  • HSL
  • RGB

depends some whitepoint. The colors are then described as being relative to that whitepoint.

For example,

  • RGB white (255,255,255) means "white"
  • Lab white (100, 0, 0) means "white"
  • LCH white (100, 0, 309) means "white"
  • HSL white (240, 0, 100) means "white"
  • HSV white (240, 0, 100) means "white"

But there is no such color as white. How do you define white? The color of sunlight?

  • at what time of day?
  • with how much cloud cover?
  • at what latitude?
  • on Earth?

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.

So we have to pick a white

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 - daylight illumination of clear summer day in northern Europe

D65 (which has a color close to 6504K, but not quite because of the Earth's atmosphere), has a color of:

  • XYZ_D65: (0.95047, 1.00000, 1.08883)

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:

  • XYZ: (0.342957, 0.106256, 1.900700)
  • xyz: (0.145945, 0.045217, 0.808838)
  • xyY: (0.145945, 0.045217, 0.045217)
  • Lab: (38.94259, 119.14058, -146.08508) (assuming d65)

But you want RGB

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:

  • not only is the color relative to some white-point
  • but is is also relative to three primary colors: red, green, blue

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:

  • CIE 1931
  • ROMM RGB
  • Adobe Wide Gamut RGB
  • DCI-P3
  • NTSC (1953)
  • Apple RGB
  • sRGB
  • Japanese NTSC
  • PAL/SECAM
  • Adobe RGB 98
  • scRGB

I'll pick for you. I'll pick these three colors:

  • Red: xyY = (0.6400, 0.3300, 0.2126)
  • Green: xyY = (0.3000, 0.6000, 0.7152)
  • Blue: xyY = (0.1500, 0.0600, 0.0722)

Those were also the primaries chosen for by an international committee in 1996.

They created a standard that said everyone should use:

  • Whitepoint: D65 daylight
  • Red: (0.6400, 0.3300, 0.2126)
  • Green: (0.3000, 0.6000, 0.7152)
  • Blue: (0.1500, 0.0600, 0.0722)

And they called that standard sRGB.

The final push

Now that we have chosen our

  • white-point
  • three primaries

we can now convert you XYZ color into RGB:

  • RGB = (1.47450, -178.21694, 345.59392)

Unfortunately there are some problems with that RGB value:

  • your monitor cannot display negative green (-178.21694); that means it's a color outside what your monitor can display.
  • your monitor cannot display more blue than 255 (345.59392); the monitor only only be as blue as the blue is - it can't get any bluer. That means it's a color outside what your monitor can display.

So we have to round:

  • XYZ: (0.342957, 0.106256, 1.900700)
  • xyz: (0.145945, 0.045217, 0.808838)
  • xyY: (0.145945, 0.045217, 0.045217)
  • Lab: (38.94259, 119.14058, -146.08508) (d65)
  • RGB: (1, 0, 255) (sRGB)

And now we have the closest approximation sRGB of wavelength 455 nm of light:

enter image description here

Solution 4

Partial "Approximate RGB values for Visible Wavelengths"

Credit: Dan Bruton - Color Science

Original FORTRAN code @ (http://www.physics.sfasu.edu/astro/color/spectra.html)

Will return smooth(continuous) spectrum, heavy on the red side.

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

Ignoring gamma and intensity simple leaves:

if w >= 380 and w < 440:
    R = -(w - 440.) / (440. - 380.)
    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

Solution 5

If you want an exact match then the only solution is to perform a convolution of the x,y,z color matching functions with your spectral values so you finally get a (device-independent) XYZ color representation that you can later convert into (device-dependent) RGB.

This is described here: http://www.cs.rit.edu/~ncs/color/t_spectr.html

You can find the x,y,z color matching function for convolution here: http://cvrl.ioo.ucl.ac.uk/cmfs.htm

Share:
53,365
sorush-r
Author by

sorush-r

C/C++ Linux development Embedded Systems Electronics Hobbyist And I Love cats :&gt;

Updated on July 08, 2022

Comments

  • sorush-r
    sorush-r almost 2 years

    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
    (source: kms at www1.appstate.edu)

    sorry if this was irrelevant :-]

  • FrustratedWithFormsDesigner
    FrustratedWithFormsDesigner almost 14 years
    This would give 6 discrete RGB values. Anything for the in-between colours?
  • Andrey
    Andrey almost 14 years
    @FrustratedWithFormsDesigner no, it is gradient. w is inside the expressions, so they are f(w)
  • Rup
    Rup almost 14 years
    Aren't the R=-(w - 440.) / (440. - 350.) bits for the in-between values? R,G,B are floating-point here, not ints.
  • Rup
    Rup almost 14 years
    What's the source of the algorithm - your own from frequency tables for some colours or is this a standard computation?
  • sorush-r
    sorush-r almost 14 years
    @FrustratedWithFormsDesigner: there is not just five discrete values. (w stands for the wavelenght.) it sounds to work. i'll try it.
  • msw
    msw almost 14 years
    This answer false for reasons noted in my answer.
  • Andrey
    Andrey almost 14 years
    @msw My answer gives acceptable approximation. it is almost a conversion from hue (wavelength) to RBG. what is wrong? author needs formula first
  • Andrey
    Andrey almost 14 years
    @Rup it is not mine, but i think it corresponds well with en.wikipedia.org/wiki/File:Computer_color_spectrum.svg (see RGB decomposition below the spectrum)
  • Andrey
    Andrey almost 14 years
    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
  • msw
    msw almost 14 years
    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.
  • mctylr
    mctylr almost 14 years
    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).
  • Andrey
    Andrey almost 14 years
    @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.
  • msw
    msw almost 14 years
    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.
  • Andrey
    Andrey almost 14 years
    @mctylr yes yes yes, still hsl/hsv -> rgb breaks function into 6 regions, this is not very precise, neither is my function.
  • Spektre
    Spektre almost 10 years
    @tivus no it is not an typo it should be 5.82 as it is in the code!!! otherwise the Green end of slope will overflow to negative values around 639 nm creating invalid color peak.
  • Broseph
    Broseph almost 10 years
    couldn't this code be very simplified if you use functions to approximate the curves of each color band? One function that comes to mind that might be good is 1/sqrt(x^2+1), with each band getting the appropriate changes.
  • Spektre
    Spektre almost 10 years
    @Broseph changing simple low order polynomial with 1/sqrt does not sound good to me(speed,accuracy) if you want speed or code simplicity use table + interpolation.
  • Broseph
    Broseph almost 10 years
    I ended up using a 5th degree polynomial regression on the color bands from the photo you linked to make functions for this, though I haven't implemented it yet. I am certain though, that the readability gained from using three function calls instead of a mess of ifs and elses will more than make up for the negligible slow down.
  • Spektre
    Spektre almost 10 years
    @Broseph I agree if the fit will be good enough post it and please comment me so I see it too (for mine purposes is speed not priority but the color correctness) as I wrote above this fit was done manually so more then second degree polynomial would be horrible for edit (via mouse wheel and few keys) the if's are bad for pipelines so changing them for few multiplications may be faster. Do not forget to fit the bands to that real spectrum graph (choppy bars)
  • Ruslan
    Ruslan almost 8 years
    Your rendering doesn't seem to have enough violet. The left end of the spectrum is mostly blue, while even a rainbow from a prism looks very violet at the short wavelengths end.
  • Spektre
    Spektre over 7 years
    @Ruslan A prism's refraction angle varies nonlinearly with wavelength. So you are comparing apples and oranges This spectra has linear wavelength scale (like for diffraction gratings). When I visually compare with linear Sun's Spectra with my home made Spectroscope (DVD grating + some optics and diffraction gap) then it matches (in colors and also in proportions of the interval there is a bit more red then blue/violet). But you need to take into account also the monitor you are viewing this spectra with (especially if you got gamma and RGB correction ON because those can screw this up a lot).
  • Spektre
    Spektre over 7 years
    @Ruslan also if you monitor uses different wavelengths for the RGB then all is screwed for good as you would need to do reintegration to X,Y,Z curves for those different wavelengthd to make it right again ...
  • Swift - Friday Pie
    Swift - Friday Pie over 7 years
    @Spektre even wide gamut displays don't implement whole specter. mainly because they can't. What they do is that they emit three different ranges of wavelength, it is quantized thanks to matrix function principle. Our eyes "usually" decode that similar to monotonic wavelength for one simple reason: eye sensors are just stack of filters, each sensitive to certain wavelength range. They get partially agitated , combination of which creates "color". The first work on this topic and about"fake" perception colors was written by.. Wolfgang Goethe. It is known as "color theory" used by artists.
  • Cris Luengo
    Cris Luengo over 6 years
    Hue is the angle around the white-black axis in the RGB cube. Sure there is some sort of relationship to wavelength, but it is more accidental than by design. Note that one sextant of this plane has colors that do not map to any wavelength at all.
  • troy_s
    troy_s over 5 years
    Better to suggest the 1931 CIE 2° standard observer chart, as the governs the majority of RGB cases. See also, Colour-Science.org for the CMFS and a plethora of other useful functions.
  • Ruslan
    Ruslan about 4 years
    You pick a reasonable white point, but the problem is that many monitors have default white point that is very far from D65 (and thus are far from being sRGB-compliant by default). I've been using such monitors that have yellowish whites, closer to D50 (and adjusting RGB maxima for calibration reduced maximum brightness threefold there), as well as those with 11 000 K whites.
  • Ruslan
    Ruslan about 4 years
    BTW, you've forgotten about gamma correction. sRGB specifies a nonlinearity that can be modelled as a gamma of 2.2, so your final RGB value should be raised to the power of 1/2.2 before displaying it on the screen.
  • mistertodd
    mistertodd about 4 years
    @Ruslan I did not Also the idea of 2.2 needs to die - we've been in sRGB for over 20 years now.
  • Ruslan
    Ruslan about 4 years
    Not sure what you meant by this link. My point is that if you display #00007f and #0000ff, you'll not get the former color having half the luminance of the latter, instead something closer to 22% of the latter's luminance. And I'm also not sure what exactly idea you want to die. If you mean that piecewise linear-and-2.4 pedantry, then its difference from 2.2 is pretty negligible compared to the imperfection of gamma curves of real-world monitors.
  • mistertodd
    mistertodd about 4 years
    @Ruslan The pedantry was you said i forgot about the sRGB transfer function; i did not. And i want to remind everyone not to use 2.2 - when they can just as easily use the correct function.
  • Ruslan
    Ruslan over 3 years
    Almost a year later I've learned to appreciate your insistence on the correct sRGB transfer function. I wasn't smart enough before to take into account the relative error of the 2.2 approximation, which appears quite significant at low RGB values and sometimes noticeable even at high values. Although real monitors deviate from sRGB, calibration should fix the transfer function to the piecewise version, rather than to the power-of-2.2 one.
  • mistertodd
    mistertodd over 3 years
    @Ruslan Nice to hear that. The sRGB transfer function really only is important if you're dealing with sRGB - you're free to use the simply gamma of 2.2 if you're not following the sRGB rules. But if you're dealing with sRGB images, you want to follow the sRGB rules. And since the entire Internet, all PCs, all browsers, and all digital cameras have settled on sRGB: it's good to just use the right transfer function - especially when the changes needed to support it are so trivial.
  • Sohail Si
    Sohail Si about 2 years
    The "like this" link in your first paragraph is dead. Does not load anymore: cfa.harvard.edu/ssp/images/SolarCCD.jpg (Not Found).
  • Spektre
    Spektre about 2 years
    @SohailSi thx for noting me I added lower quality image from my archive (the original is in reverse order and 2.9MByte jpg in size which is beond imgur 2MByte limit so can not post it)