4 * Copyright 1993 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
14 /***********************************************************************
17 * First 10 and last 10 colors in COLOR_sysPalette are
18 * "guarded". RealizePalette changes only the rest of colorcells. For
19 * currently inactive window it changes only DC palette mappings.
22 PALETTEENTRY *COLOR_sysPal = NULL; /* current system palette */
24 int COLOR_gapStart = 256;
25 int COLOR_gapEnd = -1;
26 int COLOR_gapFilled = 0;
29 const PALETTEENTRY COLOR_sysPalTemplate[NB_RESERVED_COLORS] =
31 /* first 10 entries in the system palette */
32 /* red green blue flags */
33 { 0x00, 0x00, 0x00, PC_SYS_USED },
34 { 0x80, 0x00, 0x00, PC_SYS_USED },
35 { 0x00, 0x80, 0x00, PC_SYS_USED },
36 { 0x80, 0x80, 0x00, PC_SYS_USED },
37 { 0x00, 0x00, 0x80, PC_SYS_USED },
38 { 0x80, 0x00, 0x80, PC_SYS_USED },
39 { 0x00, 0x80, 0x80, PC_SYS_USED },
40 { 0xc0, 0xc0, 0xc0, PC_SYS_USED },
41 { 0xc0, 0xdc, 0xc0, PC_SYS_USED },
42 { 0xa6, 0xca, 0xf0, PC_SYS_USED },
44 /* ... c_min/2 dynamic colorcells */
46 /* ... gap (for sparse palettes) */
48 /* ... c_min/2 dynamic colorcells */
50 { 0xff, 0xfb, 0xf0, PC_SYS_USED },
51 { 0xa0, 0xa0, 0xa4, PC_SYS_USED },
52 { 0x80, 0x80, 0x80, PC_SYS_USED },
53 { 0xff, 0x00, 0x00, PC_SYS_USED },
54 { 0x00, 0xff, 0x00, PC_SYS_USED },
55 { 0xff, 0xff, 0x00, PC_SYS_USED },
56 { 0x00, 0x00, 0xff, PC_SYS_USED },
57 { 0xff, 0x00, 0xff, PC_SYS_USED },
58 { 0x00, 0xff, 0xff, PC_SYS_USED },
59 { 0xff, 0xff, 0xff, PC_SYS_USED } /* last 10 */
62 /***********************************************************************
63 * COLOR_GetSystemPaletteTemplate
65 const PALETTEENTRY* COLOR_GetSystemPaletteTemplate(void)
67 return COLOR_sysPalTemplate;
70 /***********************************************************************
71 * COLOR_GetSystemPaletteEntry
74 COLORREF COLOR_GetSystemPaletteEntry(UINT i)
76 return *(COLORREF*)(COLOR_sysPal + i) & 0x00ffffff;
79 /***********************************************************************
82 * Check whether 'color' can be represented with a solid color.
84 BOOL COLOR_IsSolid( COLORREF color )
87 const PALETTEENTRY *pEntry = COLOR_sysPal;
89 if (color & 0xff000000) return TRUE; /* indexed color */
91 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
93 for (i = 0; i < 256 ; i++, pEntry++)
95 if( i < COLOR_gapStart || i > COLOR_gapEnd )
96 if ((GetRValue(color) == pEntry->peRed) &&
97 (GetGValue(color) == pEntry->peGreen) &&
98 (GetBValue(color) == pEntry->peBlue)) return TRUE;
103 /***********************************************************************
104 * COLOR_PaletteLookupPixel
106 int COLOR_PaletteLookupPixel( PALETTEENTRY* palPalEntry, int size,
107 int* mapping, COLORREF col, BOOL skipReserved )
109 int i, best = 0, diff = 0x7fffffff;
112 for( i = 0; i < size && diff ; i++ )
114 if( !(palPalEntry[i].peFlags & PC_SYS_USED) ||
115 (skipReserved && palPalEntry[i].peFlags & PC_SYS_RESERVED) )
118 r = palPalEntry[i].peRed - GetRValue(col);
119 g = palPalEntry[i].peGreen - GetGValue(col);
120 b = palPalEntry[i].peBlue - GetBValue(col);
124 if( r < diff ) { best = i; diff = r; }
126 return (mapping) ? mapping[best] : best;
129 /***********************************************************************
130 * COLOR_PaletteLookupExactIndex
132 int COLOR_PaletteLookupExactIndex( PALETTEENTRY* palPalEntry, int size,
136 BYTE r = GetRValue(col), g = GetGValue(col), b = GetBValue(col);
137 for( i = 0; i < size; i++ )
139 if( palPalEntry[i].peFlags & PC_SYS_USED ) /* skips gap */
140 if( palPalEntry[i].peRed == r &&
141 palPalEntry[i].peGreen == g &&
142 palPalEntry[i].peBlue == b )
148 /***********************************************************************
149 * COLOR_LookupNearestColor
151 COLORREF COLOR_LookupNearestColor( PALETTEENTRY* palPalEntry, int size, COLORREF color )
153 unsigned char spec_type = color >> 24;
156 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
158 if( spec_type == 2 ) /* PALETTERGB */
160 (palPalEntry + COLOR_PaletteLookupPixel(palPalEntry,size,NULL,color,FALSE));
162 else if( spec_type == 1 ) /* PALETTEINDEX */
164 if( (i = color & 0x0000ffff) >= size )
166 WARN(palette, "RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, i);
167 color = *(COLORREF*)palPalEntry;
169 else color = *(COLORREF*)(palPalEntry + i);
173 return (0x00ffffff & *(COLORREF*)
174 (COLOR_sysPal + COLOR_PaletteLookupPixel(COLOR_sysPal, 256, NULL, color, FALSE)));