Is there an algorithm to find out the wavelength of the color given the hue value (between 0 degree to 360 degree). Is there any built-in function in MATLABfor the same?
-
4Technically there are multiple ways to get a hue if you're willing to mix wavelengths, and some purples can't be represented by a single wavelength at all.– Mark RansomAug 7, 2012 at 16:22
-
@MarkRansom, which would be the easiest in matlab?– SegFaultAug 7, 2012 at 16:25
-
I can't say anything specific about Matlab. You'll probably need to convert the color space. You might find the CIE official colorimetric table to be useful: cie.co.at/index.php/LEFTMENUE/index.php?i_ca_id=298– Mark RansomAug 7, 2012 at 16:42
6 Answers
While Mark Ransom and Franco Callari are completely right that you cannot recover the spectrum of a perceptual color, nor unambiguously map hue values to wavelengths, you could definitely piece something together if you just want the corresponding monochromatic wavelength.
The part of the hue cycle between 270 and 360 is another problem. There is nothing corresponding to pink or magenta in the light spectrum, so let's assume that we only use hue values between 0 and 270 degrees.
Estimating that the usable part of the visible spectrum is 400-650nm, with wavelength L
(in nm) and hue value H
(in degrees), you can improvise this:
L = 650 - 250 / 270 * H
650 is the maximum wavelength, 250 is the wavelength range and 270 is the hue range.
I think this should be in the right direction but there may of course be room for improvement. You might be able to get better results comparing between input hues and corresponding colors on a visible spectrum chart, and then adjusting the values somewhat.
-
1
-
3@AkshayHazari: You could look up the numbers I used in any physics textbook. As for the formula itself, no I just made it up. But I explained how I derived it.– JunuxxMay 30, 2017 at 13:41
-
I had a feature to recognize a dominant color in an image by binning the image colors and checking which color is it closest to. If I was able to get wavelength or a single value representing a color it would greatly reduce the complexity. Is there any way I could tweak this a little and use it , so far I haven't found anything, near to representing rgb or hue values like you have. May 31, 2017 at 5:24
-
Wavelength is not a full representation of an RGB color though. The above formula approximates a mapping from hue (from the HSV color representation) to wavelength. If wavelength is a sufficient color representation for your application, you might as well take the hue value. RGB to HSV conversion is well-defined.– JunuxxMay 31, 2017 at 5:32
-
Yes that makes sense. In that case what would be the best boundaries to classify VIBGYOR for hue values (Rainbow colors). en.wikipedia.org/wiki/Spectral_color and workwithcolor.com/red-orange-color-hue-range-01.htm . The wiki link and some more pages give me definite values for identifying the colors but no ranges. May 31, 2017 at 5:46
I cant provide simple solution, but there is something you need to consider:
- The visible part of the spektrum is roughly between 380nm (UV-border) and 780nm (IR-border). But what you see (hue) depends on the cone-cells triggered. Above 660nm, the M-cone is not triggered at all, so everything between 660nm and 780nm is hue 0°.
- at 580nm you have yellow with hue 60°, the purest green is at about 535nm, so that is 120°, and the purest blue (240°) is at about 457nm.
- if you apply a linear function, yellow should be at 597nm - which it is not, so you'd need a more complex approach.
- above blue, the red cone still gets triggered until we see violet, but we wont reach red again on higher frequencies, so you cant go above approximately 300°.
- the hue range between 300° and 360° has no æquivalent in visible spektrum, it can only be simulated by mixing high frequency light (blue or violet) with red light, which results in something between magenta and red on the purple-line.
It is possible to find the dominant wavelength of a color/hue. But as said most colors arn’t monochromatic and the same color can be constructed with different “mixes” of wavelengths. I.e. metamerism. Also, for the extra spectral magenta and violet colors only a complementary wavelength can be specified. I.e. the hue/dominant wavelength that additively mixes to white. Also white must be specified, since the is no absolute white due to adaption. Also psychologically our perception of hues doesn’t follow dominant hue lines. Se the Munsell and NCS systems.
Here you can calulate dominant wavelength from RGB values or different CIE systems: http://www.brucelindbloom.com/index.html?Calc.html I don’t have the formula though.
You can then transform RGB to/from HSL and similar. And to/from Munsell or NCS perceptual hues (NCS values are proprietary, so you have to pay and use their software).
Short answer: NO. A given hue can in general be produced by a triple infinity of wavelengths.
A "physical color" is a combination of pure spectral colors (in the visible range). In principle there exist infinitely many distinct spectral colors, and so the set of all physical colors may be thought of as an infinite-dimensional vector space (a Hilbert space). This space is typically notated Hcolor. More technically, the space of physical colors may be considered to be the topological cone over the simplex whose vertices are the spectral colors, with white at the centroid of the simplex, black at the apex of the cone, and the monochromatic color associated with any given vertex somewhere along the line from that vertex to the apex depending on its brightness.
. . .
This system implies that for any hue or non-spectral color not on the boundary of the chromaticity diagram, there are infinitely many distinct physical spectra that are all perceived as that hue or color. So, in general there is no such thing as the combination of spectral colors that we perceive as (say) a specific version of tan; instead there are infinitely many possibilities that produce that exact color. The boundary colors that are pure spectral colors can be perceived only in response to light that is purely at the associated wavelength, while the boundary colors on the "line of purples" can each only be generated by a specific ratio of the pure violet and the pure red at the ends of the visible spectral colors.
The CIE chromaticity diagram is horseshoe-shaped, with its curved edge corresponding to all spectral colors (the spectral locus), and the remaining straight edge corresponding to the most saturated purples, mixtures of red and violet.
(Source)
-
2
-
I found this site that converts a given wavelength to a hue. With a bit of work, you could actually reverse the process. It's not ideal, but I trust the guy who is a consultant in applied mathematics more than myself in solving this issue. That's that.
https://www.johndcook.com/wavelength_to_RGB.html
function convert(input) {
var w = parseFloat(input);
if (w >= 380 && w < 440) {
r = -(w - 440) / (440 - 380);
g = 0.0;
b = 1.0;
} else if (w >= 440 && w < 490) {
r = 0.0;
g = (w - 440) / (490 - 440);
b = 1.0;
} else if (w >= 490 && w < 510) {
r = 0.0;
g = 1.0;
b = -(w - 510) / (510 - 490);
} else if (w >= 510 && w < 580) {
r = (w - 510) / (580 - 510);
g = 1.0;
b = 0.0;
} else if (w >= 580 && w < 645) {
r = 1.0;
g = -(w - 645) / (645 - 580);
b = 0.0;
} else if (w >= 645 && w < 781) {
r = 1.0;
g = 0.0;
b = 0.0;
} else {
r = 0.0;
g = 0.0;
b = 0.0;
}
// Let the intensity fall off near the vision limits
if (w >= 380 && w < 420)
factor = 0.3 + 0.7 * (w - 380) / (420 - 380);
else if (w >= 420 && w < 701)
factor = 1.0;
else if (w >= 701 && w < 781)
factor = 0.3 + 0.7 * (780 - w) / (780 - 700);
else
factor = 0.0;
var gamma = 0.80;
var R = (r > 0 ? 255 * Math.pow(r * factor, gamma) : 0);
var G = (g > 0 ? 255 * Math.pow(g * factor, gamma) : 0);
var B = (b > 0 ? 255 * Math.pow(b * factor, gamma) : 0);
return [R, G, B]
}
There's no conversion because they don't overlap.
Hue moves you around an RGB colour space, usually sRGB that almost all consumer digital equipment uses. That's a subset of the colours that our visual systems recognise under normal conditions (defined by CIE 1931), and does not overlap the vibrant line of colours perceived at monochromatic wavelengths of light at all.
Though Hue from 0-120 (reddish orange to yellowish green) and near 240 (indigo) are reasonably close, sRGB is quite functional if you don't care about all the washed out greens and blues, and you can fake the violet and red ends of the full spectrum by making them darker Hue around 270 or 330 respectively, and the only place you can't really approximate is around 180, computer cyan just isn't close at all to the monochromatic vibrant blue-greens.