Release 950606
[wine/testsucceed.git] / objects / palette.c
blobdd925ceaa7596c015cac25177bc68febabaa1358
1 /*
2 * GDI palette objects
4 * Copyright 1993,1994 Alexandre Julliard
6 static char Copyright[] = "Copyright Alexandre Julliard, 1993,1994";
7 */
8 #include <stdlib.h>
9 #include <string.h>
10 #include <limits.h>
12 #if !defined (MAXINT)
13 #include <limits.h>
14 #define MAXINT INT_MAX
15 #endif
17 #include <X11/Xlib.h>
18 #include "color.h"
19 #include "palette.h"
20 #include "stddebug.h"
21 /* #define DEBUG_PALETTE */
22 #include "debug.h"
24 static WORD SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
26 /***********************************************************************
27 * CreatePalette (GDI.360)
29 HPALETTE CreatePalette( LOGPALETTE * palette )
31 PALETTEOBJ * palettePtr;
32 HPALETTE hpalette;
33 int size;
35 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
36 hpalette = GDI_AllocObject( sizeof(GDIOBJHDR) + size, PALETTE_MAGIC );
37 if (!hpalette) return 0;
38 palettePtr = (PALETTEOBJ *) GDI_HEAP_LIN_ADDR( hpalette );
39 memcpy( &palettePtr->logpalette, palette, size );
40 return hpalette;
44 /***********************************************************************
45 * GetPaletteEntries (GDI.363)
47 WORD GetPaletteEntries( HPALETTE hpalette, WORD start, WORD count,
48 LPPALETTEENTRY entries )
50 PALETTEOBJ * palPtr;
51 int numEntries;
53 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
54 if (!palPtr) return 0;
55 numEntries = palPtr->logpalette.palNumEntries;
56 if (start >= numEntries) return 0;
57 if (start+count > numEntries) count = numEntries - start;
58 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
59 count * sizeof(PALETTEENTRY) );
60 return count;
64 /***********************************************************************
65 * SetPaletteEntries (GDI.364)
67 WORD SetPaletteEntries( HPALETTE hpalette, WORD start, WORD count,
68 LPPALETTEENTRY entries )
70 PALETTEOBJ * palPtr;
71 int numEntries;
73 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
74 if (!palPtr) return 0;
75 numEntries = palPtr->logpalette.palNumEntries;
76 if (start >= numEntries) return 0;
77 if (start+count > numEntries) count = numEntries - start;
78 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
79 count * sizeof(PALETTEENTRY) );
80 return count;
84 /***********************************************************************
85 * SetSystemPaletteUse (GDI.373)
86 * Should this be per DC rather than system wide?
87 * Currently, it does not matter as the use is only set and returned,
88 * but not taken into account
90 WORD SetSystemPaletteUse( HDC hdc, WORD use)
92 WORD old=SystemPaletteUse;
93 printf("SetSystemPaletteUse(%04X,%04X) // empty stub !!!\n", hdc, use);
94 SystemPaletteUse=use;
95 return old;
98 /***********************************************************************
99 * GetSystemPaletteUse (GDI.374)
101 WORD GetSystemPaletteUse( HDC hdc )
103 printf("GetSystemPaletteUse(%04X) // empty stub !!!\n", hdc);
104 return SystemPaletteUse;
108 /***********************************************************************
109 * GetSystemPaletteEntries (GDI.375)
111 WORD GetSystemPaletteEntries( HDC hdc, WORD start, WORD count,
112 LPPALETTEENTRY entries )
114 WORD i;
115 DC *dc;
116 XColor color;
118 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
119 if (start >= dc->w.devCaps->sizePalette) return 0;
120 if (start+count >= dc->w.devCaps->sizePalette)
121 count = dc->w.devCaps->sizePalette - start;
122 for (i = 0; i < count; i++)
124 color.pixel = start + i;
125 XQueryColor( display, COLOR_WinColormap, &color );
126 entries[i].peRed = color.red >> 8;
127 entries[i].peGreen = color.green >> 8;
128 entries[i].peBlue = color.blue >> 8;
129 entries[i].peFlags = 0;
131 return count;
135 /***********************************************************************
136 * GetNearestPaletteIndex (GDI.370)
138 WORD GetNearestPaletteIndex( HPALETTE hpalette, COLORREF color )
140 int i, minDist, dist;
141 WORD index = 0;
142 BYTE r, g, b;
143 PALETTEENTRY * entry;
144 PALETTEOBJ * palPtr;
146 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
147 if (!palPtr) return 0;
149 if ((COLOR_WinColormap != DefaultColormapOfScreen(screen)) &&
150 (hpalette == STOCK_DEFAULT_PALETTE))
152 if ((color & 0xffffff) == 0) return 0; /* Entry 0 is black */
153 if ((color & 0xffffff) == 0xffffff) /* Max entry is white */
154 return palPtr->logpalette.palNumEntries - 1;
157 r = GetRValue(color);
158 g = GetGValue(color);
159 b = GetBValue(color);
161 entry = palPtr->logpalette.palPalEntry;
162 for (i = 0, minDist = MAXINT; minDist !=0 &&
163 i < palPtr->logpalette.palNumEntries ; i++)
165 if (entry->peFlags != 0xff)
167 dist = (r - entry->peRed) * (r - entry->peRed) +
168 (g - entry->peGreen) * (g - entry->peGreen) +
169 (b - entry->peBlue) * (b - entry->peBlue);
170 if (dist < minDist)
172 minDist = dist;
173 index = i;
176 entry++;
178 dprintf_palette(stddeb,"GetNearestPaletteIndex(%x,%06lx): returning %d\n",
179 hpalette, color, index );
180 return index;
184 /***********************************************************************
185 * PALETTE_GetObject
187 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
189 if (count > sizeof(WORD)) count = sizeof(WORD);
190 memcpy( buffer, &palette->logpalette.palNumEntries, count );
191 return count;
195 /***********************************************************************
196 * GDISelectPalette (GDI.361)
198 HPALETTE GDISelectPalette( HDC hdc, HPALETTE hpal )
200 HPALETTE prev;
201 DC *dc;
203 dprintf_palette(stddeb, "GDISelectPalette: %d %d\n", hdc, hpal );
204 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
205 prev = dc->w.hPalette;
206 dc->w.hPalette = hpal;
207 if (hpal != STOCK_DEFAULT_PALETTE) COLOR_SetMapping( dc, 0, 0, 0 );
208 else RealizeDefaultPalette( hdc ); /* Always realize default palette */
209 return prev;
213 /***********************************************************************
214 * GDIRealizePalette (GDI.362)
216 UINT GDIRealizePalette( HDC hdc )
218 dprintf_palette(stdnimp, "GDIRealizePalette: %d\n", hdc );
219 return 0;
223 /***********************************************************************
224 * SelectPalette (USER.282)
226 HPALETTE SelectPalette(HDC hDC, HPALETTE hPal, BOOL bForceBackground)
228 return GDISelectPalette( hDC, hPal );
232 /***********************************************************************
233 * RealizePalette (USER.283)
235 UINT RealizePalette(HDC hDC)
237 return GDIRealizePalette( hDC );