three into one variable does go?

This is a discussion on three into one variable does go? within the C++ Programming forums, part of the General Programming Boards category; in the example below i can't understand how the variable 'color' can apparently store the indivdual RGB values, in the ...

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425

    three into one variable does go?

    in the example below i can't understand how the variable 'color' can apparently store the indivdual RGB values, in the header file it describes this function as
    Maps an RGB triple to an opaque pixel value for a given pixel format
    could anybody please enlighten me on this?


    and the Uint32, is that some kind of SDL variable type or just an alternative way of writing unsigned 32bit integer? i mean would simply 'int color' have worked here too?

    Code:
    SDL_Rect rect;
        
    Uint32 color;
    
        color = SDL_MapRGB (screen->format, 50, 50, 50);
    
        SDL_FillRect (screen, NULL, color);

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    actually i think this makes sense really, the call carries the R G B values into the function which then returns them as a single value that can be referenced as a colour. understanding how this works is another matter

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,705
    Well, since each RGB value here is constrained within the range 0-255, it fits perfectly into a single byte. So obviously, you can pack all 3 of them into a 32-bit integer, with 1 byte to spare (often this extra one is used to carry the 'alpha' (opacity) channel).

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    This is commonly shown when converting RGB to hex (binary to operate per byte):

    Code:
    rgb = (a << 24) 
            + (r << 16) 
            + (g << 8) 
            + b;
    
    ...
    
    rgb = ((r & 0xFF) << 16)
            | ((g & 0xFF) << 8)
            | (b & 0xFF);
    Code:
    a = (color & 0xFF000000) >> 24;
    r = (color & 0x00FF0000) >> 16;
    g = (color & 0x0000FF00) >> 8;
    b = (color & 0x000000FF);
    
    ...
    
    r = (color & 0xFF0000) >> 16;
    g = (color & 0x00FF00) >> 8;
    b = (color & 0x0000FF);
    Something like that, I guess. I took it from an AS project. RGBA would include the last byte (24 bits into a 32 bit byte).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,705
    Well, the important thing to remember is that the color data in compact format should be treated strictly as an 'opaque' value. The implementation could choose to change the format at any point in the future, for whatever reason, and moreover, the internal representation might actually map to a completely different color model (a pallete index, for instance), altogether. So the safest way to approach it is to simply consider it a handle to some abstract color type.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    union rgbquad
    {
        struct
        {
            unsigned char r;
            unsigned char g;
            unsigned char b;
            unsigned char a;
        } rgba;
        uint32 quad;
    };
    
    ...
    
    uint32 foo( unsigned char r, unsigned char g, unsigned char b, unsigned char a = 0 )
    {
        union rgbquad bar;
        bar.rgba.r = r;
        bar.rgba.g = g;
        bar.rgba.b = b;
        bar.rgba.a = a;
    
        return bar.quad;
    }
    There you go. Swap out 'uint32' for some unisgned 32 bit integer type of your choosing.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    thanks for the comments.....
    simply consider it a handle to some abstract color type.
    Does this mean like in the old sense of enumerated type colours, like Magenta, = 9, high intensity white = 7 etc?

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,705
    Quote Originally Posted by rogster001 View Post
    simply consider it a handle to some abstract color type.
    Does this mean like in the old sense of enumerated type colours, like Magenta, = 9, high intensity white = 7 etc?
    Or rather like a pointer to some implementation-defined type. In other words, it may very well be simply 3 bytes packed onto an integer, but then again, it could also be an index into a pallette entry, or even a pointer to some complex object. RGB isn't the only way to represent colors, after all. The internal system may choose to use floats for better precision, or perhaps the HSV color space, or whatever. Besides that, different systems have their own capabilities/limitations. Some displays are monochrome, for instance, so RGB might be meaningless in that context. So the bottom line is, by separating the 'handle' from the RGB has more uses than just a handy place to stuff three bytes into. It makes portability more straightforward.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21