Color Keying

DirectDraw supports source and destination color keying for blits and overlay surfaces. You can supply a color key or a color range for both of these types of color keying. For general information about color keying, see Transparent Blitting and Color Keys. You set a surface's color key by calling the its IDirectDrawSurface3::SetColorKey method.

When blitting, source color keying specifies a color or color range that is not copied. Likewise, destination color keying specifies a color or color range that is replaced 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 changed, or covered up, on the destination surface.

In addition to blit-related color keys, overlay surfaces can use overlay color keys. For more information, see Overlay Color Keys.

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 using 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 IDirectDrawSurface3::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);