Color Keying

Source and destination color keying for blits and overlays are supported by DirectDraw. You can supply a color key or a color range for both of these types of color keying.

Source color keying specifies a color or color range that, in the case of blitting, is not copied, or, in the case of overlays, is not visible on the destination. Destination color keying specifies a color or color range that, in the case of blitting, is replaced or, in the case of overlays, is covered up on the destination. The source color key specifies what can and cannot be read from the surface. The destination color key specifies what can and cannot be written onto, or covered up, on the destination surface. If a destination surface has a color key, only the pixels that match the color key are be changed, or covered up, on the destination surface.

Some hardware supports color ranges only for YUV pixel data. YUV data is usually video, and the transparent background may not be a single color due to quantitization errors during conversion. Content should be written to a single transparent color whenever possible, regardless of pixel format.

Color keys are specified in the pixel format of a surface. If a surface is in a palettized format, the color key is specified as an index or a range of indices. If the surface's pixel format is specified by a FOURCC code that describes a YUV format, the YUV color key is specified by the three low-order bytes in both the dwColorSpaceLowValue and dwColorSpaceHighValue members of the DDCOLORKEY structure. The lowest order byte contains the V data, the second lowest order byte contains the U data, and the highest order byte contains the Y data. The dwFlags parameter of the IDirectDrawSurface2::SetColorKey method specifies whether the color key is to be used for overlay or blit operations, and whether it is a source or a destination key. Some examples of valid color keys follow:

8-bit palettized mode

// Palette entry 26 is the color key.

dwColorSpaceLowValue = 26;

dwColorSpaceHighValue = 26;

24-bit true-color mode

// Color 255,128,128 is the color key.

dwColorSpaceLowValue = RGBQUAD(255,128,128);

dwColorSpaceHighValue = RGBQUAD(255,128,128);

FourCC YUV mode

// Any YUV color where Y is between 100 and 110

// and U or V is between 50 and 55 is transparent.

dwColorSpaceLowValue = YUVQUAD(100,50,50);

dwColorSpaceHighValue = YUVQUAD(110,55,55);