Thread: Pixel color conversion question!

  1. #16
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by brewbuck View Post
    I've always thought this was weird. The sun does put out the most light near green, and yet plants, which use sunlight to produce food, most strongly REFLECT this portion of the spectrum. A plant that absorbed most strongly in green would have a color more like magenta. Why don't we have magenta plants?
    I don't think sunlight is most green. It's just that green happens to be in the middle of the spectrum. Plants (the ones that use photosynthesis) are green because they actually absorb red and blue. They have no use for green and so it is reflected.
    ( Chlorophyll | Causes of Color )

  2. #17
    Registered User
    Join Date
    Jun 2010
    Posts
    53
    iMalc,

    As long as we reminise on this ... just to understand your formula here,

    my green = 0-128-0... to get my new red in respect to the rgb green:

    (0*30%) + (128*59%) + (0*11%) = 75.52 >>>>> max(75.52, 255); ???

    r

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm guessing he meant min, not max.

  4. #19
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So the sun actually produces white light (a mix of several wavelengths) as well as a lot of non-visible radiation. Of the visible radiation, the yellow-green wavelengths are most intense. It appears to be related to the temperature of the sun.

    While this may not be a direct cause of our vision and plant color, I would wager it factors in pretty heavily. It would stand to reason that we are sensitive to green light because plants are green. Being able to detect subtle hues in plants may be the difference between a poisonous plant and some beneficial herb. Plants do appear green because chlorophyll reflects green wavelengths, but as for why it reflects green, I think the answer has to do with molecule structure. As for why plants evolved to use chlorophyll instead of some other molecule that would absorb the most intense wavelengths of light, here is an interesting theory.

    This has turned into a very fun and informative tangent...

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    So the sun actually produces white light (a mix of several wavelengths) as well as a lot of non-visible radiation. Of the visible radiation, the yellow-green wavelengths are most intense. It appears to be related to the temperature of the sun.
    This is also related to the receptors in our eyes. Our Retinas have photon receptors for Red, Blue and Yellow light. Green seems brighter because it hits both Blue and Yellow receptors. Same for Purple ... red and blue and Orange... red and yellow. It's also why green laser pointers are so dangerous.

  6. #21
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by tabstop View Post
    I'm guessing he meant min, not max.
    Yes I did. Thanks for the correction.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #22
    Registered User
    Join Date
    Jun 2010
    Posts
    53
    Imalc,

    So just to recap if my green = 0-128-0... to get my new red in respect to the rgb green:

    (0*30%) + (128*59%) + (0*11%) = 75.52 >>>>> min(75.52, 255); (Okay so I will put up with the color still being maroonish.. based on all that human biology theory about how the human eye percieves color)

    But ... what happens if my green needs to be converted to its equivalent yellow... or grey... or teal...??? Or what if a teal nedds to be converted to a yellow or purple???

    Althought the function of perceived brightness is and will always be:
    30% of the red value, 59% of the green value, and 11% of the blue value

    What would be the new formulas for the other color conversions just mentioned ??? Confused!

    If I do the same calculation I get the same result ?

    r

  8. #23
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    But ... what happens if my green needs to be converted to its equivalent yellow... or grey... or teal...??? Or what if a teal nedds to be converted to a yellow or purple???
    What are you talking about? If the originaol color did not have that information in it then it won't be in the converted color either. You can transform colors between color spaces just as you can coordinates or any other values. You are not creating a new color. You are simply transforming it. The formula that has been given for RGB is the correct.

    This converts a color to greyscale:
    r = g = b = 0.2125*r + 0.7154*g + 0.0721*b;

    This adjusts its contrast:
    pOut->r = 0.5f + c * (pC->r - 0.5f);
    pOut->g = 0.5f + c * (pC->g - 0.5f);
    pOut->b = 0.5f + c * (pC->b - 0.5f);

    This adjusts its saturation:
    FLOAT grey = pC->r * 0.2125f + pC->g * 0.7154f + pC->b * 0.0721f;
    pOut->r = grey + s * (pC->r - grey);
    pOut->g = grey + s * (pC->g - grey);
    pOut->b = grey + s * (pC->b - grey);

    Note that all color components in the equations are in range 0.0f to 1.0f.

    As has been said RGB is not a good color space to do any type of calculations in. It would be better to convert to HSV, perform the color operations, and then convert back. More information about colors can be found in the Direct3D SDK, various graphics books, and/or online.

  9. #24
    Registered User
    Join Date
    Jun 2010
    Posts
    53
    >What are you talking about?

    Stop! I never used HSV or HSL! Doesn't it show!

    /*
    r = g = b = 0.2125*r + 0.7154*g + 0.0721*b;

    This adjusts its contrast:
    pOut->r = 0.5f + c * (pC->r - 0.5f);
    pOut->g = 0.5f + c * (pC->g - 0.5f);
    pOut->b = 0.5f + c * (pC->b - 0.5f);

    This adjusts its saturation:
    FLOAT grey = pC->r * 0.2125f + pC->g * 0.7154f + pC->b * 0.0721f;
    pOut->r = grey + s * (pC->r - grey);
    pOut->g = grey + s * (pC->g - grey);
    pOut->b = grey + s * (pC->b - grey);
    */

    Not really helpful for the time being .... Now I know I need to learn the details on how to convert RGB to HSL! Just the math and no code pollution .... I need a small mathematical sample on how to convert rgb to hsl. I will do some searches on line.

    thanks
    r

  10. #25
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I recommend looking at an HSV/HSL cone or cylinder, then finding a color picker that will give you HSV/HSL and RGB values and playing with it to get a feel for what the conversion does. Then look up the math.

    To help you out with the conversion, here is a quick summary of HSV (HSL is similar, but not identical):
    H: Hue. This tells you whether you have red, blue, green, yellow, etc. It is usually expressed as an angle from 0-360 degrees.
    S: Saturation. This is tells you how "washed out" a color is, i.e. does it look like red paint or red paint that has faded or been mixed with white paint. You can think of this as a spectrum from white (S = 0) to pink to light red to full red (S = 1).
    V: Value. This tells you how much light of the given color you have. This is like how much black paint you mixed in with your red. It gives you a spectrum from black (V = 0), to dark red to full red (V = 1).

    The reason HSV will work so well for what you're doing (since you're not concerned with perceived color) is that you don't have any complicated math. You only have to "rotate" your H value. Pure red has a hue of 0 degrees, green 120 and blue 240. Thus, to rotate your color from green to the equivalent shade of blue, you simply add 120 degrees to your hue. From green to red, you subtract 120, etc. For other colors like teal, you would have to find the hue values (visually or from some "authority" like Pantone), and add/subtract the right amout. Converting to grey will depend on whether you're using HSL or HSV, but should simply involve setting either L or S to zero.

  11. #26
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by see the big C View Post
    Imalc,

    So just to recap if my green = 0-128-0... to get my new red in respect to the rgb green:

    (0*30%) + (128*59%) + (0*11%) = 75.52 >>>>> min(75.52, 255); (Okay so I will put up with the color still being maroonish.. based on all that human biology theory about how the human eye percieves color)

    But ... what happens if my green needs to be converted to its equivalent yellow... or grey... or teal...??? Or what if a teal nedds to be converted to a yellow or purple???
    That doesn't look right at all. What I wrote earlier was correct except that I had max instead of min. However all that does is swap the red and green components.

    To this moment we still do not know what kind of colour conversion you desire. I am assuming the the input colour is one of the 16.7 million colours possible with 24-bit colour and the the output is the same. You still need to work out what conversion you actually want. Perhaps it's a hue rotation, perhaps it's swapping colour channels.
    How about you tell us what each of the following colours would be transformed into?:
    Red (255,0,0)
    Green (0,255,0)
    Blue (0,0,255)
    White (255,255,255)
    Black (0,0,0)
    Grey (128,128,128)
    Yellow (255,255,0)
    Magenta (255,0,255)
    Cyan (0,255,255)
    Brown (128,128,0)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Curious text performance question...
    By Raigne in forum Game Programming
    Replies: 4
    Last Post: 02-07-2008, 09:14 PM
  2. a question on producing a pixel histogram of a picture
    By tracyhaha in forum C Programming
    Replies: 3
    Last Post: 04-07-2007, 07:27 AM
  3. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  4. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM