简体   繁体   中英

Why does windows GDI use RGBA format for `COLORREF` instead of BGRA?

MSDN states :

When specifying an explicit RGB color, the COLORREF value has the following hexadecimal form:

0x00bbggrr

The low-order byte contains a value for the relative intensity of red; the second byte contains a value for green; and the third byte contains a value for blue. The high-order byte must be zero. The maximum value for a single byte is 0xFF.

From wingdi.h

#define RGB(r,g,b)          ((COLORREF)((BYTE)(r) | ((BYTE)(g) << 8) | ((BYTE)(b) << 16)))

#define GetRValue(rgb)      ((BYTE)  (rgb) )
#define GetGValue(rgb)      ((BYTE) ((rgb) >> 8))
#define GetBValue(rgb)      ((BYTE) ((rgb) >> 16))

As windows is little endian, COLORREF is in RGBA format. This looks strange because isn't the color format that Windows use internally, BGR(A)?

The RGBQUAD structure is defined as

typedef struct tagRGBQUAD {
  BYTE rgbBlue;
  BYTE rgbGreen;
  BYTE rgbRed;
  BYTE rgbReserved;
} RGBQUAD;

which is, unlike COLORREF , BGRA.

Since the bitblt function expects an array of COLORREF values, this means that there is always an additional conversion going on from RGBA to BGRA during every call, if Windows use BGRA as its native format.

I don't remember correctly, but I also read somewhere that there is a strange mix in the pixel format used in the winapi.

Can someone please explain?

COLORREFs date way back to when there was much less standardization in pixel formats. Many graphics adapters were still using palettes rather than full 24- or 32-bit color, so even if your adapter required a byte re-order, you didn't need to do very many of them. Some graphics adapters even stored images in separate color planes rather than a single plane of multi-channel colors. There was no "right" answer back then.

RGBQUADs came from the BMP format, which as Raymond Chen mentioned in the comments, comes from the OS/2 bitmap format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM