The following example initializes a
ColorPalette structure with four colors: aqua, black, red, and green. The code also creates an
ImageAttributes object and sets its bitmap remap table so that green will be converted to blue. Then the code adjusts the palette colors by passing the address of the palette to the
GdipGetAdjustedPalette function. The code displays the four palette colors twice: once before the adjustment and once after the adjustment.
C++
VOID Example_GetAdjustedPalette(HDC hdc)
{
Graphics graphics(hdc);
INT j;
// Create a palette that has four entries.
ColorPalette* palette =
(ColorPalette*)malloc(sizeof(ColorPalette) + 3 * sizeof(ARGB));
palette->Flags = 0;
palette->Count = 4;
palette->Entries[0] = 0xFF00FFFF; // aqua
palette->Entries[1] = 0xFF000000; // black
palette->Entries[2] = 0xFFFF0000; // red
palette->Entries[3] = 0xFF00FF00; // green
// Display the four palette colors with no adjustment.
SolidBrush brush(Color());
for(j = 0; j < 4; ++j)
{
brush.SetColor(palette->Entries[j]);
graphics.FillRectangle(&brush, 30*j, 0, 20, 20);
}
// Create a remap table that converts green to blue.
ColorMap map;
map.oldColor = Color(255, 0, 255, 0); // green
map.newColor = Color(255, 0, 0, 255); // blue
// Create an ImageAttributes object, and set its bitmap remap table.
ImageAttributes imAtt;
imAtt.SetRemapTable(1, &map, ColorAdjustTypeBitmap);
// Adjust the palette.
imAtt.GetAdjustedPalette(palette, ColorAdjustTypeBitmap);
// Display the four palette colors after the adjustment.
for(j = 0; j < 4; ++j)
{
brush.SetColor(palette->Entries[j]);
graphics.FillRectangle(&brush, 30*j, 30, 20, 20);
}
}
PowerBASIC
SUB GDIP_GetAdjustedPalette (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pBrush AS DWORD
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Create a palette that has four entries.
LOCAL palette AS COLORPALETTE
palette.Flags = 0
palette.Count = 4
palette.Entries(0) = &HFF00FFFF ' // aqua
palette.Entries(1) = &HFF000000 ' // black
palette.Entries(2) = &HFFFF0000 ' // red
palette.Entries(3) = &HFF00FF00 ' // green
' // Create a SolidBrush
hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 0, 0, 0), pBrush)
' // Display the four palette colors with no adjustment.
LOCAL j AS LONG
FOR j = 0 TO 3
hStatus = GdipSetSolidFillColor(pBrush, palette.Entries(j))
hStatus = GdipFillRectangle (pGraphics, pBrush, 30*j, 0, 20, 20)
NEXT
' // Create a remap table that converts green to blue.
LOCAL map AS GDIP_ColorMap
map.oldColor = GDIP_ARGB(255, 0, 255, 0) ' // green
map.newColor = GDIP_ARGB(255, 0, 0, 255) ' // blue
' // Create an ImageAttributes object, and set its bitmap remap table.
LOCAL pImgAttr AS DWORD
hStatus = GdipCreateImageAttributes(pImgAttr)
hStatus = GdipSetImageAttributesRemapTable(pImgAttr, _
%ColorAdjustTypeBitmap, %TRUE, 1, map)
' // Adjust the palette.
hStatus = GdipGetImageAttributesAdjustedPalette(pImgAttr, palette, %ColorAdjustTypeBitmap)
' // Display the four palette colors after the adjustment.
FOR j = 0 TO 3
hStatus = GdipSetSolidFillColor(pBrush, palette.Entries(j))
hStatus = GdipFillRectangle (pGraphics, pBrush, 30*j, 30, 20, 20)
NEXT
' // Cleanup
IF pImgAttr THEN GdipDisposeImageAttributes(pImgAttr)
IF pBrush THEN GdipDeleteBrush(pBrush)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
The following illustration shows the output of the preceding code. Note that the green in the original palette was changed to blue.
(http://www.jose.it-berater.org/captures/GdipGetImageAttributesAdjustedPalette.png)