How to get the wavelength of a pixel using RGB?

前端 未结 4 1967
深忆病人
深忆病人 2020-12-06 01:06

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 col

4条回答
  •  再見小時候
    2020-12-06 01:18

    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.

提交回复
热议问题