HPALETTE CreateIdentityPalette(RGBQUAD aRGB[], int nColors)

{

int i;

struct {

WORD Version;

WORD NumberOfEntries;

PALETTEENTRY aEntries[256];

} Palette =

{

0x300,

256

};

//*** Just use the screen DC where we need it

HDC hdc = GetDC(NULL);

//*** For SYSPAL_NOSTATIC, just copy the color table into

//*** a PALETTEENTRY array and replace the first and last entries

//*** with black and white

if (GetSystemPaletteUse(hdc) == SYSPAL_NOSTATIC)

{

//*** Fill in the palette with the given values, marking each

//*** as PC_NOCOLLAPSE

for(i = 0; i < nColors; i++)

{

Palette.aEntries[i].peRed = aRGB[i].rgbRed;

Palette.aEntries[i].peGreen = aRGB[i].rgbGreen;

Palette.aEntries[i].peBlue = aRGB[i].rgbBlue;

Palette.aEntries[i].peFlags = PC_NOCOLLAPSE;

}

//*** Mark any unused entries PC_NOCOLLAPSE

for (; i < 256; ++i)

{

Palette.aEntries[i].peFlags = PC_NOCOLLAPSE;

}

//*** Make sure the last entry is white

//*** This may replace an entry in the array!

Palette.aEntries[255].peRed = 255;

Palette.aEntries[255].peGreen = 255;

Palette.aEntries[255].peBlue = 255;

Palette.aEntries[255].peFlags = 0;

//*** And the first is black

//*** This may replace an entry in the array!

Palette.aEntries[0].peRed = 0;

Palette.aEntries[0].peGreen = 0;

Palette.aEntries[0].peBlue = 0;

Palette.aEntries[0].peFlags = 0;

}

else

//*** For SYSPAL_STATIC, get the twenty static colors into

//*** the array, then fill in the empty spaces with the

//*** given color table

{

int nStaticColors;

int nUsableColors;

//*** Get the static colors from the system palette

nStaticColors = GetDeviceCaps(hdc, NUMCOLORS);

GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);

//*** Set the peFlags of the lower static colors to zero

nStaticColors = nStaticColors / 2;

for (i=0; i<nStaticColors; i++)

Palette.aEntries[i].peFlags = 0;

//*** Fill in the entries from the given color table

nUsableColors = nColors - nStaticColors;

for (; i<nUsableColors; i++)

{

Palette.aEntries[i].peRed = aRGB[i].rgbRed;

Palette.aEntries[i].peGreen = aRGB[i].rgbGreen;

Palette.aEntries[i].peBlue = aRGB[i].rgbBlue;

Palette.aEntries[i].peFlags = PC_NOCOLLAPSE;

}

//*** Mark any empty entries as PC_NOCOLLAPSE

for (; i<256 - nStaticColors; i++)

Palette.aEntries[i].peFlags = PC_NOCOLLAPSE;

//*** Set the peFlags of the upper static colors to zero

for (i = 256 - nStaticColors; i<256; i++)

Palette.aEntries[i].peFlags = 0;

}

//*** Remember to release the DC!

ReleaseDC(NULL, hdc);

//*** Return the palette

return CreatePalette((LOGPALETTE *)&Palette);

}