Release 940405
[wine/gsoc-2012-control.git] / objects / palette.c
blobdc52635a23e6b2da45dbfb342fe71d799df24ccf
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"
26 extern void COLOR_SetMapping( DC *dc, HANDLE map, WORD size ); /* color.c */
28 extern Colormap COLOR_WinColormap;
31 /***********************************************************************
32 * CreatePalette (GDI.360)
34 HPALETTE CreatePalette( LOGPALETTE * palette )
36 PALETTEOBJ * palettePtr;
37 HPALETTE hpalette;
38 int size;
40 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
41 hpalette = GDI_AllocObject( sizeof(GDIOBJHDR) + size, PALETTE_MAGIC );
42 if (!hpalette) return 0;
43 palettePtr = (PALETTEOBJ *) GDI_HEAP_ADDR( hpalette );
44 memcpy( &palettePtr->logpalette, palette, size );
45 return hpalette;
49 /***********************************************************************
50 * GetPaletteEntries (GDI.363)
52 WORD GetPaletteEntries( HPALETTE hpalette, WORD start, WORD count,
53 LPPALETTEENTRY entries )
55 PALETTEOBJ * palPtr;
56 int numEntries;
58 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
59 if (!palPtr) return 0;
60 numEntries = palPtr->logpalette.palNumEntries;
61 if (start >= numEntries) return 0;
62 if (start+count > numEntries) count = numEntries - start;
63 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
64 count * sizeof(PALETTEENTRY) );
65 return count;
69 /***********************************************************************
70 * SetPaletteEntries (GDI.364)
72 WORD SetPaletteEntries( HPALETTE hpalette, WORD start, WORD count,
73 LPPALETTEENTRY entries )
75 PALETTEOBJ * palPtr;
76 int numEntries;
78 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
79 if (!palPtr) return 0;
80 numEntries = palPtr->logpalette.palNumEntries;
81 if (start >= numEntries) return 0;
82 if (start+count > numEntries) count = numEntries - start;
83 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
84 count * sizeof(PALETTEENTRY) );
85 return count;
89 /***********************************************************************
90 * GetSystemPaletteEntries (GDI.375)
92 WORD GetSystemPaletteEntries( HDC hdc, WORD start, WORD count,
93 LPPALETTEENTRY entries )
95 WORD i;
96 DC *dc;
97 XColor color;
99 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
100 if (start >= dc->w.devCaps->sizePalette) return 0;
101 if (start+count >= dc->w.devCaps->sizePalette)
102 count = dc->w.devCaps->sizePalette - start;
103 for (i = 0; i < count; i++)
105 color.pixel = start + i;
106 XQueryColor( display, COLOR_WinColormap, &color );
107 entries[i].peRed = color.red >> 8;
108 entries[i].peGreen = color.green >> 8;
109 entries[i].peBlue = color.blue >> 8;
110 entries[i].peFlags = 0;
112 return count;
116 /***********************************************************************
117 * GetNearestPaletteIndex (GDI.370)
119 WORD GetNearestPaletteIndex( HPALETTE hpalette, COLORREF color )
121 int i, minDist, dist;
122 WORD index = 0;
123 BYTE r, g, b;
124 PALETTEENTRY * entry;
125 PALETTEOBJ * palPtr;
127 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
128 if (!palPtr) return 0;
130 if ((COLOR_WinColormap != DefaultColormapOfScreen(screen)) &&
131 (hpalette == STOCK_DEFAULT_PALETTE))
133 if ((color & 0xffffff) == 0) return 0; /* Entry 0 is black */
134 if ((color & 0xffffff) == 0xffffff) /* Max entry is white */
135 return palPtr->logpalette.palNumEntries - 1;
138 r = GetRValue(color);
139 g = GetGValue(color);
140 b = GetBValue(color);
142 entry = palPtr->logpalette.palPalEntry;
143 for (i = 0, minDist = MAXINT; minDist !=0 &&
144 i < palPtr->logpalette.palNumEntries ; i++)
146 if (entry->peFlags != 0xff)
148 dist = (r - entry->peRed) * (r - entry->peRed) +
149 (g - entry->peGreen) * (g - entry->peGreen) +
150 (b - entry->peBlue) * (b - entry->peBlue);
151 if (dist < minDist)
153 minDist = dist;
154 index = i;
157 entry++;
159 #ifdef DEBUG_GDI
160 printf( "GetNearestPaletteIndex(%x,%06x) : returning %d\n",
161 hpalette, color, index );
162 #endif
163 return index;
167 /***********************************************************************
168 * PALETTE_GetObject
170 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
172 if (count > sizeof(WORD)) count = sizeof(WORD);
173 memcpy( buffer, &palette->logpalette.palNumEntries, count );
174 return count;
178 /***********************************************************************
179 * GDISelectPalette (GDI.361)
181 HPALETTE GDISelectPalette( HDC hdc, HPALETTE hpal )
183 HPALETTE prev;
184 DC *dc;
186 #ifdef DEBUG_PALETTE
187 printf( "GDISelectPalette: %d %d\n", hdc, hpal );
188 #endif
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 #ifdef DEBUG_PALETTE
204 printf( "GDIRealizePalette: %d\n", hdc );
205 #endif
206 return 0;
210 /***********************************************************************
211 * SelectPalette (USER.282)
213 HPALETTE SelectPalette(HDC hDC, HPALETTE hPal, BOOL bForceBackground)
215 return GDISelectPalette( hDC, hPal );
219 /***********************************************************************
220 * RealizePalette (USER.283)
222 UINT RealizePalette(HDC hDC)
224 return GDIRealizePalette( hDC );