Tuesday, March 3, 2009

Pecularities in Microsoft's RGB format

If you only worked in Microsoft platform, you are familiar with RGB color routines provided by Windows GDI. A COLORREF stores RGB value, and it is a 32-bit integer. Macro RGB is used to combine the three components into a RGB value. In order to get individual component values, you are provided GetRValue, GetGValue and GetBValue functions.

You may take granted that RGB value is stored as follows:

struct RGBValue
{
char reserved;
char RValue;
char GValue;
char BVaue;
};

and write code as below to print a color value:

printf("#%06x", color);

Until you are surprised to find that a red color is shown as blue on the screen.

Reason: Microsoft choosed to store R value at the last byte.