# Thread: three into one variable does go?

1. ## 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. 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. 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. 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).

5. 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. Code:
```union rgbquad
{
struct
{
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
} rgba;
};

...

uint32 foo( unsigned char r, unsigned char g, unsigned char b, unsigned char a = 0 )
{
bar.rgba.r = r;
bar.rgba.g = g;
bar.rgba.b = b;
bar.rgba.a = a;

}```
There you go. Swap out 'uint32' for some unisgned 32 bit integer type of your choosing.

Quzah.