C
C
cicatrix2021-09-22 14:12:39
Physics
cicatrix, 2021-09-22 14:12:39

Algorithm for converting RGB components to wavelength?

Hello, does anyone have any ideas on how to convert RGB component values ​​of a color to EM wavelength and vice versa?
The task is simple - the wavelength is set, it is necessary to find the RGB values ​​that are closest to it. Well, vice versa.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
R
Rsa97, 2021-09-22
@cicatrix

Wavelength in RGB.
Taken from https://www.johndcook.com/wavelength_to_RGB.html

const w = 640 // Длина волны
let red, green, blue;

if (w < 380 || w > 781) {
  [red, gren, blue] = [0, 0, 0];
} else if (w < 440) {
  [red, green, blue] = [(440 - w) / 60, 0, 1];
} else if (w < 490) {
  [red, green, blue] = [0, (w - 440) / 50, 1];
} else if (w < 510) {
  [red, green. blue] = [0, 1, (510 - w) / 20];
} else if (w < 580) {
  [red, green, blue] = [(w - 510) / 70, 1, 0];
} else if (w < 645) {
  [red, green, blue] = [1, (645 - w) / 65, 0];
} else {
  [red, green, blue] = [1, 0, 0];
}

let factor;
if (w < 380 || w > 781) {
  factor = 0;
} else if (w < 420) {
  factor = 0.3 + 0.7 * (w - 380) / 40;
} else if (w < 701) {
  factor = 1.0;
} else {
  factor = 0.3 + 0.7 * (780 - w) / 80;
}

const gamma = 0.8;

const R = (red > 0 ? 255 * Math.pow(red * factor, gamma) : 0);
const G = (green > 0 ? 255 * Math.pow(green * factor, gamma) : 0);
const B = (blue > 0 ? 255 * Math.pow(blue * factor, gamma) : 0);

const color = `rgb(${R}, ${G}, ${B})`;
console.log(color); // rgb(255, 32.763138565028974, 0)

W
Wataru, 2021-09-22
@wataru

It is not the tag "physics" that is needed here, but "biology". From the point of view of physics - this task does not make sense, well, a set of intensity of three lengths does not correspond to the length of some other color.
It is a challenge to find a wavelength that stimulates the average human eye in the same way as a set of three different intensities. Here you need to know the sensitivity spectra of different cones and dance from there.
You also need to remember about the specifics of the monitors - these same numbers in RGB set the square roots of the glow intensity of different pixels.
Also, it might be easier to convert the RGB to some YUV or HSL first.

U
U235U235, 2021-09-22
@U235U235

You need to convert the RGB value to the XYZ system, then on the xy diagram (color locus, CIE 1931) draw a ray from the white point (for a given light source A, C, D65 ...) through a given xy point. The point where the beam intersects with the spectral color curve will give the dominant wavelength. For magenta colors, the beam is drawn in the opposite direction. Usually they put a stroke at the lambda. From a programmatic point of view, you can use splines. References: Judd, Wyszecki.614b2c0f414c6310716842.jpeg

L
Leonid, 2021-09-22
@caballero

RGB these three wavelengths only the intensity is different.
in some one length it does not translate
brown in the spectrum, for example, does not exist at all

A
Alexander Skusnov, 2021-09-22
@AlexSku

On the contrary, it won't work.
See the values ​​of the waves along the perimeter of the figure :
%D0%A4%D0%B0%D0%B9%D0%BB:CIE1931xy_blank.svg

D
dollar, 2021-09-22
@dollar

If the color is as if pure, i.e. shines at one wavelength (and with one specific frequency), then it will correspond to one specific "color": either R, or G, or B, or something in between. And if between, then the eye may not see it at all.
LED lamps are built on this principle. Why waste electricity on light in a range that is not visible?
Natural light is usually a whole spectrum. And RGB is just 3 dots on it (conditionally). Here I sketched an example spectrum:

spoiler
614b1f51996c6771307024.png

And now the main question. Well, suppose we are resigned to the fact that out of the entire spectrum we know only 3 values. In the end, such is human vision, nothing can be done. And they found a conversion function to length, that is, to a single number. Then how in this case, from the singular number, again get three RGB values? Nothing .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question