Get rid of some direct accesses to the DC structure from outside GDI.
[wine/multimedia.git] / dlls / x11drv / palette.c
blob58f5924a7ff989c0ff8bda839a49c27345fbe876
1 /*
2 * X11DRV OEM bitmap objects
4 * Copyright 1994, 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <stdlib.h>
24 #include <string.h>
26 #include "gdi.h"
27 #include "palette.h"
28 #include "windef.h"
29 #include "winreg.h"
30 #include "x11drv.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(palette);
35 /* Palette indexed mode:
36 * logical palette -> mapping -> pixel
39 * Windows needs contiguous color space ( from 0 to n ) but
40 * it is possible only with the private colormap. Otherwise we
41 * have to map DC palette indices to real pixel values. With
42 * private colormaps it boils down to the identity mapping. The
43 * other special case is when we have a fixed color visual with
44 * the screendepth > 8 - we abandon palette mappings altogether
45 * because pixel values can be calculated without X server
46 * assistance.
48 * Windows palette manager is described in the
49 * http://premium.microsoft.com/msdn/library/techart/f30/f34/f40/d4d/sa942.htm
52 static PALETTEENTRY *COLOR_sysPal; /* current system palette */
54 static int COLOR_gapStart = 256;
55 static int COLOR_gapEnd = -1;
56 static int COLOR_gapFilled = 0;
57 static int COLOR_max = 256;
59 Colormap X11DRV_PALETTE_PaletteXColormap = 0;
60 UINT16 X11DRV_PALETTE_PaletteFlags = 0;
62 typedef struct {
63 int shift;
64 int scale;
65 int max;
66 } ColorShifts;
68 /* initialize to zero to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
69 static ColorShifts X11DRV_PALETTE_PRed = {0,0,0};
70 static ColorShifts X11DRV_PALETTE_LRed = {0,0,0};
71 static ColorShifts X11DRV_PALETTE_PGreen = {0,0,0};
72 static ColorShifts X11DRV_PALETTE_LGreen = {0,0,0};
73 static ColorShifts X11DRV_PALETTE_PBlue = {0,0,0};
74 static ColorShifts X11DRV_PALETTE_LBlue = {0,0,0};
75 static int X11DRV_PALETTE_Graymax = 0;
77 static int palette_size;
79 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
80 static int X11DRV_PALETTE_firstFree = 0;
81 static unsigned char X11DRV_PALETTE_freeList[256];
83 /**********************************************************************/
85 /* Map an EGA index (0..15) to a pixel value in the system color space. */
87 int X11DRV_PALETTE_mapEGAPixel[16];
89 /**********************************************************************/
91 #define NB_COLORCUBE_START_INDEX 63
92 #define NB_PALETTE_EMPTY_VALUE -1
94 /* Maps entry in the system palette to X pixel value */
95 int *X11DRV_PALETTE_PaletteToXPixel = NULL;
97 /* Maps pixel to the entry in the system palette */
98 int *X11DRV_PALETTE_XPixelToPalette = NULL;
100 /**********************************************************************/
102 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template );
103 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template );
104 static void X11DRV_PALETTE_ComputeShifts(unsigned long maskbits, ColorShifts *physical, ColorShifts *to_logical);
105 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template );
106 static void X11DRV_PALETTE_FormatSystemPalette(void);
107 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c);
108 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col);
111 /***********************************************************************
112 * COLOR_Init
114 * Initialize color management.
116 int X11DRV_PALETTE_Init(void)
118 int mask, white, black;
119 int monoPlane;
120 PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS];
122 TRACE("initializing palette manager...\n");
124 white = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
125 black = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
126 monoPlane = 1;
127 for( mask = 1; !((white & mask)^(black & mask)); mask <<= 1 )
128 monoPlane++;
129 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
130 palette_size = visual->map_entries;
132 switch(visual->class)
134 case DirectColor:
135 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
136 case GrayScale:
137 case PseudoColor:
139 HKEY hkey;
140 BOOL private_color_map = FALSE;
141 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", &hkey))
143 char buffer[20];
144 DWORD type, count = sizeof(buffer);
145 if(!RegQueryValueExA(hkey, "PrivateColorMap", 0, &type, buffer, &count))
147 char ch = buffer[0];
148 private_color_map = (ch == 'y' || ch == 'Y' || ch == 't' || ch == 'T' || ch == '1');
150 RegCloseKey(hkey);
153 wine_tsx11_lock();
154 if (private_color_map)
156 XSetWindowAttributes win_attr;
158 X11DRV_PALETTE_PaletteXColormap = XCreateColormap( gdi_display, root_window,
159 visual, AllocAll );
160 if (X11DRV_PALETTE_PaletteXColormap)
162 X11DRV_PALETTE_PaletteFlags |= (X11DRV_PALETTE_PRIVATE | X11DRV_PALETTE_WHITESET);
164 monoPlane = 1;
165 for( white = palette_size - 1; !(white & 1); white >>= 1 )
166 monoPlane++;
168 if( root_window != DefaultRootWindow(gdi_display) )
170 win_attr.colormap = X11DRV_PALETTE_PaletteXColormap;
171 XChangeWindowAttributes( gdi_display, root_window, CWColormap, &win_attr );
173 break;
176 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
177 visual, AllocNone);
178 wine_tsx11_unlock();
179 break;
182 case StaticGray:
183 wine_tsx11_lock();
184 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
185 visual, AllocNone);
186 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
187 X11DRV_PALETTE_Graymax = (1 << screen_depth)-1;
188 wine_tsx11_unlock();
189 break;
191 case TrueColor:
192 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
193 case StaticColor: {
194 int *depths,nrofdepths;
195 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
196 * depths 1 and 4
198 wine_tsx11_lock();
199 depths = XListDepths(gdi_display,DefaultScreen(gdi_display),&nrofdepths);
200 if ((nrofdepths==2) && ((depths[0]==4) || depths[1]==4)) {
201 monoPlane = 1;
202 for( white = palette_size - 1; !(white & 1); white >>= 1 )
203 monoPlane++;
204 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
205 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
206 visual, AllocNone);
208 else
210 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
211 visual, AllocNone);
212 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
213 X11DRV_PALETTE_ComputeShifts(visual->red_mask, &X11DRV_PALETTE_PRed, &X11DRV_PALETTE_LRed);
214 X11DRV_PALETTE_ComputeShifts(visual->green_mask, &X11DRV_PALETTE_PGreen, &X11DRV_PALETTE_LGreen);
215 X11DRV_PALETTE_ComputeShifts(visual->blue_mask, &X11DRV_PALETTE_PBlue, &X11DRV_PALETTE_LBlue);
217 XFree(depths);
218 wine_tsx11_unlock();
219 break;
223 TRACE(" visual class %i (%i)\n", visual->class, monoPlane);
225 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, sys_pal_template );
227 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
229 palette_size = 0;
231 else
233 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
234 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template );
235 else
236 X11DRV_PALETTE_BuildSharedMap( sys_pal_template );
238 /* Build free list */
240 if( X11DRV_PALETTE_firstFree != -1 )
241 X11DRV_PALETTE_FormatSystemPalette();
243 X11DRV_PALETTE_FillDefaultColors( sys_pal_template );
244 palette_size = visual->map_entries;
247 return palette_size;
250 /***********************************************************************
251 * X11DRV_PALETTE_Cleanup
253 * Free external colors we grabbed in the FillDefaultPalette()
255 void X11DRV_PALETTE_Cleanup(void)
257 if( COLOR_gapFilled )
259 wine_tsx11_lock();
260 XFreeColors(gdi_display, X11DRV_PALETTE_PaletteXColormap,
261 (unsigned long*)(X11DRV_PALETTE_PaletteToXPixel + COLOR_gapStart),
262 COLOR_gapFilled, 0);
263 wine_tsx11_unlock();
267 /***********************************************************************
268 * X11DRV_PALETTE_ComputeShifts
270 * Calculate conversion parameters for direct mapped visuals
272 static void X11DRV_PALETTE_ComputeShifts(unsigned long maskbits, ColorShifts *physical, ColorShifts *to_logical)
274 int i;
276 if (maskbits==0)
278 physical->shift=0;
279 physical->scale=0;
280 physical->max=0;
281 to_logical->shift=0;
282 to_logical->scale=0;
283 to_logical->max=0;
284 return;
287 for(i=0;!(maskbits&1);i++)
288 maskbits >>= 1;
290 physical->shift = i;
291 physical->max = maskbits;
293 for(i=0;maskbits!=0;i++)
294 maskbits >>= 1;
295 physical->scale = i;
297 if (physical->scale>8)
299 /* On FreeBSD, VNC's default 32bpp mode is bgrabb (ffc00000,3ff800,7ff)!
300 * So we adjust the shifts to also normalize the color fields to
301 * the Win32 standard of 8 bits per color.
303 to_logical->shift=physical->shift+(physical->scale-8);
304 to_logical->scale=8;
305 to_logical->max=0xff;
306 } else {
307 to_logical->shift=physical->shift;
308 to_logical->scale=physical->scale;
309 to_logical->max=physical->max;
313 /***********************************************************************
314 * X11DRV_PALETTE_BuildPrivateMap
316 * Allocate colorcells and initialize mapping tables.
318 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template )
320 /* Private colormap - identity mapping */
322 XColor color;
323 int i;
325 if((COLOR_sysPal = (PALETTEENTRY*)HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY)*palette_size)) == NULL) {
326 WARN("Can not allocate system palette\n");
327 return FALSE;
330 TRACE("Building private map - %i palette entries\n", palette_size);
332 /* Allocate system palette colors */
334 wine_tsx11_lock();
335 for( i=0; i < palette_size; i++ )
337 if( i < NB_RESERVED_COLORS/2 )
339 color.red = sys_pal_template[i].peRed * 65535 / 255;
340 color.green = sys_pal_template[i].peGreen * 65535 / 255;
341 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
342 COLOR_sysPal[i] = sys_pal_template[i];
343 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
345 else if( i >= palette_size - NB_RESERVED_COLORS/2 )
347 int j = NB_RESERVED_COLORS + i - palette_size;
348 color.red = sys_pal_template[j].peRed * 65535 / 255;
349 color.green = sys_pal_template[j].peGreen * 65535 / 255;
350 color.blue = sys_pal_template[j].peBlue * 65535 / 255;
351 COLOR_sysPal[i] = sys_pal_template[j];
352 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
355 color.flags = DoRed | DoGreen | DoBlue;
356 color.pixel = i;
357 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
359 /* Set EGA mapping if color is from the first or last eight */
361 if (i < 8)
362 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
363 else if (i >= palette_size - 8 )
364 X11DRV_PALETTE_mapEGAPixel[i - (palette_size - 16)] = color.pixel;
366 wine_tsx11_unlock();
368 X11DRV_PALETTE_XPixelToPalette = X11DRV_PALETTE_PaletteToXPixel = NULL;
370 COLOR_gapStart = 256; COLOR_gapEnd = -1;
372 X11DRV_PALETTE_firstFree = (palette_size > NB_RESERVED_COLORS)?NB_RESERVED_COLORS/2 : -1;
374 return FALSE;
377 /***********************************************************************
378 * X11DRV_PALETTE_BuildSharedMap
380 * Allocate colorcells and initialize mapping tables.
382 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template )
384 XColor color;
385 unsigned long sysPixel[NB_RESERVED_COLORS];
386 unsigned long* pixDynMapping = NULL;
387 unsigned long plane_masks[1];
388 int i, j, warn = 0;
389 int diff, r, g, b, max = 256, bp = 0, wp = 1;
390 int step = 1;
391 int defaultCM_max_copy;
392 Colormap defaultCM;
393 XColor defaultColors[256];
394 HKEY hkey;
396 defaultCM_max_copy = 128;
397 COLOR_max = 256;
399 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", &hkey))
401 char buffer[20];
402 DWORD type, count;
404 count = sizeof(buffer);
405 if(!RegQueryValueExA(hkey, "CopyDefaultColors", 0, &type, buffer, &count))
406 defaultCM_max_copy = atoi(buffer);
408 count = sizeof(buffer);
409 if(!RegQueryValueExA(hkey, "AllocSystemColors", 0, &type, buffer, &count))
410 COLOR_max = atoi(buffer);
412 RegCloseKey(hkey);
415 /* Copy the first bunch of colors out of the default colormap to prevent
416 * colormap flashing as much as possible. We're likely to get the most
417 * important Window Manager colors, etc in the first 128 colors */
418 defaultCM = DefaultColormap( gdi_display, DefaultScreen(gdi_display) );
420 for (i = 0; i < defaultCM_max_copy; i++)
421 defaultColors[i].pixel = (long) i;
422 wine_tsx11_lock();
423 XQueryColors(gdi_display, defaultCM, &defaultColors[0], defaultCM_max_copy);
424 for (i = 0; i < defaultCM_max_copy; i++)
425 XAllocColor( gdi_display, X11DRV_PALETTE_PaletteXColormap, &defaultColors[i] );
427 if (COLOR_max > 256) COLOR_max = 256;
428 else if (COLOR_max < 20) COLOR_max = 20;
429 TRACE("%d colors configured.\n", COLOR_max);
431 TRACE("Building shared map - %i palette entries\n", palette_size);
433 /* Be nice and allocate system colors as read-only */
435 for( i = 0; i < NB_RESERVED_COLORS; i++ )
437 color.red = sys_pal_template[i].peRed * 65535 / 255;
438 color.green = sys_pal_template[i].peGreen * 65535 / 255;
439 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
440 color.flags = DoRed | DoGreen | DoBlue;
442 if (!XAllocColor( gdi_display, X11DRV_PALETTE_PaletteXColormap, &color ))
444 XColor best, c;
446 if( !warn++ )
448 WARN("Not enough colors for the full system palette.\n");
450 bp = BlackPixel(gdi_display, DefaultScreen(gdi_display));
451 wp = WhitePixel(gdi_display, DefaultScreen(gdi_display));
453 max = (0xffffffff)>>(32 - screen_depth);
454 if( max > 256 )
456 step = max/256;
457 max = 256;
461 /* reinit color (XAllocColor() may change it)
462 * and map to the best shared colorcell */
464 color.red = sys_pal_template[i].peRed * 65535 / 255;
465 color.green = sys_pal_template[i].peGreen * 65535 / 255;
466 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
468 best.pixel = best.red = best.green = best.blue = 0;
469 for( c.pixel = 0, diff = 0x7fffffff; c.pixel < max; c.pixel += step )
471 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &c);
472 r = (c.red - color.red)>>8;
473 g = (c.green - color.green)>>8;
474 b = (c.blue - color.blue)>>8;
475 r = r*r + g*g + b*b;
476 if( r < diff ) { best = c; diff = r; }
479 if( XAllocColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &best) )
480 color.pixel = best.pixel;
481 else color.pixel = (i < NB_RESERVED_COLORS/2)? bp : wp;
484 sysPixel[i] = color.pixel;
486 TRACE("syscolor(%lx) -> pixel %i\n",
487 *(COLORREF*)(sys_pal_template+i), (int)color.pixel);
489 /* Set EGA mapping if color in the first or last eight */
491 if (i < 8)
492 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
493 else if (i >= NB_RESERVED_COLORS - 8 )
494 X11DRV_PALETTE_mapEGAPixel[i - (NB_RESERVED_COLORS-16)] = color.pixel;
496 wine_tsx11_unlock();
498 /* now allocate changeable set */
500 if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
502 int c_min = 0, c_max = palette_size, c_val;
504 TRACE("Dynamic colormap... \n");
506 /* let's become the first client that actually follows
507 * X guidelines and does binary search...
510 if((pixDynMapping = (unsigned long*)HeapAlloc(GetProcessHeap(), 0, sizeof(long)*palette_size)) == NULL) {
511 WARN("Out of memory while building system palette.\n");
512 return FALSE;
515 wine_tsx11_lock();
516 /* comment this out if you want to debug palette init */
517 XGrabServer(gdi_display);
519 while( c_max - c_min > 0 )
521 c_val = (c_max + c_min)/2 + (c_max + c_min)%2;
523 if( !XAllocColorCells(gdi_display, X11DRV_PALETTE_PaletteXColormap, False,
524 plane_masks, 0, pixDynMapping, c_val) )
525 c_max = c_val - 1;
526 else
528 XFreeColors(gdi_display, X11DRV_PALETTE_PaletteXColormap, pixDynMapping, c_val, 0);
529 c_min = c_val;
533 if( c_min > COLOR_max - NB_RESERVED_COLORS)
534 c_min = COLOR_max - NB_RESERVED_COLORS;
536 c_min = (c_min/2) + (c_min/2); /* need even set for split palette */
538 if( c_min > 0 )
539 if( !XAllocColorCells(gdi_display, X11DRV_PALETTE_PaletteXColormap, False,
540 plane_masks, 0, pixDynMapping, c_min) )
542 WARN("Inexplicable failure during colorcell allocation.\n");
543 c_min = 0;
546 palette_size = c_min + NB_RESERVED_COLORS;
548 XUngrabServer(gdi_display);
549 wine_tsx11_unlock();
551 TRACE("adjusted size %i colorcells\n", palette_size);
553 else if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
555 /* virtual colorspace - ToPhysical takes care of
556 * color translations but we have to allocate full palette
557 * to maintain compatibility
559 palette_size = 256;
560 TRACE("Virtual colorspace - screendepth %i\n", screen_depth);
562 else palette_size = NB_RESERVED_COLORS; /* system palette only - however we can alloc a bunch
563 * of colors and map to them */
565 TRACE("Shared system palette uses %i colors.\n", palette_size);
567 /* set gap to account for pixel shortage. It has to be right in the center
568 * of the system palette because otherwise raster ops get screwed. */
570 if( palette_size >= 256 )
571 { COLOR_gapStart = 256; COLOR_gapEnd = -1; }
572 else
573 { COLOR_gapStart = palette_size/2; COLOR_gapEnd = 255 - palette_size/2; }
575 X11DRV_PALETTE_firstFree = ( palette_size > NB_RESERVED_COLORS &&
576 (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL || !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED)) )
577 ? NB_RESERVED_COLORS/2 : -1;
579 COLOR_sysPal = (PALETTEENTRY*)HeapAlloc(GetProcessHeap(),0,sizeof(PALETTEENTRY)*256);
580 if(COLOR_sysPal == NULL) {
581 ERR("Can not allocate system palette!\n");
582 return FALSE;
585 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
587 if (screen_depth <= 8)
589 X11DRV_PALETTE_XPixelToPalette = HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(int) );
590 if(X11DRV_PALETTE_XPixelToPalette == NULL) {
591 ERR("Out of memory: XPixelToPalette!\n");
592 return FALSE;
594 for( i = 0; i < 256; i++ )
595 X11DRV_PALETTE_XPixelToPalette[i] = NB_PALETTE_EMPTY_VALUE;
598 /* for hicolor visuals PaletteToPixel mapping is used to skip
599 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
602 X11DRV_PALETTE_PaletteToXPixel = (int*)HeapAlloc(GetProcessHeap(),0,sizeof(int)*256);
603 if(X11DRV_PALETTE_PaletteToXPixel == NULL) {
604 ERR("Out of memory: PaletteToXPixel!\n");
605 return FALSE;
608 for( i = j = 0; i < 256; i++ )
610 if( i >= COLOR_gapStart && i <= COLOR_gapEnd )
612 X11DRV_PALETTE_PaletteToXPixel[i] = NB_PALETTE_EMPTY_VALUE;
613 COLOR_sysPal[i].peFlags = 0; /* mark as unused */
614 continue;
617 if( i < NB_RESERVED_COLORS/2 )
619 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[i];
620 COLOR_sysPal[i] = sys_pal_template[i];
621 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
623 else if( i >= 256 - NB_RESERVED_COLORS/2 )
625 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[(i + NB_RESERVED_COLORS) - 256];
626 COLOR_sysPal[i] = sys_pal_template[(i + NB_RESERVED_COLORS) - 256];
627 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
629 else if( pixDynMapping )
630 X11DRV_PALETTE_PaletteToXPixel[i] = pixDynMapping[j++];
631 else
632 X11DRV_PALETTE_PaletteToXPixel[i] = i;
634 TRACE("index %i -> pixel %i\n", i, X11DRV_PALETTE_PaletteToXPixel[i]);
636 if( X11DRV_PALETTE_XPixelToPalette )
637 X11DRV_PALETTE_XPixelToPalette[X11DRV_PALETTE_PaletteToXPixel[i]] = i;
640 if( pixDynMapping ) HeapFree(GetProcessHeap(), 0, pixDynMapping);
642 return TRUE;
645 /***********************************************************************
646 * Colormap Initialization
648 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template )
650 /* initialize unused entries to what Windows uses as a color
651 * cube - based on Greg Kreider's code.
654 int i = 0, idx = 0;
655 int red, no_r, inc_r;
656 int green, no_g, inc_g;
657 int blue, no_b, inc_b;
659 if (palette_size <= NB_RESERVED_COLORS)
660 return;
661 while (i*i*i < (palette_size - NB_RESERVED_COLORS)) i++;
662 no_r = no_g = no_b = --i;
663 if ((no_r * (no_g+1) * no_b) < (palette_size - NB_RESERVED_COLORS)) no_g++;
664 if ((no_r * no_g * (no_b+1)) < (palette_size - NB_RESERVED_COLORS)) no_b++;
665 inc_r = (255 - NB_COLORCUBE_START_INDEX)/no_r;
666 inc_g = (255 - NB_COLORCUBE_START_INDEX)/no_g;
667 inc_b = (255 - NB_COLORCUBE_START_INDEX)/no_b;
669 wine_tsx11_lock();
671 idx = X11DRV_PALETTE_firstFree;
673 if( idx != -1 )
674 for (blue = NB_COLORCUBE_START_INDEX; blue < 256 && idx; blue += inc_b )
675 for (green = NB_COLORCUBE_START_INDEX; green < 256 && idx; green += inc_g )
676 for (red = NB_COLORCUBE_START_INDEX; red < 256 && idx; red += inc_r )
678 /* weird but true */
680 if( red == NB_COLORCUBE_START_INDEX && green == red && blue == green ) continue;
682 COLOR_sysPal[idx].peRed = red;
683 COLOR_sysPal[idx].peGreen = green;
684 COLOR_sysPal[idx].peBlue = blue;
686 /* set X color */
688 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
690 if (X11DRV_PALETTE_PRed.max != 255) no_r = (red * X11DRV_PALETTE_PRed.max) / 255;
691 if (X11DRV_PALETTE_PGreen.max != 255) no_g = (green * X11DRV_PALETTE_PGreen.max) / 255;
692 if (X11DRV_PALETTE_PBlue.max != 255) no_b = (blue * X11DRV_PALETTE_PBlue.max) / 255;
694 X11DRV_PALETTE_PaletteToXPixel[idx] = (no_r << X11DRV_PALETTE_PRed.shift) | (no_g << X11DRV_PALETTE_PGreen.shift) | (no_b << X11DRV_PALETTE_PBlue.shift);
696 else if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
698 XColor color;
699 color.pixel = (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[idx] : idx;
700 color.red = COLOR_sysPal[idx].peRed << 8;
701 color.green = COLOR_sysPal[idx].peGreen << 8;
702 color.blue = COLOR_sysPal[idx].peBlue << 8;
703 color.flags = DoRed | DoGreen | DoBlue;
704 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
706 idx = X11DRV_PALETTE_freeList[idx];
709 /* try to fill some entries in the "gap" with
710 * what's already in the colormap - they will be
711 * mappable to but not changeable. */
713 if( COLOR_gapStart < COLOR_gapEnd && X11DRV_PALETTE_XPixelToPalette )
715 XColor xc;
716 int r, g, b, max;
718 max = COLOR_max - (256 - (COLOR_gapEnd - COLOR_gapStart));
719 for ( i = 0, idx = COLOR_gapStart; i < 256 && idx <= COLOR_gapEnd; i++ )
720 if( X11DRV_PALETTE_XPixelToPalette[i] == NB_PALETTE_EMPTY_VALUE )
722 xc.pixel = i;
724 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &xc);
725 r = xc.red>>8; g = xc.green>>8; b = xc.blue>>8;
727 if( xc.pixel < 256 && X11DRV_PALETTE_CheckSysColor( sys_pal_template, RGB(r, g, b)) &&
728 XAllocColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &xc) )
730 X11DRV_PALETTE_XPixelToPalette[xc.pixel] = idx;
731 X11DRV_PALETTE_PaletteToXPixel[idx] = xc.pixel;
732 *(COLORREF*)(COLOR_sysPal + idx) = RGB(r, g, b);
733 COLOR_sysPal[idx++].peFlags |= PC_SYS_USED;
734 if( --max <= 0 ) break;
737 COLOR_gapFilled = idx - COLOR_gapStart;
739 wine_tsx11_unlock();
743 /***********************************************************************
744 * X11DRV_IsSolidColor
746 * Check whether 'color' can be represented with a solid color.
748 BOOL X11DRV_IsSolidColor( COLORREF color )
750 int i;
751 const PALETTEENTRY *pEntry = COLOR_sysPal;
753 if (color & 0xff000000) return TRUE; /* indexed color */
755 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
757 for (i = 0; i < 256 ; i++, pEntry++)
759 if( i < COLOR_gapStart || i > COLOR_gapEnd )
760 if ((GetRValue(color) == pEntry->peRed) &&
761 (GetGValue(color) == pEntry->peGreen) &&
762 (GetBValue(color) == pEntry->peBlue)) return TRUE;
764 return FALSE;
768 /***********************************************************************
769 * X11DRV_PALETTE_ToLogical
771 * Return RGB color for given X pixel.
773 COLORREF X11DRV_PALETTE_ToLogical(int pixel)
775 XColor color;
777 #if 0
778 /* truecolor visual */
780 if (screen_depth >= 24) return pixel;
781 #endif
783 /* check for hicolor visuals first */
785 if ( (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) && !X11DRV_PALETTE_Graymax )
787 color.red = (pixel >> X11DRV_PALETTE_LRed.shift) & X11DRV_PALETTE_LRed.max;
788 if (X11DRV_PALETTE_LRed.scale<8)
789 color.red= color.red << (8-X11DRV_PALETTE_LRed.scale) |
790 color.red >> (2*X11DRV_PALETTE_LRed.scale-8);
791 color.green = (pixel >> X11DRV_PALETTE_LGreen.shift) & X11DRV_PALETTE_LGreen.max;
792 if (X11DRV_PALETTE_LGreen.scale<8)
793 color.green=color.green << (8-X11DRV_PALETTE_LGreen.scale) |
794 color.green >> (2*X11DRV_PALETTE_LGreen.scale-8);
795 color.blue = (pixel >> X11DRV_PALETTE_LBlue.shift) & X11DRV_PALETTE_LBlue.max;
796 if (X11DRV_PALETTE_LBlue.scale<8)
797 color.blue= color.blue << (8-X11DRV_PALETTE_LBlue.scale) |
798 color.blue >> (2*X11DRV_PALETTE_LBlue.scale-8);
799 return RGB(color.red,color.green,color.blue);
802 /* check if we can bypass X */
804 if ((screen_depth <= 8) && (pixel < 256) &&
805 !(X11DRV_PALETTE_PaletteFlags & (X11DRV_PALETTE_VIRTUAL | X11DRV_PALETTE_FIXED)) ) {
806 return ( *(COLORREF*)(COLOR_sysPal +
807 ((X11DRV_PALETTE_XPixelToPalette)?X11DRV_PALETTE_XPixelToPalette[pixel]:pixel)) ) & 0x00ffffff;
810 wine_tsx11_lock();
811 color.pixel = pixel;
812 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
813 wine_tsx11_unlock();
814 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
818 /***********************************************************************
819 * X11DRV_SysPaletteLookupPixel
821 static int X11DRV_SysPaletteLookupPixel( COLORREF col, BOOL skipReserved )
823 int i, best = 0, diff = 0x7fffffff;
824 int r,g,b;
826 for( i = 0; i < palette_size && diff ; i++ )
828 if( !(COLOR_sysPal[i].peFlags & PC_SYS_USED) ||
829 (skipReserved && COLOR_sysPal[i].peFlags & PC_SYS_RESERVED) )
830 continue;
832 r = COLOR_sysPal[i].peRed - GetRValue(col);
833 g = COLOR_sysPal[i].peGreen - GetGValue(col);
834 b = COLOR_sysPal[i].peBlue - GetBValue(col);
836 r = r*r + g*g + b*b;
838 if( r < diff ) { best = i; diff = r; }
840 return best;
844 /***********************************************************************
845 * X11DRV_PALETTE_ToPhysical
847 * Return the physical color closest to 'color'.
849 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
851 WORD index = 0;
852 HPALETTE hPal = physDev ? GetCurrentObject(physDev->hdc, OBJ_PAL ) : GetStockObject(DEFAULT_PALETTE);
853 unsigned char spec_type = color >> 24;
854 PALETTEOBJ* palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
856 /* palPtr can be NULL when DC is being destroyed */
857 if( !palPtr ) return 0;
859 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
861 /* there is no colormap limitation; we are going to have to compute
862 * the pixel value from the visual information stored earlier
865 unsigned long red, green, blue;
866 unsigned idx = 0;
868 switch(spec_type)
870 case 1: /* PALETTEINDEX */
872 if( (idx = color & 0xffff) >= palPtr->logpalette.palNumEntries)
874 WARN("RGB(%lx) : idx %d is out of bounds, assuming black\n", color, idx);
875 GDI_ReleaseObj( hPal );
876 return 0;
879 if( palPtr->mapping )
881 int ret = palPtr->mapping[idx];
882 GDI_ReleaseObj( hPal );
883 return ret;
885 color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
886 break;
888 default:
889 color &= 0xffffff;
890 /* fall through to RGB */
892 case 0: /* RGB */
893 if (physDev && (physDev->depth == 1) )
895 GDI_ReleaseObj( hPal );
896 return (((color >> 16) & 0xff) +
897 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
902 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
904 if (X11DRV_PALETTE_Graymax)
906 /* grayscale only; return scaled value */
907 GDI_ReleaseObj( hPal );
908 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
910 else
912 /* scale each individually and construct the TrueColor pixel value */
913 if (X11DRV_PALETTE_PRed.scale < 8)
914 red = red >> (8-X11DRV_PALETTE_PRed.scale);
915 else if (X11DRV_PALETTE_PRed.scale > 8)
916 red = red << (X11DRV_PALETTE_PRed.scale-8) |
917 red >> (16-X11DRV_PALETTE_PRed.scale);
918 if (X11DRV_PALETTE_PGreen.scale < 8)
919 green = green >> (8-X11DRV_PALETTE_PGreen.scale);
920 else if (X11DRV_PALETTE_PGreen.scale > 8)
921 green = green << (X11DRV_PALETTE_PGreen.scale-8) |
922 green >> (16-X11DRV_PALETTE_PGreen.scale);
923 if (X11DRV_PALETTE_PBlue.scale < 8)
924 blue = blue >> (8-X11DRV_PALETTE_PBlue.scale);
925 else if (X11DRV_PALETTE_PBlue.scale > 8)
926 blue = blue << (X11DRV_PALETTE_PBlue.scale-8) |
927 blue >> (16-X11DRV_PALETTE_PBlue.scale);
929 GDI_ReleaseObj( hPal );
930 return (red << X11DRV_PALETTE_PRed.shift) | (green << X11DRV_PALETTE_PGreen.shift) | (blue << X11DRV_PALETTE_PBlue.shift);
933 else
936 if( !palPtr->mapping )
937 WARN("Palette %p is not realized\n", hPal);
939 switch(spec_type) /* we have to peruse DC and system palette */
941 default:
942 color &= 0xffffff;
943 /* fall through to RGB */
945 case 0: /* RGB */
946 if (physDev && (physDev->depth == 1) )
948 GDI_ReleaseObj( hPal );
949 return (((color >> 16) & 0xff) +
950 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
953 index = X11DRV_SysPaletteLookupPixel( color, FALSE);
954 if (X11DRV_PALETTE_PaletteToXPixel) index = X11DRV_PALETTE_PaletteToXPixel[index];
956 /* TRACE(palette,"RGB(%lx) -> pixel %i\n", color, index);
958 break;
959 case 1: /* PALETTEINDEX */
960 index = color & 0xffff;
962 if( index >= palPtr->logpalette.palNumEntries )
963 WARN("RGB(%lx) : index %i is out of bounds\n", color, index);
964 else if( palPtr->mapping ) index = palPtr->mapping[index];
966 /* TRACE(palette,"PALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
968 break;
969 case 2: /* PALETTERGB */
970 index = GetNearestPaletteIndex( hPal, color );
971 if (palPtr->mapping) index = palPtr->mapping[index];
972 /* TRACE(palette,"PALETTERGB(%lx) -> pixel %i\n", color, index);
974 break;
978 GDI_ReleaseObj( hPal );
979 return index;
982 /***********************************************************************
983 * X11DRV_PALETTE_LookupSystemXPixel
985 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col)
987 int i, best = 0, diff = 0x7fffffff;
988 int size = palette_size;
989 int r,g,b;
991 for( i = 0; i < size && diff ; i++ )
993 if( i == NB_RESERVED_COLORS/2 )
995 int newi = size - NB_RESERVED_COLORS/2;
996 if (newi>i) i=newi;
999 r = COLOR_sysPal[i].peRed - GetRValue(col);
1000 g = COLOR_sysPal[i].peGreen - GetGValue(col);
1001 b = COLOR_sysPal[i].peBlue - GetBValue(col);
1003 r = r*r + g*g + b*b;
1005 if( r < diff ) { best = i; diff = r; }
1008 return (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[best] : best;
1011 /***********************************************************************
1012 * X11DRV_PALETTE_FormatSystemPalette
1014 static void X11DRV_PALETTE_FormatSystemPalette(void)
1016 /* Build free list so we'd have an easy way to find
1017 * out if there are any available colorcells.
1020 int i, j = X11DRV_PALETTE_firstFree = NB_RESERVED_COLORS/2;
1022 COLOR_sysPal[j].peFlags = 0;
1023 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
1024 if( i < COLOR_gapStart || i > COLOR_gapEnd )
1026 COLOR_sysPal[i].peFlags = 0; /* unused tag */
1027 X11DRV_PALETTE_freeList[j] = i; /* next */
1028 j = i;
1030 X11DRV_PALETTE_freeList[j] = 0;
1033 /***********************************************************************
1034 * X11DRV_PALETTE_CheckSysColor
1036 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c)
1038 int i;
1039 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1040 if( c == (*(COLORREF*)(sys_pal_template + i) & 0x00ffffff) )
1041 return 0;
1042 return 1;
1046 /***********************************************************************
1047 * X11DRV_LookupSysPaletteExact
1049 static int X11DRV_LookupSysPaletteExact( COLORREF col )
1051 int i;
1052 BYTE r = GetRValue(col), g = GetGValue(col), b = GetBValue(col);
1053 for( i = 0; i < palette_size; i++ )
1055 if( COLOR_sysPal[i].peFlags & PC_SYS_USED ) /* skips gap */
1056 if( COLOR_sysPal[i].peRed == r &&
1057 COLOR_sysPal[i].peGreen == g &&
1058 COLOR_sysPal[i].peBlue == b )
1059 return i;
1061 return -1;
1065 /***********************************************************************
1066 * X11DRV_PALETTE_SetMapping
1068 * Set the color-mapping table for selected palette.
1069 * Return number of entries which mapping has changed.
1071 static UINT X11DRV_PALETTE_SetMapping( PALETTEOBJ* palPtr, UINT uStart, UINT uNum, BOOL mapOnly )
1073 char flag;
1074 int prevMapping = (palPtr->mapping) ? 1 : 0;
1075 int index;
1076 UINT iRemapped = 0;
1077 int* mapping;
1079 /* reset dynamic system palette entries */
1081 if( !mapOnly && X11DRV_PALETTE_firstFree != -1)
1082 X11DRV_PALETTE_FormatSystemPalette();
1084 /* initialize palette mapping table */
1085 if (palPtr->mapping)
1086 mapping = HeapReAlloc( GetProcessHeap(), 0, palPtr->mapping,
1087 sizeof(int)*palPtr->logpalette.palNumEntries);
1088 else
1089 mapping = HeapAlloc( GetProcessHeap(), 0,
1090 sizeof(int)*palPtr->logpalette.palNumEntries);
1092 if(mapping == NULL) {
1093 ERR("Can not allocate new mapping -- memory exausted!\n");
1094 return 0;
1096 palPtr->mapping = mapping;
1098 if (uStart >= palPtr->logpalette.palNumEntries) return 0;
1100 if (uStart + uNum > palPtr->logpalette.palNumEntries)
1101 uNum = palPtr->logpalette.palNumEntries - uStart;
1103 for( uNum += uStart; uStart < uNum; uStart++ )
1105 index = -1;
1106 flag = PC_SYS_USED;
1108 switch( palPtr->logpalette.palPalEntry[uStart].peFlags & 0x07 )
1110 case PC_EXPLICIT: /* palette entries are indices into system palette */
1111 index = *(WORD*)(palPtr->logpalette.palPalEntry + uStart);
1112 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
1114 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
1115 index = 0;
1117 break;
1119 case PC_RESERVED: /* forbid future mappings to this entry */
1120 flag |= PC_SYS_RESERVED;
1122 /* fall through */
1123 default: /* try to collapse identical colors */
1124 index = X11DRV_LookupSysPaletteExact(*(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
1125 /* fall through */
1126 case PC_NOCOLLAPSE:
1127 if( index < 0 )
1129 if( X11DRV_PALETTE_firstFree > 0 && !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
1131 XColor color;
1132 index = X11DRV_PALETTE_firstFree; /* ought to be available */
1133 X11DRV_PALETTE_firstFree = X11DRV_PALETTE_freeList[index];
1135 color.pixel = (X11DRV_PALETTE_PaletteToXPixel) ? X11DRV_PALETTE_PaletteToXPixel[index] : index;
1136 color.red = palPtr->logpalette.palPalEntry[uStart].peRed << 8;
1137 color.green = palPtr->logpalette.palPalEntry[uStart].peGreen << 8;
1138 color.blue = palPtr->logpalette.palPalEntry[uStart].peBlue << 8;
1139 color.flags = DoRed | DoGreen | DoBlue;
1140 wine_tsx11_lock();
1141 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
1142 wine_tsx11_unlock();
1144 COLOR_sysPal[index] = palPtr->logpalette.palPalEntry[uStart];
1145 COLOR_sysPal[index].peFlags = flag;
1146 X11DRV_PALETTE_freeList[index] = 0;
1148 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1149 break;
1151 else if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
1153 index = X11DRV_PALETTE_ToPhysical( NULL, 0x00ffffff &
1154 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
1155 break;
1158 /* we have to map to existing entry in the system palette */
1160 index = X11DRV_SysPaletteLookupPixel( *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), TRUE);
1162 palPtr->logpalette.palPalEntry[uStart].peFlags |= PC_SYS_USED;
1164 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1165 break;
1168 if( !prevMapping || palPtr->mapping[uStart] != index ) iRemapped++;
1169 palPtr->mapping[uStart] = index;
1171 TRACE("entry %i (%lx) -> pixel %i\n", uStart,
1172 *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), index);
1175 return iRemapped;
1178 /***********************************************************************
1179 * GetSystemPaletteEntries (X11DRV.@)
1181 UINT X11DRV_GetSystemPaletteEntries( X11DRV_PDEVICE *physDev, UINT start, UINT count,
1182 LPPALETTEENTRY entries )
1184 UINT i;
1186 if (!entries) return palette_size;
1187 if (start >= palette_size) return 0;
1188 if (start + count >= palette_size) count = palette_size - start;
1190 for (i = 0; i < count; i++)
1192 entries[i].peRed = COLOR_sysPal[start + i].peRed;
1193 entries[i].peGreen = COLOR_sysPal[start + i].peGreen;
1194 entries[i].peBlue = COLOR_sysPal[start + i].peBlue;
1195 entries[i].peFlags = 0;
1196 TRACE("\tidx(%02x) -> RGB(%08lx)\n", start + i, *(COLORREF*)(entries + i) );
1198 return count;
1202 /***********************************************************************
1203 * GetNearestColor (X11DRV.@)
1205 COLORREF X11DRV_GetNearestColor( X11DRV_PDEVICE *physDev, COLORREF color )
1207 unsigned char spec_type = color >> 24;
1208 COLORREF nearest;
1210 if (!palette_size) return color;
1212 if (spec_type == 1 || spec_type == 2)
1214 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1216 UINT index;
1217 PALETTEENTRY entry;
1218 HPALETTE hpal = GetCurrentObject( physDev->hdc, OBJ_PAL );
1220 if (!hpal) hpal = GetStockObject( DEFAULT_PALETTE );
1222 if (spec_type == 2) /* PALETTERGB */
1223 index = GetNearestPaletteIndex( hpal, color );
1224 else /* PALETTEINDEX */
1225 index = LOWORD(color);
1227 if (!GetPaletteEntries( hpal, index, 1, &entry ))
1229 WARN("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, index );
1230 if (!GetPaletteEntries( hpal, 0, 1, &entry )) return CLR_INVALID;
1232 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
1234 color &= 0x00ffffff;
1235 nearest = (0x00ffffff & *(COLORREF*)(COLOR_sysPal + X11DRV_SysPaletteLookupPixel(color, FALSE)));
1237 TRACE("(%06lx): returning %06lx\n", color, nearest );
1238 return nearest;
1242 /***********************************************************************
1243 * RealizePalette (X11DRV.@)
1245 UINT X11DRV_RealizePalette( X11DRV_PDEVICE *physDev, HPALETTE hpal, BOOL primary )
1247 UINT ret;
1248 PALETTEOBJ *palPtr;
1250 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return 0;
1252 if (!(palPtr = GDI_GetObjPtr( hpal, PALETTE_MAGIC ))) return 0;
1253 ret = X11DRV_PALETTE_SetMapping( palPtr, 0, palPtr->logpalette.palNumEntries, !primary );
1254 GDI_ReleaseObj( hpal );
1255 return ret;
1259 /***********************************************************************
1260 * RealizeDefaultPalette (X11DRV.@)
1262 UINT X11DRV_RealizeDefaultPalette( X11DRV_PDEVICE *physDev )
1264 UINT ret = 0;
1266 if (palette_size && GetObjectType(physDev->hdc) != OBJ_MEMDC)
1268 PALETTEOBJ* palPtr = GDI_GetObjPtr( GetStockObject(DEFAULT_PALETTE), PALETTE_MAGIC );
1269 if (palPtr)
1271 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1272 int i, index;
1274 for( i = 0; i < 20; i++ )
1276 index = X11DRV_PALETTE_LookupSystemXPixel(*(COLORREF*)(palPtr->logpalette.palPalEntry + i));
1277 /* mapping is allocated in COLOR_InitPalette() */
1278 if( index != palPtr->mapping[i] )
1280 palPtr->mapping[i]=index;
1281 ret++;
1284 GDI_ReleaseObj( GetStockObject(DEFAULT_PALETTE) );
1287 return ret;