Release 941227
[wine/multimedia.git] / objects / palette.c
blob5ae0188475709dda96238f50d1556cdfcdd27eee
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 "gdi.h"
19 #include "color.h"
20 #include "stddebug.h"
21 /* #define DEBUG_PALETTE */
22 #include "debug.h"
24 /***********************************************************************
25 * CreatePalette (GDI.360)
27 HPALETTE CreatePalette( LOGPALETTE * palette )
29 PALETTEOBJ * palettePtr;
30 HPALETTE hpalette;
31 int size;
33 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
34 hpalette = GDI_AllocObject( sizeof(GDIOBJHDR) + size, PALETTE_MAGIC );
35 if (!hpalette) return 0;
36 palettePtr = (PALETTEOBJ *) GDI_HEAP_ADDR( hpalette );
37 memcpy( &palettePtr->logpalette, palette, size );
38 return hpalette;
42 /***********************************************************************
43 * GetPaletteEntries (GDI.363)
45 WORD GetPaletteEntries( HPALETTE hpalette, WORD start, WORD count,
46 LPPALETTEENTRY entries )
48 PALETTEOBJ * palPtr;
49 int numEntries;
51 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
52 if (!palPtr) return 0;
53 numEntries = palPtr->logpalette.palNumEntries;
54 if (start >= numEntries) return 0;
55 if (start+count > numEntries) count = numEntries - start;
56 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
57 count * sizeof(PALETTEENTRY) );
58 return count;
62 /***********************************************************************
63 * SetPaletteEntries (GDI.364)
65 WORD SetPaletteEntries( HPALETTE hpalette, WORD start, WORD count,
66 LPPALETTEENTRY entries )
68 PALETTEOBJ * palPtr;
69 int numEntries;
71 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
72 if (!palPtr) return 0;
73 numEntries = palPtr->logpalette.palNumEntries;
74 if (start >= numEntries) return 0;
75 if (start+count > numEntries) count = numEntries - start;
76 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
77 count * sizeof(PALETTEENTRY) );
78 return count;
82 /***********************************************************************
83 * GetSystemPaletteEntries (GDI.375)
85 WORD GetSystemPaletteEntries( HDC hdc, WORD start, WORD count,
86 LPPALETTEENTRY entries )
88 WORD i;
89 DC *dc;
90 XColor color;
92 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
93 if (start >= dc->w.devCaps->sizePalette) return 0;
94 if (start+count >= dc->w.devCaps->sizePalette)
95 count = dc->w.devCaps->sizePalette - start;
96 for (i = 0; i < count; i++)
98 color.pixel = start + i;
99 XQueryColor( display, COLOR_WinColormap, &color );
100 entries[i].peRed = color.red >> 8;
101 entries[i].peGreen = color.green >> 8;
102 entries[i].peBlue = color.blue >> 8;
103 entries[i].peFlags = 0;
105 return count;
109 /***********************************************************************
110 * GetNearestPaletteIndex (GDI.370)
112 WORD GetNearestPaletteIndex( HPALETTE hpalette, COLORREF color )
114 int i, minDist, dist;
115 WORD index = 0;
116 BYTE r, g, b;
117 PALETTEENTRY * entry;
118 PALETTEOBJ * palPtr;
120 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
121 if (!palPtr) return 0;
123 if ((COLOR_WinColormap != DefaultColormapOfScreen(screen)) &&
124 (hpalette == STOCK_DEFAULT_PALETTE))
126 if ((color & 0xffffff) == 0) return 0; /* Entry 0 is black */
127 if ((color & 0xffffff) == 0xffffff) /* Max entry is white */
128 return palPtr->logpalette.palNumEntries - 1;
131 r = GetRValue(color);
132 g = GetGValue(color);
133 b = GetBValue(color);
135 entry = palPtr->logpalette.palPalEntry;
136 for (i = 0, minDist = MAXINT; minDist !=0 &&
137 i < palPtr->logpalette.palNumEntries ; i++)
139 if (entry->peFlags != 0xff)
141 dist = (r - entry->peRed) * (r - entry->peRed) +
142 (g - entry->peGreen) * (g - entry->peGreen) +
143 (b - entry->peBlue) * (b - entry->peBlue);
144 if (dist < minDist)
146 minDist = dist;
147 index = i;
150 entry++;
152 dprintf_palette(stddeb,"GetNearestPaletteIndex(%x,%06lx): returning %d\n",
153 hpalette, color, index );
154 return index;
158 /***********************************************************************
159 * PALETTE_GetObject
161 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
163 if (count > sizeof(WORD)) count = sizeof(WORD);
164 memcpy( buffer, &palette->logpalette.palNumEntries, count );
165 return count;
169 /***********************************************************************
170 * GDISelectPalette (GDI.361)
172 HPALETTE GDISelectPalette( HDC hdc, HPALETTE hpal )
174 HPALETTE prev;
175 DC *dc;
177 dprintf_palette(stddeb, "GDISelectPalette: %d %d\n", hdc, hpal );
178 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
179 prev = dc->w.hPalette;
180 dc->w.hPalette = hpal;
181 if (hpal != STOCK_DEFAULT_PALETTE) COLOR_SetMapping( dc, 0, 0, 0 );
182 else RealizeDefaultPalette( hdc ); /* Always realize default palette */
183 return prev;
187 /***********************************************************************
188 * GDIRealizePalette (GDI.362)
190 UINT GDIRealizePalette( HDC hdc )
192 dprintf_palette(stdnimp, "GDIRealizePalette: %d\n", hdc );
193 return 0;
197 /***********************************************************************
198 * SelectPalette (USER.282)
200 HPALETTE SelectPalette(HDC hDC, HPALETTE hPal, BOOL bForceBackground)
202 return GDISelectPalette( hDC, hPal );
206 /***********************************************************************
207 * RealizePalette (USER.283)
209 UINT RealizePalette(HDC hDC)
211 return GDIRealizePalette( hDC );