4 * Copyright 1993,1994 Alexandre Julliard
14 /* #define DEBUG_PALETTE */
17 static WORD SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
20 /***********************************************************************
21 * PALETTE_GetNearestIndexAndColor
23 static WORD PALETTE_GetNearestIndexAndColor(HPALETTE16 hpalette, COLORREF *color)
31 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
32 if (!palPtr) return 0;
34 if ((COLOR_WinColormap != DefaultColormapOfScreen(screen)) &&
35 (hpalette == STOCK_DEFAULT_PALETTE))
37 if ((*color & 0xffffff) == 0) return 0; /* Entry 0 is black */
38 if ((*color & 0xffffff) == 0xffffff) /* Max entry is white */
39 return palPtr->logpalette.palNumEntries - 1;
42 r = GetRValue(*color);
43 g = GetGValue(*color);
44 b = GetBValue(*color);
46 entry = palPtr->logpalette.palPalEntry;
47 for (i = 0, minDist = 0xffffff; minDist !=0 &&
48 i < palPtr->logpalette.palNumEntries ; i++)
50 if (entry->peFlags != 0xff)
52 dist = (r - entry->peRed) * (r - entry->peRed) +
53 (g - entry->peGreen) * (g - entry->peGreen) +
54 (b - entry->peBlue) * (b - entry->peBlue);
63 entry = &palPtr->logpalette.palPalEntry[index];
64 *color = RGB( entry->peRed, entry->peGreen, entry->peBlue );
69 /***********************************************************************
70 * CreatePalette (GDI.360)
72 HPALETTE16 CreatePalette( const LOGPALETTE* palette )
74 PALETTEOBJ * palettePtr;
78 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
79 hpalette = GDI_AllocObject( sizeof(GDIOBJHDR) + size, PALETTE_MAGIC );
80 if (!hpalette) return 0;
81 palettePtr = (PALETTEOBJ *) GDI_HEAP_LIN_ADDR( hpalette );
82 memcpy( &palettePtr->logpalette, palette, size );
87 /***********************************************************************
88 * GetPaletteEntries (GDI.363)
90 WORD GetPaletteEntries( HPALETTE16 hpalette, WORD start, WORD count,
91 LPPALETTEENTRY entries )
96 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
97 if (!palPtr) return 0;
98 numEntries = palPtr->logpalette.palNumEntries;
99 if (start >= numEntries) return 0;
100 if (start+count > numEntries) count = numEntries - start;
101 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
102 count * sizeof(PALETTEENTRY) );
107 /***********************************************************************
108 * SetPaletteEntries (GDI.364)
110 WORD SetPaletteEntries( HPALETTE16 hpalette, WORD start, WORD count,
111 LPPALETTEENTRY entries )
116 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
117 if (!palPtr) return 0;
118 numEntries = palPtr->logpalette.palNumEntries;
119 if (start >= numEntries) return 0;
120 if (start+count > numEntries) count = numEntries - start;
121 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
122 count * sizeof(PALETTEENTRY) );
126 /***********************************************************************
127 * ResizePalette (GDI.368)
129 BOOL ResizePalette(HPALETTE16 hPal, UINT cEntries)
131 fprintf(stdnimp,"ResizePalette: empty stub! \n");
135 /***********************************************************************
136 * AnimatePalette (GDI.367)
138 BOOL AnimatePalette(HPALETTE16 hPal, UINT StartIndex, UINT NumEntries,
139 LPPALETTEENTRY PaletteColors)
141 fprintf(stdnimp,"AnimatePalette: empty stub! \n");
145 /***********************************************************************
146 * SetSystemPaletteUse (GDI.373)
147 * Should this be per DC rather than system wide?
148 * Currently, it does not matter as the use is only set and returned,
149 * but not taken into account
151 WORD SetSystemPaletteUse( HDC hdc, WORD use)
153 WORD old=SystemPaletteUse;
154 printf("SetSystemPaletteUse(%04x,%04x) // empty stub !!!\n", hdc, use);
155 SystemPaletteUse=use;
159 /***********************************************************************
160 * GetSystemPaletteUse (GDI.374)
162 WORD GetSystemPaletteUse( HDC hdc )
164 printf("GetSystemPaletteUse(%04x) // empty stub !!!\n", hdc);
165 return SystemPaletteUse;
169 /***********************************************************************
170 * GetSystemPaletteEntries (GDI.375)
172 WORD GetSystemPaletteEntries( HDC hdc, WORD start, WORD count,
173 LPPALETTEENTRY entries )
179 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
180 if (start >= dc->w.devCaps->sizePalette) return 0;
181 if (start+count >= dc->w.devCaps->sizePalette)
182 count = dc->w.devCaps->sizePalette - start;
183 for (i = 0; i < count; i++)
185 color.pixel = start + i;
186 XQueryColor( display, COLOR_WinColormap, &color );
187 entries[i].peRed = color.red >> 8;
188 entries[i].peGreen = color.green >> 8;
189 entries[i].peBlue = color.blue >> 8;
190 entries[i].peFlags = 0;
196 /***********************************************************************
197 * GetNearestPaletteIndex (GDI.370)
199 WORD GetNearestPaletteIndex( HPALETTE16 hpalette, COLORREF color )
201 WORD index = PALETTE_GetNearestIndexAndColor( hpalette, &color );
202 dprintf_palette(stddeb,"GetNearestPaletteIndex(%04x,%06lx): returning %d\n",
203 hpalette, color, index );
208 /***********************************************************************
209 * GetNearestColor (GDI.154)
211 COLORREF GetNearestColor( HDC hdc, COLORREF color )
213 COLORREF nearest = color;
216 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
217 PALETTE_GetNearestIndexAndColor( dc->w.hPalette, &nearest );
218 dprintf_palette(stddeb,"GetNearestColor(%06lx): returning %06lx\n",
224 /***********************************************************************
227 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
229 if (count > sizeof(WORD)) count = sizeof(WORD);
230 memcpy( buffer, &palette->logpalette.palNumEntries, count );
235 /***********************************************************************
236 * GDISelectPalette (GDI.361)
238 HPALETTE16 GDISelectPalette( HDC hdc, HPALETTE16 hpal )
243 dprintf_palette(stddeb, "GDISelectPalette: %04x %04x\n", hdc, hpal );
244 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
245 prev = dc->w.hPalette;
246 dc->w.hPalette = hpal;
247 if (hpal != STOCK_DEFAULT_PALETTE) COLOR_SetMapping( dc, 0, 0, 0 );
248 else RealizeDefaultPalette( hdc ); /* Always realize default palette */
253 /***********************************************************************
254 * GDIRealizePalette (GDI.362)
256 UINT GDIRealizePalette( HDC hdc )
260 DC* dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ) ;
263 dprintf_palette(stdnimp, "GDIRealizePalette: %04x...", hdc );
267 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
275 hMap = GDI_HEAP_ALLOC(sizeof(WORD)*palPtr->logpalette.palNumEntries);
276 pMap = (WORD*)GDI_HEAP_LIN_ADDR( hMap );
280 for (i = 0; i < palPtr->logpalette.palNumEntries ; i++)
282 color = *(COLORREF*)(palPtr->logpalette.palPalEntry + i);
283 index = PALETTE_GetNearestIndexAndColor( STOCK_DEFAULT_PALETTE, &color);
284 if( index != i ) realized++;
287 COLOR_SetMapping(dc, hMap, 0, i);
292 dprintf_palette(stdnimp, " realized %i colors\n", realized );
297 /***********************************************************************
298 * SelectPalette (USER.282)
300 HPALETTE16 SelectPalette(HDC hDC, HPALETTE16 hPal, BOOL bForceBackground)
302 return GDISelectPalette( hDC, hPal );
306 /***********************************************************************
307 * RealizePalette (USER.283)
309 UINT RealizePalette(HDC hDC)
311 return GDIRealizePalette( hDC );