21

I have a project that would classify the color of a pixel. Whether it is red,violet, orange or simply any color in the color wheel. I know that there are over 16 million color combination for pixels. But I was able to read a web page that says its possible for me to do my project using the wavelengths of color. Please give me the formula to compute for the wavelength using RGB values. Thanks!

Amiel
  • 219
  • 1
  • 2
  • 3
  • Please provide a link to the web page you mentioned. – pavium Apr 28 '11 at 11:05
  • 1
    What if a color is not in the color wheel, e.g. gray? Such colors don't have a wavelength. – Goblin Alchemist Apr 28 '11 at 11:17
  • 1
    what about monitor calibration? – AdrianS Apr 28 '11 at 11:22
  • 1
    Hmm, is this question related with any programming language? However, not that easy task. Look this for beginning: [What Wavelength Goes With a Color?](http://science-edu.larc.nasa.gov/EDDOCS/Wavelengths_for_Colors.html). – Wh1T3h4Ck5 Apr 28 '11 at 11:38
  • You could convert from sRGB to CIE XYZ which should make it easier, but I can't find a direct conversion from that either. – Mark Ransom Dec 26 '13 at 21:12
  • I ran across a Python library [ColorPy](http://markkness.net/colorpy/ColorPy.html) that claims to do the reverse, mapping a wavelength to RGB. Perhaps it can be reversed? – Mark Ransom Feb 18 '14 at 04:12

5 Answers5

10

A pure color has a wavelength (any single color LED will have a specific wavelength). Red, green and blue each have a range of wavelength. However, when you make an RGB color, you add these wavelengths together, which will NOT give you a new wavelength. The eye can't distinguish a yellow composed of one wavelength from that of adding red and green (just how the eye works). I'd recommend reading up on color theory

http://en.wikipedia.org/wiki/RGB_color_model

Matthias Wandel
  • 6,383
  • 10
  • 33
  • 31
6

Well RGB for a monitor maps to 3 independant levels of Red Green and Blue light, so there are (mostly) 3 distinct wavelengths present of any one percieved colour.

BUT If you can convert your RGB colour value to its equivilent HSL, the H part (Hue) is the dominant colour in so far as wavelength goes if you are prepared to ignore the saturation (think of it as whiteness).

Based on that you could approximate the dominante wavelength of a colour based on its H value.

Red light is roughly 630–740nm wavelength, Violet is roughly 380–450nm.

Pete Stensønes
  • 5,595
  • 2
  • 39
  • 62
  • 2
    is is possible for UV wavelength too? I mean if the RGB of a pixel is made of a UV wave, is it possible to detect its source wavelength? please help! no one could help me :( – Soheil Jun 20 '13 at 11:38
  • 3
    @Soheil: RGB values are meant for visible light, so UV would by definition not be possible to express as an RGB value. – geon Sep 05 '13 at 06:04
  • 2
    @geon but in most android device cameras, there is no UV filter and lots of UV wavelengths are visible on the screen of device with a certain RGB.e.g. http://sci-toys.com/scitoys/scitoys/light/invisible/invisible.html – Soheil Sep 05 '13 at 07:07
  • 4
    @Soheil: Sure, cameras cameras see IR/UV light. IR is often used as "night vision" mode on video cameras. But the IR/UV light is converted to an RGB value like any other light, and it usually looks white or gray in the image, so you can't analyze the image and see if it was UV/IR. Some types of cameras, like telescopes usually *can* distinguish UV and infrared, but they have special purpose sensors, filters and software, to create not just RGB values, but RGB+IR+UV+whatever. Much more data to store, and you obviously can't just view the images with normal software that expects only RGB. – geon Sep 05 '13 at 09:19
  • 1
    @geon thanks for your useful information, helped me understand the issue better :) ;) – Soheil Sep 05 '13 at 10:05
5

Working out wavelength is a bit tricky, and as Goblin mentioned, not always possible (another example is the colour obtained by mixing equal amounts of red and blue light. That purple has no single wavelength).

But if all you want to do is identify the colour by name, then the HSV model would be a good one to use. HSV is Hue (where the colour is around the colour wheel), Saturation (how much colour there is as opposed to being a shade of black/grey/white) and Value (how bright or dark the pixel is). In this case Hue is probably exactly what you want.

If you are using a .NET language, then you're in luck. See the Color.GetHue Method which does all the work for you.

Otherwise, see HSV at Wikipedia for more details.

In essence, if you have R, G and B as floats ranging from 0.0 to 1.0 (instead of ints from 0 to 255 for example), then:

M = max(R, G, B)
m = min(R, G, B)
C = M-m

if M = m then H' is undefined (The pixel is some shade of grey)
if M = R then H' = (G-B)/C mod 6
if M = G then H' = (B-R)/C + 2
if M = B then H' = (R-G)/C + 4

When converting RGB to HSV you then multiply H' by 60 degrees, but for your purposes H' is probably fine. It will be a float ranging from 0 to 6 (almost). 0 is Red (as is 6). 1 is Yellow, with values between 0 and 1 being shaded between Red and Yellow. So 0.5 would be Orange. The important landmarks are:

0 - Red
1 - Yellow
2 - Green
3 - Cyan
4 - Blue
5 - Purple
6 - Red (again)

Hope that helps.

Allison Lock
  • 2,375
  • 15
  • 17
  • @CatShoes If you are using 0-255, then you have 256 shades, if float – as many as there are rational numbers or as many of them a float can hold :) – Lex Podgorny Oct 03 '16 at 15:46
  • Note that it says "undefined" when `C = 0` which makes sense, obviously, but in reality a gray is all color, like white, with a certain amount of absorption. It's only when you reach 100% absorption, which is black, that you can't know whether all the wavelength are present or not. – Alexis Wilke Apr 19 '18 at 04:57
1

http://en.wikipedia.org/wiki/Visible_spectrum

It is possible. See above. Gray background apparently makes it easier. You might get something like that on your own, and even improve on it. But to do it accurately will cost major dollars. U will need a colorimetry expert, a calibrated monitor and viewing environment (since what the dominant wavelength of your pixel is just means what monochromatic wavelength it approximates on your calibrated monitor in your calibrated viewing environment). All this will be a few thousand dollars. The work done at the above link, shown on wikipedia, does not seem that accurate but it is probably what you want.

Kyle Kanos
  • 3,257
  • 2
  • 23
  • 37
bob
  • 11
  • 2
  • 1
    Oh dude, he's not asking to determine the real physical wavelength of the pixels on his monitor. Just to determine it from the RGB values... – Alexis Wilke Apr 19 '18 at 04:58
1

Just convert the RGB to HSV then get the HSV value to degrees and this is the answer:

650 - 250 / 270 * D

where D is the degrees.
Considering...

Violet has a 380–450 nm wavelength, & Blue has a 450–495 nm wavelength, & Green has a 495–570 nm wavelength, & Yellow has a 570–590 nm wavelength, & Orange has a 590–620 nm wavelength, & Red has a 620–750 nm wavelength, then you just need to check if it is in those ranges, then you can classify it. Hope this helps!