Platform SDK: DirectX

Matching True RGB Colors to the Frame Buffer's Color Space

[Visual Basic]

The information in this topic pertains only to applications written in C++.

[C++]

Applications often need to find out how a true RGB color (RGB 888) will be mapped into a frame buffer's color space when the display device is not in RGB 888 mode. For example, imagine you're working on an application that will run in 16- and 24-bit RGB display modes. You know that when the art was created, a color was reserved for use as a transparent blitting color key; for the sake of argument, it is a 24-bit color such as RGB(128,64,255). Because your application will also run in a 16-bit RGB mode, you need a way to find out how this 24-bit color key maps into the color space that the frame buffer uses when it's running in a 16-bit RGB mode.

Although DirectDraw does not perform color matching services for you, there are ways to calculate how your color key will be mapped in the frame buffer. These methods can be pretty complicated. For most purposes, you can use the GDI built-in color matching services, combined with the DirectDraw direct frame buffer access, to determine how a color value maps into a different color space. In fact, the Ddutil.cpp source file included in the DirectX examples of the Platform SDK includes a sample function called DDColorMatch that performs this task. The DDColorMatch sample function performs the following main tasks.

  1. Retrieves the color value of a pixel in a surface at 0,0.
  2. Calls the Win32 SetPixel function, using a COLORREF structure that describes your 24-bit RGB color.
  3. Uses DirectDraw to lock the surface, getting a pointer to the frame buffer memory.
  4. Retrieves the actual color value from the frame buffer (set by GDI in Step 2) and unlocks the surface
  5. Resets the pixel at 0,0 to its original color using SetPixel.

The process used by the DDColorMatch sample function is not fast; it isn't intended to be. However, it provides a reliable way to determine how a color will be mapped across different RGB color spaces. For more information, see the source code for DDColorMatch in the Ddutil.cpp source file.

Note  Because the SetPixel GDI function only accepts a COLORREF structure on input, this technique only works for matching RGB 888 colors to the frame buffer's pixel format. If your application needs to match colors of another pixel format, you should translate them to RGB 888 before using this technique or query the primary surface for its pixel format and match colors manually.