Answer the question
In order to leave comments, you need to log in
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
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)
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.
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.
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
On the contrary, it won't work.
See the values of the waves along the perimeter of the figure :
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:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question