Release 941017
[wine/gsoc-2012-control.git] / objects / palette.c
blob7761c2e211a1e63b7e323d06cb8a6c9b2b2fdac8
1 /*
2 * GDI palette objects
4 * Copyright 1993,1994 Alexandre Julliard
5 */
7 static char Copyright[] = "Copyright Alexandre Julliard, 1993,1994";
9 #include <stdlib.h>
10 #include <string.h>
11 #include <limits.h>
13 #ifdef linux
14 #include <values.h>
15 #endif
18 #if !defined (MAXINT)
19 #include <limits.h>
20 #define MAXINT INT_MAX
21 #endif
23 #include <X11/Xlib.h>
24 #include "gdi.h"
25 #include "stddebug.h"
26 /* #define DEBUG_PALETTE /* */
27 /* #undef DEBUG_PALETTE /* */
28 #include "debug.h"
30 extern void COLOR_SetMapping( DC *dc, HANDLE map, WORD size ); /* color.c */
32 extern Colormap COLOR_WinColormap;
35 /***********************************************************************
36 * CreatePalette (GDI.360)
38 HPALETTE CreatePalette( LOGPALETTE * palette )
40 PALETTEOBJ * palettePtr;
41 HPALETTE hpalette;
42 int size;
44 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
45 hpalette = GDI_AllocObject( sizeof(GDIOBJHDR) + size, PALETTE_MAGIC );
46 if (!hpalette) return 0;
47 palettePtr = (PALETTEOBJ *) GDI_HEAP_ADDR( hpalette );
48 memcpy( &palettePtr->logpalette, palette, size );
49 return hpalette;
53 /***********************************************************************
54 * GetPaletteEntries (GDI.363)
56 WORD GetPaletteEntries( HPALETTE hpalette, WORD start, WORD count,
57 LPPALETTEENTRY entries )
59 PALETTEOBJ * palPtr;
60 int numEntries;
62 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
63 if (!palPtr) return 0;
64 numEntries = palPtr->logpalette.palNumEntries;
65 if (start >= numEntries) return 0;
66 if (start+count > numEntries) count = numEntries - start;
67 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
68 count * sizeof(PALETTEENTRY) );
69 return count;
73 /***********************************************************************
74 * SetPaletteEntries (GDI.364)
76 WORD SetPaletteEntries( HPALETTE hpalette, WORD start, WORD count,
77 LPPALETTEENTRY entries )
79 PALETTEOBJ * palPtr;
80 int numEntries;
82 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
83 if (!palPtr) return 0;
84 numEntries = palPtr->logpalette.palNumEntries;
85 if (start >= numEntries) return 0;
86 if (start+count > numEntries) count = numEntries - start;
87 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
88 count * sizeof(PALETTEENTRY) );
89 return count;
93 /***********************************************************************
94 * GetSystemPaletteEntries (GDI.375)
96 WORD GetSystemPaletteEntries( HDC hdc, WORD start, WORD count,
97 LPPALETTEENTRY entries )
99 WORD i;
100 DC *dc;
101 XColor color;
103 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
104 if (start >= dc->w.devCaps->sizePalette) return 0;
105 if (start+count >= dc->w.devCaps->sizePalette)
106 count = dc->w.devCaps->sizePalette - start;
107 for (i = 0; i < count; i++)
109 color.pixel = start + i;
110 XQueryColor( display, COLOR_WinColormap, &color );
111 entries[i].peRed = color.red >> 8;
112 entries[i].peGreen = color.green >> 8;
113 entries[i].peBlue = color.blue >> 8;
114 entries[i].peFlags = 0;
116 return count;
120 /***********************************************************************
121 * GetNearestPaletteIndex (GDI.370)
123 WORD GetNearestPaletteIndex( HPALETTE hpalette, COLORREF color )
125 int i, minDist, dist;
126 WORD index = 0;
127 BYTE r, g, b;
128 PALETTEENTRY * entry;
129 PALETTEOBJ * palPtr;
131 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
132 if (!palPtr) return 0;
134 if ((COLOR_WinColormap != DefaultColormapOfScreen(screen)) &&
135 (hpalette == STOCK_DEFAULT_PALETTE))
137 if ((color & 0xffffff) == 0) return 0; /* Entry 0 is black */
138 if ((color & 0xffffff) == 0xffffff) /* Max entry is white */
139 return palPtr->logpalette.palNumEntries - 1;
142 r = GetRValue(color);
143 g = GetGValue(color);
144 b = GetBValue(color);
146 entry = palPtr->logpalette.palPalEntry;
147 for (i = 0, minDist = MAXINT; minDist !=0 &&
148 i < palPtr->logpalette.palNumEntries ; i++)
150 if (entry->peFlags != 0xff)
152 dist = (r - entry->peRed) * (r - entry->peRed) +
153 (g - entry->peGreen) * (g - entry->peGreen) +
154 (b - entry->peBlue) * (b - entry->peBlue);
155 if (dist < minDist)
157 minDist = dist;
158 index = i;
161 entry++;
163 dprintf_palette(stddeb,"GetNearestPaletteIndex(%x,%06x) : returning %d\n",
164 hpalette, color, index );
165 return index;
169 /***********************************************************************
170 * PALETTE_GetObject
172 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
174 if (count > sizeof(WORD)) count = sizeof(WORD);
175 memcpy( buffer, &palette->logpalette.palNumEntries, count );
176 return count;
180 /***********************************************************************
181 * GDISelectPalette (GDI.361)
183 HPALETTE GDISelectPalette( HDC hdc, HPALETTE hpal )
185 HPALETTE prev;
186 DC *dc;
188 dprintf_palette(stddeb, "GDISelectPalette: %d %d\n", hdc, hpal );
189 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
190 prev = dc->w.hPalette;
191 dc->w.hPalette = hpal;
192 if (hpal != STOCK_DEFAULT_PALETTE) COLOR_SetMapping( dc, 0, 0 );
193 else RealizeDefaultPalette( hdc ); /* Always realize default palette */
194 return prev;
198 /***********************************************************************
199 * GDIRealizePalette (GDI.362)
201 UINT GDIRealizePalette( HDC hdc )
203 dprintf_palette(stdnimp, "GDIRealizePalette: %d\n", hdc );
204 return 0;
208 /***********************************************************************
209 * SelectPalette (USER.282)
211 HPALETTE SelectPalette(HDC hDC, HPALETTE hPal, BOOL bForceBackground)
213 return GDISelectPalette( hDC, hPal );
217 /***********************************************************************
218 * RealizePalette (USER.283)
220 UINT RealizePalette(HDC hDC)
222 return GDIRealizePalette( hDC );