4 * Copyright 1993,1994 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * PALETTEOBJ is documented in the Dr. Dobbs Journal May 1993.
23 * Information in the "Undocumented Windows" is incorrect.
36 #include "gdi_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(palette);
41 typedef struct tagPALETTEOBJ
44 const DC_FUNCTIONS *funcs; /* DC function table */
45 WORD version; /* palette version */
46 WORD count; /* count of palette entries */
47 PALETTEENTRY *entries;
50 static INT PALETTE_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
51 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle );
52 static BOOL PALETTE_DeleteObject( HGDIOBJ handle );
54 static const struct gdi_obj_funcs palette_funcs =
56 NULL, /* pSelectObject */
57 PALETTE_GetObject, /* pGetObjectA */
58 PALETTE_GetObject, /* pGetObjectW */
59 PALETTE_UnrealizeObject, /* pUnrealizeObject */
60 PALETTE_DeleteObject /* pDeleteObject */
63 /* Pointers to USER implementation of SelectPalette/RealizePalette */
64 /* they will be patched by USER on startup */
65 HPALETTE (WINAPI *pfnSelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = GDISelectPalette;
66 UINT (WINAPI *pfnRealizePalette)(HDC hdc) = GDIRealizePalette;
68 static UINT SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
70 static HPALETTE hPrimaryPalette = 0; /* used for WM_PALETTECHANGED */
71 static HPALETTE hLastRealizedPalette = 0; /* UnrealizeObject() needs it */
73 #define NB_RESERVED_COLORS 20 /* number of fixed colors in system palette */
75 static const PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS] =
77 /* first 10 entries in the system palette */
78 /* red green blue flags */
79 { 0x00, 0x00, 0x00, 0 },
80 { 0x80, 0x00, 0x00, 0 },
81 { 0x00, 0x80, 0x00, 0 },
82 { 0x80, 0x80, 0x00, 0 },
83 { 0x00, 0x00, 0x80, 0 },
84 { 0x80, 0x00, 0x80, 0 },
85 { 0x00, 0x80, 0x80, 0 },
86 { 0xc0, 0xc0, 0xc0, 0 },
87 { 0xc0, 0xdc, 0xc0, 0 },
88 { 0xa6, 0xca, 0xf0, 0 },
90 /* ... c_min/2 dynamic colorcells */
92 /* ... gap (for sparse palettes) */
94 /* ... c_min/2 dynamic colorcells */
96 { 0xff, 0xfb, 0xf0, 0 },
97 { 0xa0, 0xa0, 0xa4, 0 },
98 { 0x80, 0x80, 0x80, 0 },
99 { 0xff, 0x00, 0x00, 0 },
100 { 0x00, 0xff, 0x00, 0 },
101 { 0xff, 0xff, 0x00, 0 },
102 { 0x00, 0x00, 0xff, 0 },
103 { 0xff, 0x00, 0xff, 0 },
104 { 0x00, 0xff, 0xff, 0 },
105 { 0xff, 0xff, 0xff, 0 } /* last 10 */
108 /***********************************************************************
111 * Create the system palette.
113 HPALETTE PALETTE_Init(void)
118 /* create default palette (20 system colors) */
120 palPtr = HeapAlloc( GetProcessHeap(), 0,
121 sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY));
122 if (!palPtr) return FALSE;
124 palPtr->palVersion = 0x300;
125 palPtr->palNumEntries = NB_RESERVED_COLORS;
126 memcpy( palPtr->palPalEntry, sys_pal_template, sizeof(sys_pal_template) );
127 hpalette = CreatePalette( palPtr );
128 HeapFree( GetProcessHeap(), 0, palPtr );
133 /***********************************************************************
134 * CreatePalette [GDI32.@]
136 * Creates a logical color palette.
139 * Success: Handle to logical palette
142 HPALETTE WINAPI CreatePalette(
143 const LOGPALETTE* palette) /* [in] Pointer to logical color palette */
145 PALETTEOBJ * palettePtr;
149 if (!palette) return 0;
150 TRACE("entries=%i\n", palette->palNumEntries);
152 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
154 if (!(palettePtr = HeapAlloc( GetProcessHeap(), 0, sizeof(*palettePtr) ))) return 0;
155 palettePtr->funcs = NULL;
156 palettePtr->version = palette->palVersion;
157 palettePtr->count = palette->palNumEntries;
158 size = palettePtr->count * sizeof(*palettePtr->entries);
159 if (!(palettePtr->entries = HeapAlloc( GetProcessHeap(), 0, size )))
161 HeapFree( GetProcessHeap(), 0, palettePtr );
164 memcpy( palettePtr->entries, palette->palPalEntry, size );
165 if (!(hpalette = alloc_gdi_handle( &palettePtr->header, OBJ_PAL, &palette_funcs )))
167 HeapFree( GetProcessHeap(), 0, palettePtr->entries );
168 HeapFree( GetProcessHeap(), 0, palettePtr );
170 TRACE(" returning %p\n", hpalette);
175 /***********************************************************************
176 * CreateHalftonePalette [GDI32.@]
178 * Creates a halftone palette.
181 * Success: Handle to logical halftone palette
184 * FIXME: This simply creates the halftone palette derived from running
185 * tests on a windows NT machine. This is assuming a color depth
186 * of greater that 256 color. On a 256 color device the halftone
187 * palette will be different and this function will be incorrect
189 HPALETTE WINAPI CreateHalftonePalette(
190 HDC hdc) /* [in] Handle to device context */
195 WORD NumberOfEntries;
196 PALETTEENTRY aEntries[256];
199 Palette.Version = 0x300;
200 Palette.NumberOfEntries = 256;
201 GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
203 Palette.NumberOfEntries = 20;
205 for (i = 0; i < Palette.NumberOfEntries; i++)
207 Palette.aEntries[i].peRed=0xff;
208 Palette.aEntries[i].peGreen=0xff;
209 Palette.aEntries[i].peBlue=0xff;
210 Palette.aEntries[i].peFlags=0x00;
213 Palette.aEntries[0].peRed=0x00;
214 Palette.aEntries[0].peBlue=0x00;
215 Palette.aEntries[0].peGreen=0x00;
218 for (i=1; i <= 6; i++)
220 Palette.aEntries[i].peRed=(i%2)?0x80:0;
221 Palette.aEntries[i].peGreen=(i==2)?0x80:(i==3)?0x80:(i==6)?0x80:0;
222 Palette.aEntries[i].peBlue=(i>3)?0x80:0;
225 for (i=7; i <= 12; i++)
230 Palette.aEntries[i].peRed=0xc0;
231 Palette.aEntries[i].peBlue=0xc0;
232 Palette.aEntries[i].peGreen=0xc0;
235 Palette.aEntries[i].peRed=0xc0;
236 Palette.aEntries[i].peGreen=0xdc;
237 Palette.aEntries[i].peBlue=0xc0;
240 Palette.aEntries[i].peRed=0xa6;
241 Palette.aEntries[i].peGreen=0xca;
242 Palette.aEntries[i].peBlue=0xf0;
245 Palette.aEntries[i].peRed=0xff;
246 Palette.aEntries[i].peGreen=0xfb;
247 Palette.aEntries[i].peBlue=0xf0;
250 Palette.aEntries[i].peRed=0xa0;
251 Palette.aEntries[i].peGreen=0xa0;
252 Palette.aEntries[i].peBlue=0xa4;
255 Palette.aEntries[i].peRed=0x80;
256 Palette.aEntries[i].peGreen=0x80;
257 Palette.aEntries[i].peBlue=0x80;
261 for (i=13; i <= 18; i++)
263 Palette.aEntries[i].peRed=(i%2)?0xff:0;
264 Palette.aEntries[i].peGreen=(i==14)?0xff:(i==15)?0xff:(i==18)?0xff:0;
265 Palette.aEntries[i].peBlue=(i>15)?0xff:0x00;
268 return CreatePalette((LOGPALETTE *)&Palette);
272 /***********************************************************************
273 * GetPaletteEntries [GDI32.@]
275 * Retrieves palette entries.
278 * Success: Number of entries from logical palette
281 UINT WINAPI GetPaletteEntries(
282 HPALETTE hpalette, /* [in] Handle of logical palette */
283 UINT start, /* [in] First entry to receive */
284 UINT count, /* [in] Number of entries to receive */
285 LPPALETTEENTRY entries) /* [out] Address of array receiving entries */
290 TRACE("hpal = %p, count=%i\n", hpalette, count );
292 palPtr = GDI_GetObjPtr( hpalette, OBJ_PAL );
293 if (!palPtr) return 0;
295 /* NOTE: not documented but test show this to be the case */
298 count = palPtr->count;
302 numEntries = palPtr->count;
303 if (start+count > numEntries) count = numEntries - start;
306 if (start >= numEntries) count = 0;
307 else memcpy( entries, &palPtr->entries[start], count * sizeof(PALETTEENTRY) );
311 GDI_ReleaseObj( hpalette );
316 /***********************************************************************
317 * SetPaletteEntries [GDI32.@]
319 * Sets color values for range in palette.
322 * Success: Number of entries that were set
325 UINT WINAPI SetPaletteEntries(
326 HPALETTE hpalette, /* [in] Handle of logical palette */
327 UINT start, /* [in] Index of first entry to set */
328 UINT count, /* [in] Number of entries to set */
329 const PALETTEENTRY *entries) /* [in] Address of array of structures */
334 TRACE("hpal=%p,start=%i,count=%i\n",hpalette,start,count );
336 if (hpalette == GetStockObject(DEFAULT_PALETTE)) return 0;
337 palPtr = GDI_GetObjPtr( hpalette, OBJ_PAL );
338 if (!palPtr) return 0;
340 numEntries = palPtr->count;
341 if (start >= numEntries)
343 GDI_ReleaseObj( hpalette );
346 if (start+count > numEntries) count = numEntries - start;
347 memcpy( &palPtr->entries[start], entries, count * sizeof(PALETTEENTRY) );
348 GDI_ReleaseObj( hpalette );
349 UnrealizeObject( hpalette );
354 /***********************************************************************
355 * ResizePalette [GDI32.@]
357 * Resizes logical palette.
363 BOOL WINAPI ResizePalette(
364 HPALETTE hPal, /* [in] Handle of logical palette */
365 UINT cEntries) /* [in] Number of entries in logical palette */
367 PALETTEOBJ * palPtr = GDI_GetObjPtr( hPal, OBJ_PAL );
368 PALETTEENTRY *entries;
370 if( !palPtr ) return FALSE;
371 TRACE("hpal = %p, prev = %i, new = %i\n", hPal, palPtr->count, cEntries );
373 if (!(entries = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
374 palPtr->entries, cEntries * sizeof(*palPtr->entries) )))
376 GDI_ReleaseObj( hPal );
379 palPtr->entries = entries;
380 palPtr->count = cEntries;
382 GDI_ReleaseObj( hPal );
383 PALETTE_UnrealizeObject( hPal );
388 /***********************************************************************
389 * AnimatePalette [GDI32.@]
391 * Replaces entries in logical palette.
398 * Should use existing mapping when animating a primary palette
400 BOOL WINAPI AnimatePalette(
401 HPALETTE hPal, /* [in] Handle to logical palette */
402 UINT StartIndex, /* [in] First entry in palette */
403 UINT NumEntries, /* [in] Count of entries in palette */
404 const PALETTEENTRY* PaletteColors) /* [in] Pointer to first replacement */
406 TRACE("%p (%i - %i)\n", hPal, StartIndex,StartIndex+NumEntries);
408 if( hPal != GetStockObject(DEFAULT_PALETTE) )
412 const PALETTEENTRY *pptr = PaletteColors;
413 const DC_FUNCTIONS *funcs;
415 palPtr = GDI_GetObjPtr( hPal, OBJ_PAL );
416 if (!palPtr) return 0;
418 pal_entries = palPtr->count;
419 if (StartIndex >= pal_entries)
421 GDI_ReleaseObj( hPal );
424 if (StartIndex+NumEntries > pal_entries) NumEntries = pal_entries - StartIndex;
426 for (NumEntries += StartIndex; StartIndex < NumEntries; StartIndex++, pptr++) {
427 /* According to MSDN, only animate PC_RESERVED colours */
428 if (palPtr->entries[StartIndex].peFlags & PC_RESERVED) {
429 TRACE("Animating colour (%d,%d,%d) to (%d,%d,%d)\n",
430 palPtr->entries[StartIndex].peRed,
431 palPtr->entries[StartIndex].peGreen,
432 palPtr->entries[StartIndex].peBlue,
433 pptr->peRed, pptr->peGreen, pptr->peBlue);
434 palPtr->entries[StartIndex] = *pptr;
436 TRACE("Not animating entry %d -- not PC_RESERVED\n", StartIndex);
439 funcs = palPtr->funcs;
440 GDI_ReleaseObj( hPal );
441 if (funcs && funcs->pRealizePalette) funcs->pRealizePalette( NULL, hPal, hPal == hPrimaryPalette );
447 /***********************************************************************
448 * SetSystemPaletteUse [GDI32.@]
450 * Specify whether the system palette contains 2 or 20 static colors.
453 * Success: Previous system palette
454 * Failure: SYSPAL_ERROR
456 UINT WINAPI SetSystemPaletteUse(
457 HDC hdc, /* [in] Handle of device context */
458 UINT use) /* [in] Palette-usage flag */
460 UINT old = SystemPaletteUse;
462 /* Device doesn't support colour palettes */
463 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)) {
468 case SYSPAL_NOSTATIC:
469 case SYSPAL_NOSTATIC256: /* WINVER >= 0x0500 */
471 SystemPaletteUse = use;
479 /***********************************************************************
480 * GetSystemPaletteUse [GDI32.@]
482 * Gets state of system palette.
485 * Current state of system palette
487 UINT WINAPI GetSystemPaletteUse(
488 HDC hdc) /* [in] Handle of device context */
490 return SystemPaletteUse;
494 /***********************************************************************
495 * GetSystemPaletteEntries [GDI32.@]
497 * Gets range of palette entries.
500 * Success: Number of entries retrieved from palette
503 UINT WINAPI GetSystemPaletteEntries(
504 HDC hdc, /* [in] Handle of device context */
505 UINT start, /* [in] Index of first entry to be retrieved */
506 UINT count, /* [in] Number of entries to be retrieved */
507 LPPALETTEENTRY entries) /* [out] Array receiving system-palette entries */
512 TRACE("hdc=%p,start=%i,count=%i\n", hdc,start,count);
514 if ((dc = get_dc_ptr( hdc )))
516 if (dc->funcs->pGetSystemPaletteEntries)
517 ret = dc->funcs->pGetSystemPaletteEntries( dc->physDev, start, count, entries );
518 release_dc_ptr( dc );
524 /***********************************************************************
525 * GetNearestPaletteIndex [GDI32.@]
527 * Gets palette index for color.
530 * Should index be initialized to CLR_INVALID instead of 0?
533 * Success: Index of entry in logical palette
534 * Failure: CLR_INVALID
536 UINT WINAPI GetNearestPaletteIndex(
537 HPALETTE hpalette, /* [in] Handle of logical color palette */
538 COLORREF color) /* [in] Color to be matched */
540 PALETTEOBJ* palObj = GDI_GetObjPtr( hpalette, OBJ_PAL );
545 int i, diff = 0x7fffffff;
547 PALETTEENTRY* entry = palObj->entries;
549 for( i = 0; i < palObj->count && diff ; i++, entry++)
551 r = entry->peRed - GetRValue(color);
552 g = entry->peGreen - GetGValue(color);
553 b = entry->peBlue - GetBValue(color);
557 if( r < diff ) { index = i; diff = r; }
559 GDI_ReleaseObj( hpalette );
561 TRACE("(%p,%06x): returning %d\n", hpalette, color, index );
566 /***********************************************************************
567 * GetNearestColor [GDI32.@]
569 * Gets a system color to match.
572 * Success: Color from system palette that corresponds to given color
573 * Failure: CLR_INVALID
575 COLORREF WINAPI GetNearestColor(
576 HDC hdc, /* [in] Handle of device context */
577 COLORREF color) /* [in] Color to be matched */
579 unsigned char spec_type;
583 if (!(dc = get_dc_ptr( hdc ))) return CLR_INVALID;
585 if (dc->funcs->pGetNearestColor)
587 nearest = dc->funcs->pGetNearestColor( dc->physDev, color );
588 release_dc_ptr( dc );
592 if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE))
594 release_dc_ptr( dc );
598 spec_type = color >> 24;
599 if (spec_type == 1 || spec_type == 2)
601 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
605 HPALETTE hpal = dc->hPalette ? dc->hPalette : GetStockObject( DEFAULT_PALETTE );
607 if (spec_type == 2) /* PALETTERGB */
608 index = GetNearestPaletteIndex( hpal, color );
609 else /* PALETTEINDEX */
610 index = LOWORD(color);
612 if (!GetPaletteEntries( hpal, index, 1, &entry ))
614 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color, index );
615 if (!GetPaletteEntries( hpal, 0, 1, &entry ))
617 release_dc_ptr( dc );
621 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
623 nearest = color & 0x00ffffff;
624 release_dc_ptr( dc );
626 TRACE("(%06x): returning %06x\n", color, nearest );
631 /***********************************************************************
634 static INT PALETTE_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
636 PALETTEOBJ *palette = GDI_GetObjPtr( handle, OBJ_PAL );
638 if (!palette) return 0;
642 if (count > sizeof(WORD)) count = sizeof(WORD);
643 memcpy( buffer, &palette->count, count );
645 else count = sizeof(WORD);
646 GDI_ReleaseObj( handle );
651 /***********************************************************************
652 * PALETTE_UnrealizeObject
654 static BOOL PALETTE_UnrealizeObject( HGDIOBJ handle )
656 PALETTEOBJ *palette = GDI_GetObjPtr( handle, OBJ_PAL );
660 const DC_FUNCTIONS *funcs = palette->funcs;
661 palette->funcs = NULL;
662 GDI_ReleaseObj( handle );
663 if (funcs && funcs->pUnrealizePalette) funcs->pUnrealizePalette( handle );
666 if (InterlockedCompareExchangePointer( (void **)&hLastRealizedPalette, 0, handle ) == handle)
667 TRACE("unrealizing palette %p\n", handle);
673 /***********************************************************************
674 * PALETTE_DeleteObject
676 static BOOL PALETTE_DeleteObject( HGDIOBJ handle )
680 PALETTE_UnrealizeObject( handle );
681 if (!(obj = free_gdi_handle( handle ))) return FALSE;
682 HeapFree( GetProcessHeap(), 0, obj->entries );
683 return HeapFree( GetProcessHeap(), 0, obj );
687 /***********************************************************************
688 * GDISelectPalette (Not a Windows API)
690 HPALETTE WINAPI GDISelectPalette( HDC hdc, HPALETTE hpal, WORD wBkg)
695 TRACE("%p %p\n", hdc, hpal );
697 if (GetObjectType(hpal) != OBJ_PAL)
699 WARN("invalid selected palette %p\n",hpal);
702 if (!(dc = get_dc_ptr( hdc ))) return 0;
704 if (dc->funcs->pSelectPalette) hpal = dc->funcs->pSelectPalette( dc->physDev, hpal, FALSE );
708 if (!wBkg) hPrimaryPalette = hpal;
711 release_dc_ptr( dc );
716 /***********************************************************************
717 * GDIRealizePalette (Not a Windows API)
719 UINT WINAPI GDIRealizePalette( HDC hdc )
722 DC* dc = get_dc_ptr( hdc );
726 TRACE("%p...\n", hdc );
728 if( dc->hPalette == GetStockObject( DEFAULT_PALETTE ))
730 if (dc->funcs->pRealizeDefaultPalette)
731 realized = dc->funcs->pRealizeDefaultPalette( dc->physDev );
733 else if (InterlockedExchangePointer( (void **)&hLastRealizedPalette, dc->hPalette ) != dc->hPalette)
735 if (dc->funcs->pRealizePalette)
737 PALETTEOBJ *palPtr = GDI_GetObjPtr( dc->hPalette, OBJ_PAL );
740 realized = dc->funcs->pRealizePalette( dc->physDev, dc->hPalette,
741 (dc->hPalette == hPrimaryPalette) );
742 palPtr->funcs = dc->funcs;
743 GDI_ReleaseObj( dc->hPalette );
747 else TRACE(" skipping (hLastRealizedPalette = %p)\n", hLastRealizedPalette);
749 release_dc_ptr( dc );
750 TRACE(" realized %i colors.\n", realized );
755 /***********************************************************************
756 * SelectPalette [GDI32.@]
758 * Selects logical palette into DC.
761 * Success: Previous logical palette
764 HPALETTE WINAPI SelectPalette(
765 HDC hDC, /* [in] Handle of device context */
766 HPALETTE hPal, /* [in] Handle of logical color palette */
767 BOOL bForceBackground) /* [in] Foreground/background mode */
769 return pfnSelectPalette( hDC, hPal, bForceBackground );
773 /***********************************************************************
774 * RealizePalette [GDI32.@]
776 * Maps palette entries to system palette.
779 * Success: Number of entries in logical palette
782 UINT WINAPI RealizePalette(
783 HDC hDC) /* [in] Handle of device context */
785 return pfnRealizePalette( hDC );
789 typedef HWND (WINAPI *WindowFromDC_funcptr)( HDC );
790 typedef BOOL (WINAPI *RedrawWindow_funcptr)( HWND, const RECT *, HRGN, UINT );
792 /**********************************************************************
793 * UpdateColors [GDI32.@]
795 * Remaps current colors to logical palette.
801 BOOL WINAPI UpdateColors(
802 HDC hDC) /* [in] Handle of device context */
805 int size = GetDeviceCaps( hDC, SIZEPALETTE );
809 mod = GetModuleHandleA("user32.dll");
812 WindowFromDC_funcptr pWindowFromDC = (WindowFromDC_funcptr)GetProcAddress(mod,"WindowFromDC");
815 HWND hWnd = pWindowFromDC( hDC );
817 /* Docs say that we have to remap current drawable pixel by pixel
818 * but it would take forever given the speed of XGet/PutPixel.
822 RedrawWindow_funcptr pRedrawWindow = (void *)GetProcAddress( mod, "RedrawWindow" );
823 if (pRedrawWindow) pRedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
830 /*********************************************************************
831 * SetMagicColors (GDI32.@)
833 BOOL WINAPI SetMagicColors(HDC hdc, ULONG u1, ULONG u2)
835 FIXME("(%p 0x%08x 0x%08x): stub\n", hdc, u1, u2);