Thread: Pixel color conversion question!

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    53

    Pixel color conversion question!

    Hello,

    If we have a full green pixel with the RGB weight of:

    00 - 128 - 00

    and we want to convert it to its equivalent red shade, we agree that the red pixel would be a full red RGB weight of:

    255 - 00 - 00

    But what if the green pixel wasn't exactly of a pure green. Say it was:

    01 - 128 - 01

    What would its red color equivalent be???

    All helpful insigth is appreciated.
    rob

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't know that there is any exact "equivalent color", to any color, in another color. Maybe what you want to do is to swap the green and the red values, in your example, and leave the blue value alone.

    Your question is like asking what even number is equivalent to 3? It just doesn't have an answer.

  3. #3
    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
    Hello,

    If we have a full green pixel with the RGB weight of:

    00 - 128 - 00

    and we want to convert it to its equivalent red shade, we agree that the red pixel would be a full red RGB weight of:

    255 - 00 - 00

    But what if the green pixel wasn't exactly of a pure green. Say it was:

    01 - 128 - 01

    What would its red color equivalent be???

    All helpful insigth is appreciated.
    rob
    Who is "we"? In my opinion, the equivalent shade of red would be 128 - 00 - 00 because 50% green would instead become 50% red.

    As for your question, it depends on what your actual colour transformation is supposed to be. Is it supposed to simply swap the red and green, or rotate green -> red -> blue, or something else?
    You need to explain more about what you actually want.
    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"

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    53
    hello iMalc,

    >Who is "we"? In my opinion, the equivalent shade of red would be 128 - 00 - 00 because 50% >green would instead become 50% red.

    Impossible... it can't be that simple. Lets reason this a little. If we want to recolor a pixel from green to red. We are actually saying that we want a value that equates the real raw green color *shade* to the real raw red color *shade*. So:

    Real raw green to real raw red is:

    00-128-00 to 255-00-00

    which is the real color substitution. If you want to recolor a green pixel to a red one and you do this:

    00-128-00 to 128-00-00

    then 128-00-00 is not the real raw genuine red shade? Its just a darker red! Which in visual contrast is wrong.

    What I am trying to do, is recolor an image. Suppose we have a greenish square with multiple shades of green in it and I want to recolor this square to a reddish color. So if a green pixel exists in the green square I want it to be red in the red square. If a lighter green pixel exists in the green square I want a proportionally lighter red pixle in the red square.

    Anyways, I copied a green square from MS Paint to MS publisher and then converted it in MSPublisher and gave me the following results... Now I am really confused!!!

    Paint green: 00-128-00
    Converted to red in MSPublisgher : 255-92-92
    ????

    rob

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why is "real raw green" 128? Why isn't "real raw green" 255?

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    53
    Because according to any bitmap editor:

    00-255-00

    is expressed as "Lime" and not green ????

    R

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    If I take looots of snow, as much as I can, it's not snow anymore.

    If I take looots of green, as much as I can, it's lime, not green.

    EDIT: And by the way, at least all the limes I've seen are green.
    Last edited by fronty; 12-23-2010 at 11:29 AM.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If we have a full green pixel with the RGB weight of:

    00 - 128 - 00
    I wouldn't say that's full green. Sure, there is only green, but it's at half intensity. Logically, half green should map to half red.

    Because according to any bitmap editor:

    00-255-00

    is expressed as "Lime" and not green ????
    That's just a name for that shade of green, probably given by some programmer. Regardless of the names given to those RGB values, they are both pure greens, just of a different shades. There is no red or blue in either of those colors.

    For colors composed of more than one of R, G or B values, you could always try converting it to an HSL/HSV model and rotating your color by some number of degrees (+/- 120 probably). You can then convert it back if you still need RGB values.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by see the big C View Post
    Hello,

    If we have a full green pixel with the RGB weight of:

    00 - 128 - 00

    and we want to convert it to its equivalent red shade, we agree that the red pixel would be a full red RGB weight of:

    255 - 00 - 00

    But what if the green pixel wasn't exactly of a pure green. Say it was:

    01 - 128 - 01

    What would its red color equivalent be???

    All helpful insigth is appreciated.
    rob
    RGB is not the best color space for those types of conversions. I'd recommend using something like HSV, personally - selecting "similar" colors is almost trivial...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    53
    >If I take looots of green, as much as I can, it's lime, not green.

    I know what you are saying .. its just find it weird that if I have a greenish color stop sign and I want recolor it reddish, I get it marooninsh!!!!

    r

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by see the big C View Post
    its just find it weird that if I have a greenish color stop sign and I want recolor it reddish, I get it marooninsh!!!!
    That's probably because the human eye is far more sensitive to green colors than red (or any other wavelength of light). Green light (around 550nm) is close to the peak radiation that our sun puts off, which is probably why we evolved to see green best, and why plants are green, etc. Red and blue lie on the outer edges of our visual spectrum, and we can not distinguish shades of red or blue as well as we can green. I had a set of color gradients that showed this very well, but they were from a class a decade ago...

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by see the big C View Post
    >If I take looots of green, as much as I can, it's lime, not green.

    I know what you are saying .. its just find it weird that if I have a greenish color stop sign and I want recolor it reddish, I get it marooninsh!!!!

    r
    Well, RGB was designed for display monitors; it's relationship to our perception of color is a rather unnatural one!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    Registered User
    Join Date
    Jun 2010
    Posts
    53
    anduril462 ,

    I suppose so.

    thanks all for your insights.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Okay sure, you're interested in maintaining percieved brighteness. One source I knew of on wikipedia, has the following for a function of perceived brightness:
    30% of the red value, 59% of the green value, and 11% of the blue value
    You're right that this puts red at close to half the perceived brightness of green.

    However, there are still things to consider. How light of a green would you still want to convert to red? For example, would you want to convert 254 - 255 - 254 to something like 255 - 254 - 254 ?
    Note that I can't yet tell what you want to do with the existing red channel. For the conversions any of us have thought of so far, it looks like swapping (and scaling) the red and green components would work.
    A formula comes to mind:
    Code:
    NewRed = max(OldGreen * 59 / 30, 255);
    NewGreen = OldRed * 30 / 59;
    Now, how well does this do for you? What does it do that is not desireable such that we can tweak it further (or replace it)?
    Byt the way, what were you wanting to do with pixels that were already reddish?
    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"

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by anduril462 View Post
    That's probably because the human eye is far more sensitive to green colors than red (or any other wavelength of light). Green light (around 550nm) is close to the peak radiation that our sun puts off, which is probably why we evolved to see green best, and why plants are green, etc.
    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?

    EDIT: The OP can probably accomplish his goal by converting from RGB to HSV, selecting pixels with H in a "close enough" range of green, and rotate the H coordinate into the red region, then convert back to RGB.

    Note that RGB is a really, really crappy color space to be working in. It's device-dependent and has no standard definition.
    Last edited by brewbuck; 12-23-2010 at 01:44 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

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