In the article Objectwindows programming - Coloring controls in OWL applications we mention that when you request
a color, you don't always get the exact color you ask for.
If you specify an RGB value when you create a TColor
object, Windows will match the color you request to the nearest
color in the extended system palette. The color might be what
you expector it might not even be close. There is a way,
however, to get exactly the color you need: You can create a logical
palette that's always available for your use. Let's
review the steps required to do this.
You first fill in a PALETTEENTRY structure by specifying
the red, green and blue values for each color you require. You'll
usually do this by creating an array of PALETTEENTRY
structures. Then, you create a TPalette object using
the PALETTEENTRY array.
Once you create a logical palette, you need to implement it. To
do so, select the palette into your device context with TDC::SelectObject().
Next, use the TDC function RealizePalette()
to map the logical palette to the extended system palette.
Finally, you need to select a color from the palette. You can
do this in one of two ways. Your first option is to create a TColor
object by specifying the color index from the palette. For instance,
if you know that you want color number 8 in your logical palette,
you can create a TColor object as follows:
Now myColor contains the eighth color value in your logical
palette.
You can also specify an RGB component, but doing so requires an
extra step. If you use
Windows will find the closest match in the system palettenot
in your logical palette. To specify the closest match in your
logical palette, you must use TColor::PalRelative().
For example, you might use
Now Windows will search the extended system palette (to which
you've mapped your logical palette) for the closest match
to the RGB color you requested, rather than searching the system
palette.
The code snippet shown in Figure A
creates a logical palette of 16 shades of blue and fills the screen
with those colors in a gradient effect. By creating a palette
and mapping it to the extended system palette, you can be sure
of the colors your applications display.
TColor myColor(8);
TColor(128, 80, 190);
TColor myColor(128, 80, 190).PalRelative();
Figure A - This code creates a logical palette filled with 16 shades of blue.
PALETTEENTRY pe[16];
memset(&pe, 0, sizeof(pe));
int i;
for (i=0;i<16;i++) pe[i].peBlue = i * 16;
pal = new TPalette(pe, 16);
TClientDC dc(*this);
dc.SelectObject(*pal);
dc.RealizePalette();
int step = GetWindowRect().Height()/16;
for (i=0;i<16;i++) {
TColor color(i);
dc.SelectObject(TPen(color));
TRect rect(0, i*step, GetWindowRect().Width(),
(i*step) + step);
dc.FillRect(rect, TBrush(color));
}
Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.