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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(palette);
34 /* Palette indexed mode:
35 * logical palette -> mapping -> pixel
38 * Windows needs contiguous color space ( from 0 to n ) but
39 * it is possible only with the private colormap. Otherwise we
40 * have to map DC palette indices to real pixel values. With
41 * private colormaps it boils down to the identity mapping. The
42 * other special case is when we have a fixed color visual with
43 * the screendepth > 8 - we abandon palette mappings altogether
44 * because pixel values can be calculated without X server
48 #define NB_RESERVED_COLORS 20 /* number of fixed colors in system palette */
50 #define PC_SYS_USED 0x80 /* palentry is used (both system and logical) */
51 #define PC_SYS_RESERVED 0x40 /* system palentry is not to be mapped to */
53 static PALETTEENTRY *COLOR_sysPal; /* current system palette */
55 static int COLOR_gapStart = 256;
56 static int COLOR_gapEnd = -1;
57 static int COLOR_gapFilled = 0;
59 Colormap X11DRV_PALETTE_PaletteXColormap = 0;
60 UINT16 X11DRV_PALETTE_PaletteFlags = 0;
62 /* initialize to zero to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
63 ColorShifts X11DRV_PALETTE_default_shifts = { {0,0,0,}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} };
64 static int X11DRV_PALETTE_Graymax = 0;
66 static int palette_size;
68 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
69 static int X11DRV_PALETTE_firstFree = 0;
70 static unsigned char X11DRV_PALETTE_freeList[256];
72 static XContext palette_context; /* X context to associate a color mapping to a palette */
74 static CRITICAL_SECTION palette_cs;
75 static CRITICAL_SECTION_DEBUG critsect_debug =
78 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
79 0, 0, { (DWORD_PTR)(__FILE__ ": palette_cs") }
81 static CRITICAL_SECTION palette_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
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_FillDefaultColors( const PALETTEENTRY *sys_pal_template );
105 static void X11DRV_PALETTE_FormatSystemPalette(void);
106 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c);
107 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col);
110 /***********************************************************************
111 * palette_get_mapping
113 static int *palette_get_mapping( HPALETTE hpal )
118 if (XFindContext( gdi_display, (XID)hpal, palette_context, (char **)&mapping )) mapping = NULL;
124 /***********************************************************************
125 * palette_set_mapping
127 static void palette_set_mapping( HPALETTE hpal, int *mapping )
130 XSaveContext( gdi_display, (XID)hpal, palette_context, (char *)mapping );
135 /***********************************************************************
138 * Initialize color management.
140 int X11DRV_PALETTE_Init(void)
142 int mask, white, black;
145 PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS];
147 TRACE("initializing palette manager...\n");
150 palette_context = XUniqueContext();
152 white = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
153 black = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
155 for( mask = 1; !((white & mask)^(black & mask)); mask <<= 1 )
157 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
158 palette_size = visual->map_entries;
160 switch(visual->class)
163 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
167 if (private_color_map)
169 XSetWindowAttributes win_attr;
171 X11DRV_PALETTE_PaletteXColormap = XCreateColormap( gdi_display, root_window,
173 if (X11DRV_PALETTE_PaletteXColormap)
175 X11DRV_PALETTE_PaletteFlags |= (X11DRV_PALETTE_PRIVATE | X11DRV_PALETTE_WHITESET);
178 for( white = palette_size - 1; !(white & 1); white >>= 1 )
181 if( root_window != DefaultRootWindow(gdi_display) )
183 win_attr.colormap = X11DRV_PALETTE_PaletteXColormap;
184 XChangeWindowAttributes( gdi_display, root_window, CWColormap, &win_attr );
188 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
196 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
198 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
199 X11DRV_PALETTE_Graymax = (1 << screen_depth)-1;
204 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
206 int *depths,nrofdepths;
207 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
211 depths = XListDepths(gdi_display,DefaultScreen(gdi_display),&nrofdepths);
212 if ((nrofdepths==2) && ((depths[0]==4) || depths[1]==4)) {
214 for( white = palette_size - 1; !(white & 1); white >>= 1 )
216 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
217 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
222 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
224 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
225 X11DRV_PALETTE_ComputeColorShifts(&X11DRV_PALETTE_default_shifts, visual->red_mask, visual->green_mask, visual->blue_mask);
233 TRACE(" visual class %i (%i)\n", visual->class, monoPlane);
235 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, sys_pal_template );
237 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
243 if ((mapping = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int) * NB_RESERVED_COLORS )))
244 palette_set_mapping( GetStockObject(DEFAULT_PALETTE), mapping );
246 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
247 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template );
249 X11DRV_PALETTE_BuildSharedMap( sys_pal_template );
251 /* Build free list */
253 if( X11DRV_PALETTE_firstFree != -1 )
254 X11DRV_PALETTE_FormatSystemPalette();
256 X11DRV_PALETTE_FillDefaultColors( sys_pal_template );
257 palette_size = visual->map_entries;
263 /***********************************************************************
264 * X11DRV_PALETTE_Cleanup
266 * Free external colors we grabbed in the FillDefaultPalette()
268 void X11DRV_PALETTE_Cleanup(void)
270 if( COLOR_gapFilled )
273 XFreeColors(gdi_display, X11DRV_PALETTE_PaletteXColormap,
274 (unsigned long*)(X11DRV_PALETTE_PaletteToXPixel + COLOR_gapStart),
278 DeleteCriticalSection(&palette_cs);
281 /***********************************************************************
282 * X11DRV_PALETTE_ComputeChannelShift
284 * Calculate conversion parameters for a given color mask
286 static void X11DRV_PALETTE_ComputeChannelShift(unsigned long maskbits, ChannelShift *physical, ChannelShift *to_logical)
301 for(i=0;!(maskbits&1);i++)
305 physical->max = maskbits;
307 for(i=0;maskbits!=0;i++)
311 if (physical->scale>8)
313 /* On FreeBSD, VNC's default 32bpp mode is bgrabb (ffc00000,3ff800,7ff)!
314 * So we adjust the shifts to also normalize the color fields to
315 * the Win32 standard of 8 bits per color.
317 to_logical->shift=physical->shift+(physical->scale-8);
319 to_logical->max=0xff;
321 to_logical->shift=physical->shift;
322 to_logical->scale=physical->scale;
323 to_logical->max=physical->max;
327 /***********************************************************************
328 * X11DRV_PALETTE_ComputeColorShifts
330 * Calculate conversion parameters for a given color
332 void X11DRV_PALETTE_ComputeColorShifts(ColorShifts *shifts, unsigned long redMask, unsigned long greenMask, unsigned long blueMask)
334 X11DRV_PALETTE_ComputeChannelShift(redMask, &shifts->physicalRed, &shifts->logicalRed);
335 X11DRV_PALETTE_ComputeChannelShift(greenMask, &shifts->physicalGreen, &shifts->logicalGreen);
336 X11DRV_PALETTE_ComputeChannelShift(blueMask, &shifts->physicalBlue, &shifts->logicalBlue);
339 /***********************************************************************
340 * X11DRV_PALETTE_BuildPrivateMap
342 * Allocate colorcells and initialize mapping tables.
344 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template )
346 /* Private colormap - identity mapping */
351 if((COLOR_sysPal = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY)*palette_size)) == NULL) {
352 WARN("Unable to allocate the system palette\n");
356 TRACE("Building private map - %i palette entries\n", palette_size);
358 /* Allocate system palette colors */
361 for( i=0; i < palette_size; i++ )
363 if( i < NB_RESERVED_COLORS/2 )
365 color.red = sys_pal_template[i].peRed * 65535 / 255;
366 color.green = sys_pal_template[i].peGreen * 65535 / 255;
367 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
368 COLOR_sysPal[i] = sys_pal_template[i];
369 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
371 else if( i >= palette_size - NB_RESERVED_COLORS/2 )
373 int j = NB_RESERVED_COLORS + i - palette_size;
374 color.red = sys_pal_template[j].peRed * 65535 / 255;
375 color.green = sys_pal_template[j].peGreen * 65535 / 255;
376 color.blue = sys_pal_template[j].peBlue * 65535 / 255;
377 COLOR_sysPal[i] = sys_pal_template[j];
378 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
381 color.flags = DoRed | DoGreen | DoBlue;
383 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
385 /* Set EGA mapping if color is from the first or last eight */
388 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
389 else if (i >= palette_size - 8 )
390 X11DRV_PALETTE_mapEGAPixel[i - (palette_size - 16)] = color.pixel;
394 X11DRV_PALETTE_XPixelToPalette = X11DRV_PALETTE_PaletteToXPixel = NULL;
396 COLOR_gapStart = 256; COLOR_gapEnd = -1;
398 X11DRV_PALETTE_firstFree = (palette_size > NB_RESERVED_COLORS)?NB_RESERVED_COLORS/2 : -1;
403 /***********************************************************************
404 * X11DRV_PALETTE_BuildSharedMap
406 * Allocate colorcells and initialize mapping tables.
408 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template )
411 unsigned long sysPixel[NB_RESERVED_COLORS];
412 unsigned long* pixDynMapping = NULL;
413 unsigned long plane_masks[1];
415 int diff, r, g, b, bp = 0, wp = 1;
417 unsigned int max = 256;
419 XColor defaultColors[256];
421 /* Copy the first bunch of colors out of the default colormap to prevent
422 * colormap flashing as much as possible. We're likely to get the most
423 * important Window Manager colors, etc in the first 128 colors */
424 defaultCM = DefaultColormap( gdi_display, DefaultScreen(gdi_display) );
426 if (copy_default_colors > 256) copy_default_colors = 256;
427 for (i = 0; i < copy_default_colors; i++)
428 defaultColors[i].pixel = (long) i;
430 XQueryColors(gdi_display, defaultCM, &defaultColors[0], copy_default_colors);
431 for (i = 0; i < copy_default_colors; i++)
432 XAllocColor( gdi_display, X11DRV_PALETTE_PaletteXColormap, &defaultColors[i] );
434 if (alloc_system_colors > 256) alloc_system_colors = 256;
435 else if (alloc_system_colors < 20) alloc_system_colors = 20;
436 TRACE("%d colors configured.\n", alloc_system_colors);
438 TRACE("Building shared map - %i palette entries\n", palette_size);
440 /* Be nice and allocate system colors as read-only */
442 for( i = 0; i < NB_RESERVED_COLORS; i++ )
444 color.red = sys_pal_template[i].peRed * 65535 / 255;
445 color.green = sys_pal_template[i].peGreen * 65535 / 255;
446 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
447 color.flags = DoRed | DoGreen | DoBlue;
449 if (!XAllocColor( gdi_display, X11DRV_PALETTE_PaletteXColormap, &color ))
455 WARN("Not enough colors for the full system palette.\n");
457 bp = BlackPixel(gdi_display, DefaultScreen(gdi_display));
458 wp = WhitePixel(gdi_display, DefaultScreen(gdi_display));
460 max = (0xffffffff)>>(32 - screen_depth);
468 /* reinit color (XAllocColor() may change it)
469 * and map to the best shared colorcell */
471 color.red = sys_pal_template[i].peRed * 65535 / 255;
472 color.green = sys_pal_template[i].peGreen * 65535 / 255;
473 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
475 best.pixel = best.red = best.green = best.blue = 0;
476 for( c.pixel = 0, diff = 0x7fffffff; c.pixel < max; c.pixel += step )
478 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &c);
479 r = (c.red - color.red)>>8;
480 g = (c.green - color.green)>>8;
481 b = (c.blue - color.blue)>>8;
483 if( r < diff ) { best = c; diff = r; }
486 if( XAllocColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &best) )
487 color.pixel = best.pixel;
488 else color.pixel = (i < NB_RESERVED_COLORS/2)? bp : wp;
491 sysPixel[i] = color.pixel;
493 TRACE("syscolor(%x) -> pixel %i\n", *(const COLORREF*)(sys_pal_template+i),
496 /* Set EGA mapping if color in the first or last eight */
499 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
500 else if (i >= NB_RESERVED_COLORS - 8 )
501 X11DRV_PALETTE_mapEGAPixel[i - (NB_RESERVED_COLORS-16)] = color.pixel;
505 /* now allocate changeable set */
507 if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
509 int c_min = 0, c_max = palette_size, c_val;
511 TRACE("Dynamic colormap...\n");
513 /* let's become the first client that actually follows
514 * X guidelines and does binary search...
517 if((pixDynMapping = HeapAlloc(GetProcessHeap(), 0, sizeof(long)*palette_size)) == NULL) {
518 WARN("Out of memory while building system palette.\n");
523 /* comment this out if you want to debug palette init */
524 XGrabServer(gdi_display);
526 while( c_max - c_min > 0 )
528 c_val = (c_max + c_min)/2 + (c_max + c_min)%2;
530 if( !XAllocColorCells(gdi_display, X11DRV_PALETTE_PaletteXColormap, False,
531 plane_masks, 0, pixDynMapping, c_val) )
535 XFreeColors(gdi_display, X11DRV_PALETTE_PaletteXColormap, pixDynMapping, c_val, 0);
540 if( c_min > alloc_system_colors - NB_RESERVED_COLORS)
541 c_min = alloc_system_colors - NB_RESERVED_COLORS;
543 c_min = (c_min/2) + (c_min/2); /* need even set for split palette */
546 if( !XAllocColorCells(gdi_display, X11DRV_PALETTE_PaletteXColormap, False,
547 plane_masks, 0, pixDynMapping, c_min) )
549 WARN("Inexplicable failure during colorcell allocation.\n");
553 palette_size = c_min + NB_RESERVED_COLORS;
555 XUngrabServer(gdi_display);
558 TRACE("adjusted size %i colorcells\n", palette_size);
560 else if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
562 /* virtual colorspace - ToPhysical takes care of
563 * color translations but we have to allocate full palette
564 * to maintain compatibility
567 TRACE("Virtual colorspace - screendepth %i\n", screen_depth);
569 else palette_size = NB_RESERVED_COLORS; /* system palette only - however we can alloc a bunch
570 * of colors and map to them */
572 TRACE("Shared system palette uses %i colors.\n", palette_size);
574 /* set gap to account for pixel shortage. It has to be right in the center
575 * of the system palette because otherwise raster ops get screwed. */
577 if( palette_size >= 256 )
578 { COLOR_gapStart = 256; COLOR_gapEnd = -1; }
580 { COLOR_gapStart = palette_size/2; COLOR_gapEnd = 255 - palette_size/2; }
582 X11DRV_PALETTE_firstFree = ( palette_size > NB_RESERVED_COLORS &&
583 (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL || !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED)) )
584 ? NB_RESERVED_COLORS/2 : -1;
586 COLOR_sysPal = HeapAlloc(GetProcessHeap(),0,sizeof(PALETTEENTRY)*256);
587 if(COLOR_sysPal == NULL) {
588 ERR("Unable to allocate the system palette!\n");
589 HeapFree(GetProcessHeap(), 0, pixDynMapping);
593 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
595 if (screen_depth <= 8)
597 X11DRV_PALETTE_XPixelToPalette = HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(int) );
598 if(X11DRV_PALETTE_XPixelToPalette == NULL) {
599 ERR("Out of memory: XPixelToPalette!\n");
600 HeapFree(GetProcessHeap(), 0, pixDynMapping);
603 for( i = 0; i < 256; i++ )
604 X11DRV_PALETTE_XPixelToPalette[i] = NB_PALETTE_EMPTY_VALUE;
607 /* for hicolor visuals PaletteToPixel mapping is used to skip
608 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
611 X11DRV_PALETTE_PaletteToXPixel = HeapAlloc(GetProcessHeap(),0,sizeof(int)*256);
612 if(X11DRV_PALETTE_PaletteToXPixel == NULL) {
613 ERR("Out of memory: PaletteToXPixel!\n");
614 HeapFree(GetProcessHeap(), 0, pixDynMapping);
618 for( i = j = 0; i < 256; i++ )
620 if( i >= COLOR_gapStart && i <= COLOR_gapEnd )
622 X11DRV_PALETTE_PaletteToXPixel[i] = NB_PALETTE_EMPTY_VALUE;
623 COLOR_sysPal[i].peFlags = 0; /* mark as unused */
627 if( i < NB_RESERVED_COLORS/2 )
629 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[i];
630 COLOR_sysPal[i] = sys_pal_template[i];
631 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
633 else if( i >= 256 - NB_RESERVED_COLORS/2 )
635 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[(i + NB_RESERVED_COLORS) - 256];
636 COLOR_sysPal[i] = sys_pal_template[(i + NB_RESERVED_COLORS) - 256];
637 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
639 else if( pixDynMapping )
640 X11DRV_PALETTE_PaletteToXPixel[i] = pixDynMapping[j++];
642 X11DRV_PALETTE_PaletteToXPixel[i] = i;
644 TRACE("index %i -> pixel %i\n", i, X11DRV_PALETTE_PaletteToXPixel[i]);
646 if( X11DRV_PALETTE_XPixelToPalette )
647 X11DRV_PALETTE_XPixelToPalette[X11DRV_PALETTE_PaletteToXPixel[i]] = i;
650 HeapFree(GetProcessHeap(), 0, pixDynMapping);
655 /***********************************************************************
656 * Colormap Initialization
658 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template )
660 /* initialize unused entries to what Windows uses as a color
661 * cube - based on Greg Kreider's code.
665 int red, no_r, inc_r;
666 int green, no_g, inc_g;
667 int blue, no_b, inc_b;
669 if (palette_size <= NB_RESERVED_COLORS)
671 while (i*i*i < (palette_size - NB_RESERVED_COLORS)) i++;
672 no_r = no_g = no_b = --i;
673 if ((no_r * (no_g+1) * no_b) < (palette_size - NB_RESERVED_COLORS)) no_g++;
674 if ((no_r * no_g * (no_b+1)) < (palette_size - NB_RESERVED_COLORS)) no_b++;
675 inc_r = (255 - NB_COLORCUBE_START_INDEX)/no_r;
676 inc_g = (255 - NB_COLORCUBE_START_INDEX)/no_g;
677 inc_b = (255 - NB_COLORCUBE_START_INDEX)/no_b;
681 idx = X11DRV_PALETTE_firstFree;
684 for (blue = NB_COLORCUBE_START_INDEX; blue < 256 && idx; blue += inc_b )
685 for (green = NB_COLORCUBE_START_INDEX; green < 256 && idx; green += inc_g )
686 for (red = NB_COLORCUBE_START_INDEX; red < 256 && idx; red += inc_r )
690 if( red == NB_COLORCUBE_START_INDEX && green == red && blue == green ) continue;
692 COLOR_sysPal[idx].peRed = red;
693 COLOR_sysPal[idx].peGreen = green;
694 COLOR_sysPal[idx].peBlue = blue;
698 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
700 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
701 if (shifts->physicalRed.max != 255) no_r = (red * shifts->physicalRed.max) / 255;
702 if (shifts->physicalGreen.max != 255) no_g = (green * shifts->physicalGreen.max) / 255;
703 if (shifts->physicalBlue.max != 255) no_b = (blue * shifts->physicalBlue.max) / 255;
705 X11DRV_PALETTE_PaletteToXPixel[idx] = (no_r << shifts->physicalRed.shift) | (no_g << shifts->physicalGreen.shift) | (no_b << shifts->physicalBlue.shift);
707 else if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
710 color.pixel = (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[idx] : idx;
711 color.red = COLOR_sysPal[idx].peRed << 8;
712 color.green = COLOR_sysPal[idx].peGreen << 8;
713 color.blue = COLOR_sysPal[idx].peBlue << 8;
714 color.flags = DoRed | DoGreen | DoBlue;
715 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
717 idx = X11DRV_PALETTE_freeList[idx];
720 /* try to fill some entries in the "gap" with
721 * what's already in the colormap - they will be
722 * mappable to but not changeable. */
724 if( COLOR_gapStart < COLOR_gapEnd && X11DRV_PALETTE_XPixelToPalette )
729 max = alloc_system_colors - (256 - (COLOR_gapEnd - COLOR_gapStart));
730 for ( i = 0, idx = COLOR_gapStart; i < 256 && idx <= COLOR_gapEnd; i++ )
731 if( X11DRV_PALETTE_XPixelToPalette[i] == NB_PALETTE_EMPTY_VALUE )
735 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &xc);
736 r = xc.red>>8; g = xc.green>>8; b = xc.blue>>8;
738 if( xc.pixel < 256 && X11DRV_PALETTE_CheckSysColor( sys_pal_template, RGB(r, g, b)) &&
739 XAllocColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &xc) )
741 X11DRV_PALETTE_XPixelToPalette[xc.pixel] = idx;
742 X11DRV_PALETTE_PaletteToXPixel[idx] = xc.pixel;
743 *(COLORREF*)(COLOR_sysPal + idx) = RGB(r, g, b);
744 COLOR_sysPal[idx++].peFlags |= PC_SYS_USED;
745 if( --max <= 0 ) break;
748 COLOR_gapFilled = idx - COLOR_gapStart;
754 /***********************************************************************
755 * X11DRV_IsSolidColor
757 * Check whether 'color' can be represented with a solid color.
759 BOOL X11DRV_IsSolidColor( COLORREF color )
762 const PALETTEENTRY *pEntry = COLOR_sysPal;
764 if (color & 0xff000000) return TRUE; /* indexed color */
766 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
768 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return TRUE; /* no palette */
770 EnterCriticalSection( &palette_cs );
771 for (i = 0; i < palette_size ; i++, pEntry++)
773 if( i < COLOR_gapStart || i > COLOR_gapEnd )
774 if ((GetRValue(color) == pEntry->peRed) &&
775 (GetGValue(color) == pEntry->peGreen) &&
776 (GetBValue(color) == pEntry->peBlue))
778 LeaveCriticalSection( &palette_cs );
782 LeaveCriticalSection( &palette_cs );
787 /***********************************************************************
788 * X11DRV_PALETTE_ToLogical
790 * Return RGB color for given X pixel.
792 COLORREF X11DRV_PALETTE_ToLogical(X11DRV_PDEVICE *physDev, int pixel)
797 /* truecolor visual */
799 if (screen_depth >= 24) return pixel;
802 /* check for hicolor visuals first */
804 if ( (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) && !X11DRV_PALETTE_Graymax )
806 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
808 if(physDev->color_shifts)
809 shifts = physDev->color_shifts;
811 color.red = (pixel >> shifts->logicalRed.shift) & shifts->logicalRed.max;
812 if (shifts->logicalRed.scale<8)
813 color.red= color.red << (8-shifts->logicalRed.scale) |
814 color.red >> (2*shifts->logicalRed.scale-8);
815 color.green = (pixel >> shifts->logicalGreen.shift) & shifts->logicalGreen.max;
816 if (shifts->logicalGreen.scale<8)
817 color.green=color.green << (8-shifts->logicalGreen.scale) |
818 color.green >> (2*shifts->logicalGreen.scale-8);
819 color.blue = (pixel >> shifts->logicalBlue.shift) & shifts->logicalBlue.max;
820 if (shifts->logicalBlue.scale<8)
821 color.blue= color.blue << (8-shifts->logicalBlue.scale) |
822 color.blue >> (2*shifts->logicalBlue.scale-8);
823 return RGB(color.red,color.green,color.blue);
826 /* check if we can bypass X */
828 if ((screen_depth <= 8) && (pixel < 256) &&
829 !(X11DRV_PALETTE_PaletteFlags & (X11DRV_PALETTE_VIRTUAL | X11DRV_PALETTE_FIXED)) ) {
831 EnterCriticalSection( &palette_cs );
832 ret = *(COLORREF *)(COLOR_sysPal + (X11DRV_PALETTE_XPixelToPalette ? X11DRV_PALETTE_XPixelToPalette[pixel]: pixel)) & 0x00ffffff;
833 LeaveCriticalSection( &palette_cs );
839 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
841 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
845 /***********************************************************************
846 * X11DRV_SysPaletteLookupPixel
848 static int X11DRV_SysPaletteLookupPixel( COLORREF col, BOOL skipReserved )
850 int i, best = 0, diff = 0x7fffffff;
853 for( i = 0; i < palette_size && diff ; i++ )
855 if( !(COLOR_sysPal[i].peFlags & PC_SYS_USED) ||
856 (skipReserved && COLOR_sysPal[i].peFlags & PC_SYS_RESERVED) )
859 r = COLOR_sysPal[i].peRed - GetRValue(col);
860 g = COLOR_sysPal[i].peGreen - GetGValue(col);
861 b = COLOR_sysPal[i].peBlue - GetBValue(col);
865 if( r < diff ) { best = i; diff = r; }
871 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
873 return (c1.rgbRed * c1.rgbRed + c1.rgbGreen * c1.rgbGreen + c1.rgbBlue * c1.rgbBlue) >
874 (c2.rgbRed * c2.rgbRed + c2.rgbGreen * c2.rgbGreen + c2.rgbBlue * c2.rgbBlue);
877 /***********************************************************************
878 * X11DRV_PALETTE_GetColor
880 * Resolve PALETTEINDEX/PALETTERGB/DIBINDEX COLORREFs to an RGB COLORREF.
882 COLORREF X11DRV_PALETTE_GetColor( X11DRV_PDEVICE *physDev, COLORREF color )
884 HPALETTE hPal = GetCurrentObject(physDev->dev.hdc, OBJ_PAL );
885 unsigned char spec_type = color >> 24;
886 unsigned idx = color & 0xffff;
892 case 2: /* PALETTERGB */
893 idx = GetNearestPaletteIndex( hPal, color );
894 /* fall through to PALETTEINDEX */
896 case 1: /* PALETTEINDEX */
897 if (!GetPaletteEntries( hPal, idx, 1, &entry ))
899 WARN("PALETTEINDEX(%x) : idx %d is out of bounds, assuming black\n", color, idx);
902 return RGB( entry.peRed, entry.peGreen, entry.peBlue );
904 case 0x10: /* DIBINDEX */
905 if( GetDIBColorTable( physDev->dev.hdc, idx, 1, &quad ) != 1 ) {
906 WARN("DIBINDEX(%x) : idx %d is out of bounds, assuming black\n", color , idx);
909 return RGB( quad.rgbRed, quad.rgbGreen, quad.rgbBlue );
913 /* fall through to RGB */
920 /***********************************************************************
921 * X11DRV_PALETTE_ToPhysical
923 * Return the physical color closest to 'color'.
925 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
928 HPALETTE hPal = GetCurrentObject(physDev->dev.hdc, OBJ_PAL );
929 unsigned char spec_type = color >> 24;
930 int *mapping = palette_get_mapping( hPal );
932 ColorShifts *shifts = &X11DRV_PALETTE_default_shifts;
934 if(physDev->color_shifts)
935 shifts = physDev->color_shifts;
937 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
939 /* there is no colormap limitation; we are going to have to compute
940 * the pixel value from the visual information stored earlier
943 unsigned long red, green, blue;
944 unsigned idx = color & 0xffff;
948 case 0x10: /* DIBINDEX */
949 color = X11DRV_PALETTE_GetColor( physDev, color );
952 case 1: /* PALETTEINDEX */
953 if (!GetPaletteEntries( hPal, idx, 1, &entry ))
955 WARN("PALETTEINDEX(%x) : idx %d is out of bounds, assuming black\n", color, idx);
958 if (mapping) return mapping[idx];
959 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
964 /* fall through to RGB */
967 if (physDev->depth == 1)
972 if (GetDIBColorTable( physDev->dev.hdc, 0, 2, table ) == 2)
974 if(!colour_is_brighter(table[1], table[0])) white = 0;
976 return (((color >> 16) & 0xff) +
977 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? white : 1 - white;
982 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
984 if (X11DRV_PALETTE_Graymax)
986 /* grayscale only; return scaled value */
987 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
991 /* scale each individually and construct the TrueColor pixel value */
992 if (shifts->physicalRed.scale < 8)
993 red = red >> (8-shifts->physicalRed.scale);
994 else if (shifts->physicalRed.scale > 8)
995 red = red << (shifts->physicalRed.scale-8) |
996 red >> (16-shifts->physicalRed.scale);
997 if (shifts->physicalGreen.scale < 8)
998 green = green >> (8-shifts->physicalGreen.scale);
999 else if (shifts->physicalGreen.scale > 8)
1000 green = green << (shifts->physicalGreen.scale-8) |
1001 green >> (16-shifts->physicalGreen.scale);
1002 if (shifts->physicalBlue.scale < 8)
1003 blue = blue >> (8-shifts->physicalBlue.scale);
1004 else if (shifts->physicalBlue.scale > 8)
1005 blue = blue << (shifts->physicalBlue.scale-8) |
1006 blue >> (16-shifts->physicalBlue.scale);
1008 return (red << shifts->physicalRed.shift) | (green << shifts->physicalGreen.shift) | (blue << shifts->physicalBlue.shift);
1014 WARN("Palette %p is not realized\n", hPal);
1016 switch(spec_type) /* we have to peruse DC and system palette */
1020 /* fall through to RGB */
1023 if (physDev->depth == 1)
1028 if (GetDIBColorTable( physDev->dev.hdc, 0, 2, table ) == 2)
1030 if(!colour_is_brighter(table[1], table[0]))
1033 return (((color >> 16) & 0xff) +
1034 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? white : 1 - white;
1037 EnterCriticalSection( &palette_cs );
1038 index = X11DRV_SysPaletteLookupPixel( color, FALSE);
1039 if (X11DRV_PALETTE_PaletteToXPixel) index = X11DRV_PALETTE_PaletteToXPixel[index];
1040 LeaveCriticalSection( &palette_cs );
1042 /* TRACE(palette,"RGB(%lx) -> pixel %i\n", color, index);
1045 case 1: /* PALETTEINDEX */
1046 index = color & 0xffff;
1047 if (!GetPaletteEntries( hPal, index, 1, &entry ))
1048 WARN("PALETTEINDEX(%x) : index %i is out of bounds\n", color, index);
1049 else if (mapping) index = mapping[index];
1051 /* TRACE(palette,"PALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
1054 case 2: /* PALETTERGB */
1055 index = GetNearestPaletteIndex( hPal, color );
1056 if (mapping) index = mapping[index];
1057 /* TRACE(palette,"PALETTERGB(%lx) -> pixel %i\n", color, index);
1065 /***********************************************************************
1066 * X11DRV_PALETTE_LookupPixel
1068 int X11DRV_PALETTE_LookupPixel(ColorShifts *shifts, COLORREF color )
1070 unsigned char spec_type = color >> 24;
1072 /* Only accept RGB which has spec_type = 0 */
1078 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
1080 unsigned long red, green, blue;
1081 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
1083 if (X11DRV_PALETTE_Graymax)
1085 /* grayscale only; return scaled value */
1086 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
1090 /* No shifts are set in case of 1-bit */
1091 if(!shifts) shifts = &X11DRV_PALETTE_default_shifts;
1093 /* scale each individually and construct the TrueColor pixel value */
1094 if (shifts->physicalRed.scale < 8)
1095 red = red >> (8-shifts->physicalRed.scale);
1096 else if (shifts->physicalRed.scale > 8)
1097 red = red << (shifts->physicalRed.scale-8) |
1098 red >> (16-shifts->physicalRed.scale);
1099 if (shifts->physicalGreen.scale < 8)
1100 green = green >> (8-shifts->physicalGreen.scale);
1101 else if (shifts->physicalGreen.scale > 8)
1102 green = green << (shifts->physicalGreen.scale-8) |
1103 green >> (16-shifts->physicalGreen.scale);
1104 if (shifts->physicalBlue.scale < 8)
1105 blue = blue >> (8-shifts->physicalBlue.scale);
1106 else if (shifts->physicalBlue.scale > 8)
1107 blue = blue << (shifts->physicalBlue.scale-8) |
1108 blue >> (16-shifts->physicalBlue.scale);
1110 return (red << shifts->physicalRed.shift) | (green << shifts->physicalGreen.shift) | (blue << shifts->physicalBlue.shift);
1116 HPALETTE hPal = GetStockObject(DEFAULT_PALETTE);
1117 int *mapping = palette_get_mapping( hPal );
1120 WARN("Palette %p is not realized\n", hPal);
1122 EnterCriticalSection( &palette_cs );
1123 index = X11DRV_SysPaletteLookupPixel( color, FALSE);
1124 if (X11DRV_PALETTE_PaletteToXPixel)
1125 index = X11DRV_PALETTE_PaletteToXPixel[index];
1126 LeaveCriticalSection( &palette_cs );
1132 /***********************************************************************
1133 * X11DRV_PALETTE_LookupSystemXPixel
1135 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col)
1137 int i, best = 0, diff = 0x7fffffff;
1138 int size = palette_size;
1141 for( i = 0; i < size && diff ; i++ )
1143 if( i == NB_RESERVED_COLORS/2 )
1145 int newi = size - NB_RESERVED_COLORS/2;
1149 r = COLOR_sysPal[i].peRed - GetRValue(col);
1150 g = COLOR_sysPal[i].peGreen - GetGValue(col);
1151 b = COLOR_sysPal[i].peBlue - GetBValue(col);
1153 r = r*r + g*g + b*b;
1155 if( r < diff ) { best = i; diff = r; }
1158 return (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[best] : best;
1161 /***********************************************************************
1162 * X11DRV_PALETTE_FormatSystemPalette
1164 static void X11DRV_PALETTE_FormatSystemPalette(void)
1166 /* Build free list so we'd have an easy way to find
1167 * out if there are any available colorcells.
1170 int i, j = X11DRV_PALETTE_firstFree = NB_RESERVED_COLORS/2;
1172 COLOR_sysPal[j].peFlags = 0;
1173 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
1174 if( i < COLOR_gapStart || i > COLOR_gapEnd )
1176 COLOR_sysPal[i].peFlags = 0; /* unused tag */
1177 X11DRV_PALETTE_freeList[j] = i; /* next */
1180 X11DRV_PALETTE_freeList[j] = 0;
1183 /***********************************************************************
1184 * X11DRV_PALETTE_CheckSysColor
1186 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c)
1189 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1190 if( c == (*(const COLORREF*)(sys_pal_template + i) & 0x00ffffff) )
1196 /***********************************************************************
1197 * X11DRV_LookupSysPaletteExact
1199 static int X11DRV_LookupSysPaletteExact( BYTE r, BYTE g, BYTE b )
1202 for( i = 0; i < palette_size; i++ )
1204 if( COLOR_sysPal[i].peFlags & PC_SYS_USED ) /* skips gap */
1205 if( COLOR_sysPal[i].peRed == r &&
1206 COLOR_sysPal[i].peGreen == g &&
1207 COLOR_sysPal[i].peBlue == b )
1214 /***********************************************************************
1215 * RealizePalette (X11DRV.@)
1217 UINT X11DRV_RealizePalette( PHYSDEV dev, HPALETTE hpal, BOOL primary )
1219 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1222 UINT i, iRemapped = 0;
1223 int *prev_mapping, *mapping;
1224 PALETTEENTRY entries[256];
1227 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return 0;
1229 if (!GetObjectW( hpal, sizeof(num_entries), &num_entries )) return 0;
1231 /* initialize palette mapping table */
1232 prev_mapping = palette_get_mapping( hpal );
1234 mapping = HeapReAlloc( GetProcessHeap(), 0, prev_mapping, sizeof(int)*num_entries);
1236 mapping = HeapAlloc( GetProcessHeap(), 0, sizeof(int)*num_entries);
1238 if(mapping == NULL) {
1239 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1242 palette_set_mapping( hpal, mapping );
1244 if (num_entries > 256)
1246 FIXME( "more than 256 entries not supported\n" );
1249 if (!(num_entries = GetPaletteEntries( hpal, 0, num_entries, entries ))) return 0;
1251 /* reset dynamic system palette entries */
1253 EnterCriticalSection( &palette_cs );
1254 if( primary && X11DRV_PALETTE_firstFree != -1)
1255 X11DRV_PALETTE_FormatSystemPalette();
1257 for (i = 0; i < num_entries; i++)
1262 /* Even though the docs say that only one flag is to be set,
1263 * they are a bitmask. At least one app sets more than one at
1265 if ( entries[i].peFlags & PC_EXPLICIT ) {
1266 /* palette entries are indices into system palette */
1267 index = *(WORD*)&entries[i];
1268 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
1270 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
1273 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1275 if ( entries[i].peFlags & PC_RESERVED ) {
1276 /* forbid future mappings to this entry */
1277 flag |= PC_SYS_RESERVED;
1280 if (! (entries[i].peFlags & PC_NOCOLLAPSE) ) {
1281 /* try to collapse identical colors */
1282 index = X11DRV_LookupSysPaletteExact( entries[i].peRed, entries[i].peGreen, entries[i].peBlue );
1287 if( X11DRV_PALETTE_firstFree > 0 )
1290 index = X11DRV_PALETTE_firstFree; /* ought to be available */
1291 X11DRV_PALETTE_firstFree = X11DRV_PALETTE_freeList[index];
1293 color.pixel = (X11DRV_PALETTE_PaletteToXPixel) ? X11DRV_PALETTE_PaletteToXPixel[index] : index;
1294 color.red = entries[i].peRed << 8;
1295 color.green = entries[i].peGreen << 8;
1296 color.blue = entries[i].peBlue << 8;
1297 color.flags = DoRed | DoGreen | DoBlue;
1299 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
1300 wine_tsx11_unlock();
1302 COLOR_sysPal[index] = entries[i];
1303 COLOR_sysPal[index].peFlags = flag;
1304 X11DRV_PALETTE_freeList[index] = 0;
1306 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1308 else if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
1310 index = X11DRV_PALETTE_LookupPixel( physDev->color_shifts, RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ));
1313 /* we have to map to existing entry in the system palette */
1315 index = X11DRV_SysPaletteLookupPixel( RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ),
1319 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1322 if( !prev_mapping || mapping[i] != index ) iRemapped++;
1325 TRACE("entry %i (%x) -> pixel %i\n", i, *(COLORREF*)&entries[i], index);
1328 LeaveCriticalSection( &palette_cs );
1333 /***********************************************************************
1334 * UnrealizePalette (X11DRV.@)
1336 BOOL X11DRV_UnrealizePalette( HPALETTE hpal )
1338 int *mapping = palette_get_mapping( hpal );
1343 XDeleteContext( gdi_display, (XID)hpal, palette_context );
1344 wine_tsx11_unlock();
1345 HeapFree( GetProcessHeap(), 0, mapping );
1351 /***********************************************************************
1352 * GetSystemPaletteEntries (X11DRV.@)
1354 UINT X11DRV_GetSystemPaletteEntries( PHYSDEV dev, UINT start, UINT count, LPPALETTEENTRY entries )
1358 if (!entries) return palette_size;
1359 if (start >= palette_size) return 0;
1360 if (start + count >= palette_size) count = palette_size - start;
1362 EnterCriticalSection( &palette_cs );
1363 for (i = 0; i < count; i++)
1365 entries[i].peRed = COLOR_sysPal[start + i].peRed;
1366 entries[i].peGreen = COLOR_sysPal[start + i].peGreen;
1367 entries[i].peBlue = COLOR_sysPal[start + i].peBlue;
1368 entries[i].peFlags = 0;
1369 TRACE("\tidx(%02x) -> RGB(%08x)\n", start + i, *(COLORREF*)(entries + i) );
1371 LeaveCriticalSection( &palette_cs );
1376 /***********************************************************************
1377 * GetNearestColor (X11DRV.@)
1379 COLORREF X11DRV_GetNearestColor( PHYSDEV dev, COLORREF color )
1381 unsigned char spec_type = color >> 24;
1384 if (!palette_size) return color;
1386 if (spec_type == 1 || spec_type == 2)
1388 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1392 HPALETTE hpal = GetCurrentObject( dev->hdc, OBJ_PAL );
1394 if (!hpal) hpal = GetStockObject( DEFAULT_PALETTE );
1396 if (spec_type == 2) /* PALETTERGB */
1397 index = GetNearestPaletteIndex( hpal, color );
1398 else /* PALETTEINDEX */
1399 index = LOWORD(color);
1401 if (!GetPaletteEntries( hpal, index, 1, &entry ))
1403 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color, index );
1404 if (!GetPaletteEntries( hpal, 0, 1, &entry )) return CLR_INVALID;
1406 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
1408 color &= 0x00ffffff;
1409 EnterCriticalSection( &palette_cs );
1410 nearest = (0x00ffffff & *(COLORREF*)(COLOR_sysPal + X11DRV_SysPaletteLookupPixel(color, FALSE)));
1411 LeaveCriticalSection( &palette_cs );
1413 TRACE("(%06x): returning %06x\n", color, nearest );
1418 /***********************************************************************
1419 * RealizeDefaultPalette (X11DRV.@)
1421 UINT X11DRV_RealizeDefaultPalette( PHYSDEV dev )
1425 if (palette_size && GetObjectType(dev->hdc) != OBJ_MEMDC)
1427 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1428 int i, index, *mapping = palette_get_mapping( GetStockObject(DEFAULT_PALETTE) );
1429 PALETTEENTRY entries[NB_RESERVED_COLORS];
1431 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, entries );
1432 EnterCriticalSection( &palette_cs );
1433 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1435 index = X11DRV_PALETTE_LookupSystemXPixel( RGB(entries[i].peRed,
1437 entries[i].peBlue) );
1438 /* mapping is allocated in COLOR_InitPalette() */
1439 if( index != mapping[i] )
1445 LeaveCriticalSection( &palette_cs );