widl: Fix alignment check for struct members format string.
[wine/multimedia.git] / dlls / winex11.drv / palette.c
blob7ccfed70fbfb69cb3b390171947821df51d7b902
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "windef.h"
28 #include "winbase.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 #define NB_RESERVED_COLORS 20 /* number of fixed colors in system palette */
54 #define PC_SYS_USED 0x80 /* palentry is used (both system and logical) */
55 #define PC_SYS_RESERVED 0x40 /* system palentry is not to be mapped to */
57 static PALETTEENTRY *COLOR_sysPal; /* current system palette */
59 static int COLOR_gapStart = 256;
60 static int COLOR_gapEnd = -1;
61 static int COLOR_gapFilled = 0;
63 Colormap X11DRV_PALETTE_PaletteXColormap = 0;
64 UINT16 X11DRV_PALETTE_PaletteFlags = 0;
66 typedef struct {
67 int shift;
68 int scale;
69 int max;
70 } ColorShifts;
72 /* initialize to zero to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
73 static ColorShifts X11DRV_PALETTE_PRed = {0,0,0};
74 static ColorShifts X11DRV_PALETTE_LRed = {0,0,0};
75 static ColorShifts X11DRV_PALETTE_PGreen = {0,0,0};
76 static ColorShifts X11DRV_PALETTE_LGreen = {0,0,0};
77 static ColorShifts X11DRV_PALETTE_PBlue = {0,0,0};
78 static ColorShifts X11DRV_PALETTE_LBlue = {0,0,0};
79 static int X11DRV_PALETTE_Graymax = 0;
81 static int palette_size;
83 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
84 static int X11DRV_PALETTE_firstFree = 0;
85 static unsigned char X11DRV_PALETTE_freeList[256];
87 static XContext palette_context; /* X context to associate a color mapping to a palette */
89 /**********************************************************************/
91 /* Map an EGA index (0..15) to a pixel value in the system color space. */
93 int X11DRV_PALETTE_mapEGAPixel[16];
95 /**********************************************************************/
97 #define NB_COLORCUBE_START_INDEX 63
98 #define NB_PALETTE_EMPTY_VALUE -1
100 /* Maps entry in the system palette to X pixel value */
101 int *X11DRV_PALETTE_PaletteToXPixel = NULL;
103 /* Maps pixel to the entry in the system palette */
104 int *X11DRV_PALETTE_XPixelToPalette = NULL;
106 /**********************************************************************/
108 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template );
109 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template );
110 static void X11DRV_PALETTE_ComputeShifts(unsigned long maskbits, ColorShifts *physical, ColorShifts *to_logical);
111 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template );
112 static void X11DRV_PALETTE_FormatSystemPalette(void);
113 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c);
114 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col);
117 /***********************************************************************
118 * palette_get_mapping
120 static int *palette_get_mapping( HPALETTE hpal )
122 int *mapping;
124 wine_tsx11_lock();
125 if (XFindContext( gdi_display, (XID)hpal, palette_context, (char **)&mapping )) mapping = NULL;
126 wine_tsx11_unlock();
127 return mapping;
131 /***********************************************************************
132 * palette_set_mapping
134 static void palette_set_mapping( HPALETTE hpal, int *mapping )
136 wine_tsx11_lock();
137 XSaveContext( gdi_display, (XID)hpal, palette_context, (char *)mapping );
138 wine_tsx11_unlock();
142 /***********************************************************************
143 * COLOR_Init
145 * Initialize color management.
147 int X11DRV_PALETTE_Init(void)
149 int mask, white, black;
150 int monoPlane;
151 int *mapping;
152 PALETTEENTRY sys_pal_template[NB_RESERVED_COLORS];
154 TRACE("initializing palette manager...\n");
156 wine_tsx11_lock();
157 palette_context = XUniqueContext();
158 wine_tsx11_unlock();
159 white = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
160 black = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
161 monoPlane = 1;
162 for( mask = 1; !((white & mask)^(black & mask)); mask <<= 1 )
163 monoPlane++;
164 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
165 palette_size = visual->map_entries;
167 switch(visual->class)
169 case DirectColor:
170 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
171 case GrayScale:
172 case PseudoColor:
173 wine_tsx11_lock();
174 if (private_color_map)
176 XSetWindowAttributes win_attr;
178 X11DRV_PALETTE_PaletteXColormap = XCreateColormap( gdi_display, root_window,
179 visual, AllocAll );
180 if (X11DRV_PALETTE_PaletteXColormap)
182 X11DRV_PALETTE_PaletteFlags |= (X11DRV_PALETTE_PRIVATE | X11DRV_PALETTE_WHITESET);
184 monoPlane = 1;
185 for( white = palette_size - 1; !(white & 1); white >>= 1 )
186 monoPlane++;
188 if( root_window != DefaultRootWindow(gdi_display) )
190 win_attr.colormap = X11DRV_PALETTE_PaletteXColormap;
191 XChangeWindowAttributes( gdi_display, root_window, CWColormap, &win_attr );
194 } else {
195 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
196 visual, AllocNone);
198 wine_tsx11_unlock();
199 break;
201 case StaticGray:
202 wine_tsx11_lock();
203 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
204 visual, AllocNone);
205 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
206 X11DRV_PALETTE_Graymax = (1 << screen_depth)-1;
207 wine_tsx11_unlock();
208 break;
210 case TrueColor:
211 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_VIRTUAL;
212 case StaticColor: {
213 int *depths,nrofdepths;
214 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
215 * depths 1 and 4
217 wine_tsx11_lock();
218 depths = XListDepths(gdi_display,DefaultScreen(gdi_display),&nrofdepths);
219 if ((nrofdepths==2) && ((depths[0]==4) || depths[1]==4)) {
220 monoPlane = 1;
221 for( white = palette_size - 1; !(white & 1); white >>= 1 )
222 monoPlane++;
223 X11DRV_PALETTE_PaletteFlags = (white & mask) ? X11DRV_PALETTE_WHITESET : 0;
224 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
225 visual, AllocNone);
227 else
229 X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
230 visual, AllocNone);
231 X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
232 X11DRV_PALETTE_ComputeShifts(visual->red_mask, &X11DRV_PALETTE_PRed, &X11DRV_PALETTE_LRed);
233 X11DRV_PALETTE_ComputeShifts(visual->green_mask, &X11DRV_PALETTE_PGreen, &X11DRV_PALETTE_LGreen);
234 X11DRV_PALETTE_ComputeShifts(visual->blue_mask, &X11DRV_PALETTE_PBlue, &X11DRV_PALETTE_LBlue);
236 XFree(depths);
237 wine_tsx11_unlock();
238 break;
242 TRACE(" visual class %i (%i)\n", visual->class, monoPlane);
244 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, sys_pal_template );
246 if ((mapping = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int) * NB_RESERVED_COLORS )))
247 palette_set_mapping( GetStockObject(DEFAULT_PALETTE), mapping );
249 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
251 palette_size = 0;
253 else
255 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
256 X11DRV_PALETTE_BuildPrivateMap( sys_pal_template );
257 else
258 X11DRV_PALETTE_BuildSharedMap( sys_pal_template );
260 /* Build free list */
262 if( X11DRV_PALETTE_firstFree != -1 )
263 X11DRV_PALETTE_FormatSystemPalette();
265 X11DRV_PALETTE_FillDefaultColors( sys_pal_template );
266 palette_size = visual->map_entries;
269 return palette_size;
272 /***********************************************************************
273 * X11DRV_PALETTE_Cleanup
275 * Free external colors we grabbed in the FillDefaultPalette()
277 void X11DRV_PALETTE_Cleanup(void)
279 if( COLOR_gapFilled )
281 wine_tsx11_lock();
282 XFreeColors(gdi_display, X11DRV_PALETTE_PaletteXColormap,
283 (unsigned long*)(X11DRV_PALETTE_PaletteToXPixel + COLOR_gapStart),
284 COLOR_gapFilled, 0);
285 wine_tsx11_unlock();
289 /***********************************************************************
290 * X11DRV_PALETTE_ComputeShifts
292 * Calculate conversion parameters for direct mapped visuals
294 static void X11DRV_PALETTE_ComputeShifts(unsigned long maskbits, ColorShifts *physical, ColorShifts *to_logical)
296 int i;
298 if (maskbits==0)
300 physical->shift=0;
301 physical->scale=0;
302 physical->max=0;
303 to_logical->shift=0;
304 to_logical->scale=0;
305 to_logical->max=0;
306 return;
309 for(i=0;!(maskbits&1);i++)
310 maskbits >>= 1;
312 physical->shift = i;
313 physical->max = maskbits;
315 for(i=0;maskbits!=0;i++)
316 maskbits >>= 1;
317 physical->scale = i;
319 if (physical->scale>8)
321 /* On FreeBSD, VNC's default 32bpp mode is bgrabb (ffc00000,3ff800,7ff)!
322 * So we adjust the shifts to also normalize the color fields to
323 * the Win32 standard of 8 bits per color.
325 to_logical->shift=physical->shift+(physical->scale-8);
326 to_logical->scale=8;
327 to_logical->max=0xff;
328 } else {
329 to_logical->shift=physical->shift;
330 to_logical->scale=physical->scale;
331 to_logical->max=physical->max;
335 /***********************************************************************
336 * X11DRV_PALETTE_BuildPrivateMap
338 * Allocate colorcells and initialize mapping tables.
340 static BOOL X11DRV_PALETTE_BuildPrivateMap( const PALETTEENTRY *sys_pal_template )
342 /* Private colormap - identity mapping */
344 XColor color;
345 int i;
347 if((COLOR_sysPal = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY)*palette_size)) == NULL) {
348 WARN("Unable to allocate the system palette\n");
349 return FALSE;
352 TRACE("Building private map - %i palette entries\n", palette_size);
354 /* Allocate system palette colors */
356 wine_tsx11_lock();
357 for( i=0; i < palette_size; i++ )
359 if( i < NB_RESERVED_COLORS/2 )
361 color.red = sys_pal_template[i].peRed * 65535 / 255;
362 color.green = sys_pal_template[i].peGreen * 65535 / 255;
363 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
364 COLOR_sysPal[i] = sys_pal_template[i];
365 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
367 else if( i >= palette_size - NB_RESERVED_COLORS/2 )
369 int j = NB_RESERVED_COLORS + i - palette_size;
370 color.red = sys_pal_template[j].peRed * 65535 / 255;
371 color.green = sys_pal_template[j].peGreen * 65535 / 255;
372 color.blue = sys_pal_template[j].peBlue * 65535 / 255;
373 COLOR_sysPal[i] = sys_pal_template[j];
374 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
377 color.flags = DoRed | DoGreen | DoBlue;
378 color.pixel = i;
379 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
381 /* Set EGA mapping if color is from the first or last eight */
383 if (i < 8)
384 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
385 else if (i >= palette_size - 8 )
386 X11DRV_PALETTE_mapEGAPixel[i - (palette_size - 16)] = color.pixel;
388 wine_tsx11_unlock();
390 X11DRV_PALETTE_XPixelToPalette = X11DRV_PALETTE_PaletteToXPixel = NULL;
392 COLOR_gapStart = 256; COLOR_gapEnd = -1;
394 X11DRV_PALETTE_firstFree = (palette_size > NB_RESERVED_COLORS)?NB_RESERVED_COLORS/2 : -1;
396 return FALSE;
399 /***********************************************************************
400 * X11DRV_PALETTE_BuildSharedMap
402 * Allocate colorcells and initialize mapping tables.
404 static BOOL X11DRV_PALETTE_BuildSharedMap( const PALETTEENTRY *sys_pal_template )
406 XColor color;
407 unsigned long sysPixel[NB_RESERVED_COLORS];
408 unsigned long* pixDynMapping = NULL;
409 unsigned long plane_masks[1];
410 int i, j, warn = 0;
411 int diff, r, g, b, bp = 0, wp = 1;
412 int step = 1;
413 unsigned int max = 256;
414 Colormap defaultCM;
415 XColor defaultColors[256];
417 /* Copy the first bunch of colors out of the default colormap to prevent
418 * colormap flashing as much as possible. We're likely to get the most
419 * important Window Manager colors, etc in the first 128 colors */
420 defaultCM = DefaultColormap( gdi_display, DefaultScreen(gdi_display) );
422 if (copy_default_colors > 256) copy_default_colors = 256;
423 for (i = 0; i < copy_default_colors; i++)
424 defaultColors[i].pixel = (long) i;
425 wine_tsx11_lock();
426 XQueryColors(gdi_display, defaultCM, &defaultColors[0], copy_default_colors);
427 for (i = 0; i < copy_default_colors; i++)
428 XAllocColor( gdi_display, X11DRV_PALETTE_PaletteXColormap, &defaultColors[i] );
430 if (alloc_system_colors > 256) alloc_system_colors = 256;
431 else if (alloc_system_colors < 20) alloc_system_colors = 20;
432 TRACE("%d colors configured.\n", alloc_system_colors);
434 TRACE("Building shared map - %i palette entries\n", palette_size);
436 /* Be nice and allocate system colors as read-only */
438 for( i = 0; i < NB_RESERVED_COLORS; i++ )
440 color.red = sys_pal_template[i].peRed * 65535 / 255;
441 color.green = sys_pal_template[i].peGreen * 65535 / 255;
442 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
443 color.flags = DoRed | DoGreen | DoBlue;
445 if (!XAllocColor( gdi_display, X11DRV_PALETTE_PaletteXColormap, &color ))
447 XColor best, c;
449 if( !warn++ )
451 WARN("Not enough colors for the full system palette.\n");
453 bp = BlackPixel(gdi_display, DefaultScreen(gdi_display));
454 wp = WhitePixel(gdi_display, DefaultScreen(gdi_display));
456 max = (0xffffffff)>>(32 - screen_depth);
457 if( max > 256 )
459 step = max/256;
460 max = 256;
464 /* reinit color (XAllocColor() may change it)
465 * and map to the best shared colorcell */
467 color.red = sys_pal_template[i].peRed * 65535 / 255;
468 color.green = sys_pal_template[i].peGreen * 65535 / 255;
469 color.blue = sys_pal_template[i].peBlue * 65535 / 255;
471 best.pixel = best.red = best.green = best.blue = 0;
472 for( c.pixel = 0, diff = 0x7fffffff; c.pixel < max; c.pixel += step )
474 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &c);
475 r = (c.red - color.red)>>8;
476 g = (c.green - color.green)>>8;
477 b = (c.blue - color.blue)>>8;
478 r = r*r + g*g + b*b;
479 if( r < diff ) { best = c; diff = r; }
482 if( XAllocColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &best) )
483 color.pixel = best.pixel;
484 else color.pixel = (i < NB_RESERVED_COLORS/2)? bp : wp;
487 sysPixel[i] = color.pixel;
489 TRACE("syscolor(%x) -> pixel %i\n", *(const COLORREF*)(sys_pal_template+i),
490 (int)color.pixel);
492 /* Set EGA mapping if color in the first or last eight */
494 if (i < 8)
495 X11DRV_PALETTE_mapEGAPixel[i] = color.pixel;
496 else if (i >= NB_RESERVED_COLORS - 8 )
497 X11DRV_PALETTE_mapEGAPixel[i - (NB_RESERVED_COLORS-16)] = color.pixel;
499 wine_tsx11_unlock();
501 /* now allocate changeable set */
503 if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
505 int c_min = 0, c_max = palette_size, c_val;
507 TRACE("Dynamic colormap...\n");
509 /* let's become the first client that actually follows
510 * X guidelines and does binary search...
513 if((pixDynMapping = HeapAlloc(GetProcessHeap(), 0, sizeof(long)*palette_size)) == NULL) {
514 WARN("Out of memory while building system palette.\n");
515 return FALSE;
518 wine_tsx11_lock();
519 /* comment this out if you want to debug palette init */
520 XGrabServer(gdi_display);
522 while( c_max - c_min > 0 )
524 c_val = (c_max + c_min)/2 + (c_max + c_min)%2;
526 if( !XAllocColorCells(gdi_display, X11DRV_PALETTE_PaletteXColormap, False,
527 plane_masks, 0, pixDynMapping, c_val) )
528 c_max = c_val - 1;
529 else
531 XFreeColors(gdi_display, X11DRV_PALETTE_PaletteXColormap, pixDynMapping, c_val, 0);
532 c_min = c_val;
536 if( c_min > alloc_system_colors - NB_RESERVED_COLORS)
537 c_min = alloc_system_colors - NB_RESERVED_COLORS;
539 c_min = (c_min/2) + (c_min/2); /* need even set for split palette */
541 if( c_min > 0 )
542 if( !XAllocColorCells(gdi_display, X11DRV_PALETTE_PaletteXColormap, False,
543 plane_masks, 0, pixDynMapping, c_min) )
545 WARN("Inexplicable failure during colorcell allocation.\n");
546 c_min = 0;
549 palette_size = c_min + NB_RESERVED_COLORS;
551 XUngrabServer(gdi_display);
552 wine_tsx11_unlock();
554 TRACE("adjusted size %i colorcells\n", palette_size);
556 else if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
558 /* virtual colorspace - ToPhysical takes care of
559 * color translations but we have to allocate full palette
560 * to maintain compatibility
562 palette_size = 256;
563 TRACE("Virtual colorspace - screendepth %i\n", screen_depth);
565 else palette_size = NB_RESERVED_COLORS; /* system palette only - however we can alloc a bunch
566 * of colors and map to them */
568 TRACE("Shared system palette uses %i colors.\n", palette_size);
570 /* set gap to account for pixel shortage. It has to be right in the center
571 * of the system palette because otherwise raster ops get screwed. */
573 if( palette_size >= 256 )
574 { COLOR_gapStart = 256; COLOR_gapEnd = -1; }
575 else
576 { COLOR_gapStart = palette_size/2; COLOR_gapEnd = 255 - palette_size/2; }
578 X11DRV_PALETTE_firstFree = ( palette_size > NB_RESERVED_COLORS &&
579 (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL || !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED)) )
580 ? NB_RESERVED_COLORS/2 : -1;
582 COLOR_sysPal = HeapAlloc(GetProcessHeap(),0,sizeof(PALETTEENTRY)*256);
583 if(COLOR_sysPal == NULL) {
584 ERR("Unable to allocate the system palette!\n");
585 HeapFree(GetProcessHeap(), 0, pixDynMapping);
586 return FALSE;
589 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
591 if (screen_depth <= 8)
593 X11DRV_PALETTE_XPixelToPalette = HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(int) );
594 if(X11DRV_PALETTE_XPixelToPalette == NULL) {
595 ERR("Out of memory: XPixelToPalette!\n");
596 HeapFree(GetProcessHeap(), 0, pixDynMapping);
597 return FALSE;
599 for( i = 0; i < 256; i++ )
600 X11DRV_PALETTE_XPixelToPalette[i] = NB_PALETTE_EMPTY_VALUE;
603 /* for hicolor visuals PaletteToPixel mapping is used to skip
604 * RGB->pixel calculation in X11DRV_PALETTE_ToPhysical().
607 X11DRV_PALETTE_PaletteToXPixel = HeapAlloc(GetProcessHeap(),0,sizeof(int)*256);
608 if(X11DRV_PALETTE_PaletteToXPixel == NULL) {
609 ERR("Out of memory: PaletteToXPixel!\n");
610 HeapFree(GetProcessHeap(), 0, pixDynMapping);
611 return FALSE;
614 for( i = j = 0; i < 256; i++ )
616 if( i >= COLOR_gapStart && i <= COLOR_gapEnd )
618 X11DRV_PALETTE_PaletteToXPixel[i] = NB_PALETTE_EMPTY_VALUE;
619 COLOR_sysPal[i].peFlags = 0; /* mark as unused */
620 continue;
623 if( i < NB_RESERVED_COLORS/2 )
625 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[i];
626 COLOR_sysPal[i] = sys_pal_template[i];
627 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
629 else if( i >= 256 - NB_RESERVED_COLORS/2 )
631 X11DRV_PALETTE_PaletteToXPixel[i] = sysPixel[(i + NB_RESERVED_COLORS) - 256];
632 COLOR_sysPal[i] = sys_pal_template[(i + NB_RESERVED_COLORS) - 256];
633 COLOR_sysPal[i].peFlags |= PC_SYS_USED;
635 else if( pixDynMapping )
636 X11DRV_PALETTE_PaletteToXPixel[i] = pixDynMapping[j++];
637 else
638 X11DRV_PALETTE_PaletteToXPixel[i] = i;
640 TRACE("index %i -> pixel %i\n", i, X11DRV_PALETTE_PaletteToXPixel[i]);
642 if( X11DRV_PALETTE_XPixelToPalette )
643 X11DRV_PALETTE_XPixelToPalette[X11DRV_PALETTE_PaletteToXPixel[i]] = i;
646 HeapFree(GetProcessHeap(), 0, pixDynMapping);
648 return TRUE;
651 /***********************************************************************
652 * Colormap Initialization
654 static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_template )
656 /* initialize unused entries to what Windows uses as a color
657 * cube - based on Greg Kreider's code.
660 int i = 0, idx = 0;
661 int red, no_r, inc_r;
662 int green, no_g, inc_g;
663 int blue, no_b, inc_b;
665 if (palette_size <= NB_RESERVED_COLORS)
666 return;
667 while (i*i*i < (palette_size - NB_RESERVED_COLORS)) i++;
668 no_r = no_g = no_b = --i;
669 if ((no_r * (no_g+1) * no_b) < (palette_size - NB_RESERVED_COLORS)) no_g++;
670 if ((no_r * no_g * (no_b+1)) < (palette_size - NB_RESERVED_COLORS)) no_b++;
671 inc_r = (255 - NB_COLORCUBE_START_INDEX)/no_r;
672 inc_g = (255 - NB_COLORCUBE_START_INDEX)/no_g;
673 inc_b = (255 - NB_COLORCUBE_START_INDEX)/no_b;
675 wine_tsx11_lock();
677 idx = X11DRV_PALETTE_firstFree;
679 if( idx != -1 )
680 for (blue = NB_COLORCUBE_START_INDEX; blue < 256 && idx; blue += inc_b )
681 for (green = NB_COLORCUBE_START_INDEX; green < 256 && idx; green += inc_g )
682 for (red = NB_COLORCUBE_START_INDEX; red < 256 && idx; red += inc_r )
684 /* weird but true */
686 if( red == NB_COLORCUBE_START_INDEX && green == red && blue == green ) continue;
688 COLOR_sysPal[idx].peRed = red;
689 COLOR_sysPal[idx].peGreen = green;
690 COLOR_sysPal[idx].peBlue = blue;
692 /* set X color */
694 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
696 if (X11DRV_PALETTE_PRed.max != 255) no_r = (red * X11DRV_PALETTE_PRed.max) / 255;
697 if (X11DRV_PALETTE_PGreen.max != 255) no_g = (green * X11DRV_PALETTE_PGreen.max) / 255;
698 if (X11DRV_PALETTE_PBlue.max != 255) no_b = (blue * X11DRV_PALETTE_PBlue.max) / 255;
700 X11DRV_PALETTE_PaletteToXPixel[idx] = (no_r << X11DRV_PALETTE_PRed.shift) | (no_g << X11DRV_PALETTE_PGreen.shift) | (no_b << X11DRV_PALETTE_PBlue.shift);
702 else if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
704 XColor color;
705 color.pixel = (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[idx] : idx;
706 color.red = COLOR_sysPal[idx].peRed << 8;
707 color.green = COLOR_sysPal[idx].peGreen << 8;
708 color.blue = COLOR_sysPal[idx].peBlue << 8;
709 color.flags = DoRed | DoGreen | DoBlue;
710 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
712 idx = X11DRV_PALETTE_freeList[idx];
715 /* try to fill some entries in the "gap" with
716 * what's already in the colormap - they will be
717 * mappable to but not changeable. */
719 if( COLOR_gapStart < COLOR_gapEnd && X11DRV_PALETTE_XPixelToPalette )
721 XColor xc;
722 int r, g, b, max;
724 max = alloc_system_colors - (256 - (COLOR_gapEnd - COLOR_gapStart));
725 for ( i = 0, idx = COLOR_gapStart; i < 256 && idx <= COLOR_gapEnd; i++ )
726 if( X11DRV_PALETTE_XPixelToPalette[i] == NB_PALETTE_EMPTY_VALUE )
728 xc.pixel = i;
730 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &xc);
731 r = xc.red>>8; g = xc.green>>8; b = xc.blue>>8;
733 if( xc.pixel < 256 && X11DRV_PALETTE_CheckSysColor( sys_pal_template, RGB(r, g, b)) &&
734 XAllocColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &xc) )
736 X11DRV_PALETTE_XPixelToPalette[xc.pixel] = idx;
737 X11DRV_PALETTE_PaletteToXPixel[idx] = xc.pixel;
738 *(COLORREF*)(COLOR_sysPal + idx) = RGB(r, g, b);
739 COLOR_sysPal[idx++].peFlags |= PC_SYS_USED;
740 if( --max <= 0 ) break;
743 COLOR_gapFilled = idx - COLOR_gapStart;
745 wine_tsx11_unlock();
749 /***********************************************************************
750 * X11DRV_IsSolidColor
752 * Check whether 'color' can be represented with a solid color.
754 BOOL X11DRV_IsSolidColor( COLORREF color )
756 int i;
757 const PALETTEENTRY *pEntry = COLOR_sysPal;
759 if (color & 0xff000000) return TRUE; /* indexed color */
761 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
763 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return TRUE; /* no palette */
765 for (i = 0; i < palette_size ; i++, pEntry++)
767 if( i < COLOR_gapStart || i > COLOR_gapEnd )
768 if ((GetRValue(color) == pEntry->peRed) &&
769 (GetGValue(color) == pEntry->peGreen) &&
770 (GetBValue(color) == pEntry->peBlue)) return TRUE;
772 return FALSE;
776 /***********************************************************************
777 * X11DRV_PALETTE_ToLogical
779 * Return RGB color for given X pixel.
781 COLORREF X11DRV_PALETTE_ToLogical(int pixel)
783 XColor color;
785 #if 0
786 /* truecolor visual */
788 if (screen_depth >= 24) return pixel;
789 #endif
791 /* check for hicolor visuals first */
793 if ( (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) && !X11DRV_PALETTE_Graymax )
795 color.red = (pixel >> X11DRV_PALETTE_LRed.shift) & X11DRV_PALETTE_LRed.max;
796 if (X11DRV_PALETTE_LRed.scale<8)
797 color.red= color.red << (8-X11DRV_PALETTE_LRed.scale) |
798 color.red >> (2*X11DRV_PALETTE_LRed.scale-8);
799 color.green = (pixel >> X11DRV_PALETTE_LGreen.shift) & X11DRV_PALETTE_LGreen.max;
800 if (X11DRV_PALETTE_LGreen.scale<8)
801 color.green=color.green << (8-X11DRV_PALETTE_LGreen.scale) |
802 color.green >> (2*X11DRV_PALETTE_LGreen.scale-8);
803 color.blue = (pixel >> X11DRV_PALETTE_LBlue.shift) & X11DRV_PALETTE_LBlue.max;
804 if (X11DRV_PALETTE_LBlue.scale<8)
805 color.blue= color.blue << (8-X11DRV_PALETTE_LBlue.scale) |
806 color.blue >> (2*X11DRV_PALETTE_LBlue.scale-8);
807 return RGB(color.red,color.green,color.blue);
810 /* check if we can bypass X */
812 if ((screen_depth <= 8) && (pixel < 256) &&
813 !(X11DRV_PALETTE_PaletteFlags & (X11DRV_PALETTE_VIRTUAL | X11DRV_PALETTE_FIXED)) ) {
814 return ( *(COLORREF*)(COLOR_sysPal +
815 ((X11DRV_PALETTE_XPixelToPalette)?X11DRV_PALETTE_XPixelToPalette[pixel]:pixel)) ) & 0x00ffffff;
818 wine_tsx11_lock();
819 color.pixel = pixel;
820 XQueryColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
821 wine_tsx11_unlock();
822 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
826 /***********************************************************************
827 * X11DRV_SysPaletteLookupPixel
829 static int X11DRV_SysPaletteLookupPixel( COLORREF col, BOOL skipReserved )
831 int i, best = 0, diff = 0x7fffffff;
832 int r,g,b;
834 for( i = 0; i < palette_size && diff ; i++ )
836 if( !(COLOR_sysPal[i].peFlags & PC_SYS_USED) ||
837 (skipReserved && COLOR_sysPal[i].peFlags & PC_SYS_RESERVED) )
838 continue;
840 r = COLOR_sysPal[i].peRed - GetRValue(col);
841 g = COLOR_sysPal[i].peGreen - GetGValue(col);
842 b = COLOR_sysPal[i].peBlue - GetBValue(col);
844 r = r*r + g*g + b*b;
846 if( r < diff ) { best = i; diff = r; }
848 return best;
852 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
854 return (c1.rgbRed * c1.rgbRed + c1.rgbGreen * c1.rgbGreen + c1.rgbBlue * c1.rgbBlue) >
855 (c2.rgbRed * c2.rgbRed + c2.rgbGreen * c2.rgbGreen + c2.rgbBlue * c2.rgbBlue);
858 /***********************************************************************
859 * X11DRV_PALETTE_ToPhysical
861 * Return the physical color closest to 'color'.
863 int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
865 WORD index = 0;
866 HPALETTE hPal = physDev ? GetCurrentObject(physDev->hdc, OBJ_PAL ) : GetStockObject(DEFAULT_PALETTE);
867 unsigned char spec_type = color >> 24;
868 int *mapping = palette_get_mapping( hPal );
869 PALETTEENTRY entry;
871 if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
873 /* there is no colormap limitation; we are going to have to compute
874 * the pixel value from the visual information stored earlier
877 unsigned long red, green, blue;
878 unsigned idx = color & 0xffff;
879 RGBQUAD quad;
881 switch(spec_type)
883 case 0x10: /* DIBINDEX */
884 if( GetDIBColorTable( physDev->hdc, idx, 1, &quad ) != 1 ) {
885 WARN("DIBINDEX(%x) : idx %d is out of bounds, assuming black\n", color , idx);
886 return 0;
888 color = RGB( quad.rgbRed, quad.rgbGreen, quad.rgbBlue );
889 break;
891 case 1: /* PALETTEINDEX */
892 if (!GetPaletteEntries( hPal, idx, 1, &entry ))
894 WARN("PALETTEINDEX(%x) : idx %d is out of bounds, assuming black\n", color, idx);
895 return 0;
897 if (mapping) return mapping[idx];
898 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
899 break;
901 default:
902 color &= 0xffffff;
903 /* fall through to RGB */
905 case 0: /* RGB */
906 if (physDev && (physDev->depth == 1) )
908 int white = 1;
909 RGBQUAD table[2];
911 if (GetDIBColorTable( physDev->hdc, 0, 2, table ) == 2)
913 if(!colour_is_brighter(table[1], table[0])) white = 0;
915 return (((color >> 16) & 0xff) +
916 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? white : 1 - white;
921 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
923 if (X11DRV_PALETTE_Graymax)
925 /* grayscale only; return scaled value */
926 return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
928 else
930 /* scale each individually and construct the TrueColor pixel value */
931 if (X11DRV_PALETTE_PRed.scale < 8)
932 red = red >> (8-X11DRV_PALETTE_PRed.scale);
933 else if (X11DRV_PALETTE_PRed.scale > 8)
934 red = red << (X11DRV_PALETTE_PRed.scale-8) |
935 red >> (16-X11DRV_PALETTE_PRed.scale);
936 if (X11DRV_PALETTE_PGreen.scale < 8)
937 green = green >> (8-X11DRV_PALETTE_PGreen.scale);
938 else if (X11DRV_PALETTE_PGreen.scale > 8)
939 green = green << (X11DRV_PALETTE_PGreen.scale-8) |
940 green >> (16-X11DRV_PALETTE_PGreen.scale);
941 if (X11DRV_PALETTE_PBlue.scale < 8)
942 blue = blue >> (8-X11DRV_PALETTE_PBlue.scale);
943 else if (X11DRV_PALETTE_PBlue.scale > 8)
944 blue = blue << (X11DRV_PALETTE_PBlue.scale-8) |
945 blue >> (16-X11DRV_PALETTE_PBlue.scale);
947 return (red << X11DRV_PALETTE_PRed.shift) | (green << X11DRV_PALETTE_PGreen.shift) | (blue << X11DRV_PALETTE_PBlue.shift);
950 else
952 if (!mapping)
953 WARN("Palette %p is not realized\n", hPal);
955 switch(spec_type) /* we have to peruse DC and system palette */
957 default:
958 color &= 0xffffff;
959 /* fall through to RGB */
961 case 0: /* RGB */
962 if (physDev && (physDev->depth == 1) )
964 int white = 1;
965 RGBQUAD table[2];
967 if (GetDIBColorTable( physDev->hdc, 0, 2, table ) == 2)
969 if(!colour_is_brighter(table[1], table[0]))
970 white = 0;
972 return (((color >> 16) & 0xff) +
973 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? white : 1 - white;
976 index = X11DRV_SysPaletteLookupPixel( color, FALSE);
977 if (X11DRV_PALETTE_PaletteToXPixel) index = X11DRV_PALETTE_PaletteToXPixel[index];
979 /* TRACE(palette,"RGB(%lx) -> pixel %i\n", color, index);
981 break;
982 case 1: /* PALETTEINDEX */
983 index = color & 0xffff;
984 if (!GetPaletteEntries( hPal, index, 1, &entry ))
985 WARN("PALETTEINDEX(%x) : index %i is out of bounds\n", color, index);
986 else if (mapping) index = mapping[index];
988 /* TRACE(palette,"PALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
990 break;
991 case 2: /* PALETTERGB */
992 index = GetNearestPaletteIndex( hPal, color );
993 if (mapping) index = mapping[index];
994 /* TRACE(palette,"PALETTERGB(%lx) -> pixel %i\n", color, index);
996 break;
999 return index;
1002 /***********************************************************************
1003 * X11DRV_PALETTE_LookupSystemXPixel
1005 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col)
1007 int i, best = 0, diff = 0x7fffffff;
1008 int size = palette_size;
1009 int r,g,b;
1011 for( i = 0; i < size && diff ; i++ )
1013 if( i == NB_RESERVED_COLORS/2 )
1015 int newi = size - NB_RESERVED_COLORS/2;
1016 if (newi>i) i=newi;
1019 r = COLOR_sysPal[i].peRed - GetRValue(col);
1020 g = COLOR_sysPal[i].peGreen - GetGValue(col);
1021 b = COLOR_sysPal[i].peBlue - GetBValue(col);
1023 r = r*r + g*g + b*b;
1025 if( r < diff ) { best = i; diff = r; }
1028 return (X11DRV_PALETTE_PaletteToXPixel)? X11DRV_PALETTE_PaletteToXPixel[best] : best;
1031 /***********************************************************************
1032 * X11DRV_PALETTE_FormatSystemPalette
1034 static void X11DRV_PALETTE_FormatSystemPalette(void)
1036 /* Build free list so we'd have an easy way to find
1037 * out if there are any available colorcells.
1040 int i, j = X11DRV_PALETTE_firstFree = NB_RESERVED_COLORS/2;
1042 COLOR_sysPal[j].peFlags = 0;
1043 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
1044 if( i < COLOR_gapStart || i > COLOR_gapEnd )
1046 COLOR_sysPal[i].peFlags = 0; /* unused tag */
1047 X11DRV_PALETTE_freeList[j] = i; /* next */
1048 j = i;
1050 X11DRV_PALETTE_freeList[j] = 0;
1053 /***********************************************************************
1054 * X11DRV_PALETTE_CheckSysColor
1056 static BOOL X11DRV_PALETTE_CheckSysColor( const PALETTEENTRY *sys_pal_template, COLORREF c)
1058 int i;
1059 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1060 if( c == (*(const COLORREF*)(sys_pal_template + i) & 0x00ffffff) )
1061 return 0;
1062 return 1;
1066 /***********************************************************************
1067 * X11DRV_LookupSysPaletteExact
1069 static int X11DRV_LookupSysPaletteExact( BYTE r, BYTE g, BYTE b )
1071 int i;
1072 for( i = 0; i < palette_size; i++ )
1074 if( COLOR_sysPal[i].peFlags & PC_SYS_USED ) /* skips gap */
1075 if( COLOR_sysPal[i].peRed == r &&
1076 COLOR_sysPal[i].peGreen == g &&
1077 COLOR_sysPal[i].peBlue == b )
1078 return i;
1080 return -1;
1084 /***********************************************************************
1085 * RealizePalette (X11DRV.@)
1087 UINT X11DRV_RealizePalette( X11DRV_PDEVICE *physDev, HPALETTE hpal, BOOL primary )
1089 char flag;
1090 int index;
1091 UINT i, iRemapped = 0;
1092 int *prev_mapping, *mapping;
1093 PALETTEENTRY entries[256];
1094 WORD num_entries;
1096 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) return 0;
1098 if (!GetObjectW( hpal, sizeof(num_entries), &num_entries )) return 0;
1100 /* reset dynamic system palette entries */
1102 if( primary && X11DRV_PALETTE_firstFree != -1)
1103 X11DRV_PALETTE_FormatSystemPalette();
1105 /* initialize palette mapping table */
1106 prev_mapping = palette_get_mapping( hpal );
1107 if (prev_mapping)
1108 mapping = HeapReAlloc( GetProcessHeap(), 0, prev_mapping, sizeof(int)*num_entries);
1109 else
1110 mapping = HeapAlloc( GetProcessHeap(), 0, sizeof(int)*num_entries);
1112 if(mapping == NULL) {
1113 ERR("Unable to allocate new mapping -- memory exhausted!\n");
1114 return 0;
1116 palette_set_mapping( hpal, mapping );
1118 if (num_entries > 256)
1120 FIXME( "more than 256 entries not supported\n" );
1121 num_entries = 256;
1123 if (!(num_entries = GetPaletteEntries( hpal, 0, num_entries, entries ))) return 0;
1125 for (i = 0; i < num_entries; i++)
1127 index = -1;
1128 flag = PC_SYS_USED;
1130 /* Even though the docs say that only one flag is to be set,
1131 * they are a bitmask. At least one app sets more than one at
1132 * the same time. */
1133 if ( entries[i].peFlags & PC_EXPLICIT ) {
1134 /* palette entries are indices into system palette */
1135 index = *(WORD*)&entries[i];
1136 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
1138 WARN("PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
1139 index = 0;
1141 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1142 } else {
1143 if ( entries[i].peFlags & PC_RESERVED ) {
1144 /* forbid future mappings to this entry */
1145 flag |= PC_SYS_RESERVED;
1148 if (! (entries[i].peFlags & PC_NOCOLLAPSE) ) {
1149 /* try to collapse identical colors */
1150 index = X11DRV_LookupSysPaletteExact( entries[i].peRed, entries[i].peGreen, entries[i].peBlue );
1153 if( index < 0 )
1155 if( X11DRV_PALETTE_firstFree > 0 )
1157 XColor color;
1158 index = X11DRV_PALETTE_firstFree; /* ought to be available */
1159 X11DRV_PALETTE_firstFree = X11DRV_PALETTE_freeList[index];
1161 color.pixel = (X11DRV_PALETTE_PaletteToXPixel) ? X11DRV_PALETTE_PaletteToXPixel[index] : index;
1162 color.red = entries[i].peRed << 8;
1163 color.green = entries[i].peGreen << 8;
1164 color.blue = entries[i].peBlue << 8;
1165 color.flags = DoRed | DoGreen | DoBlue;
1166 wine_tsx11_lock();
1167 XStoreColor(gdi_display, X11DRV_PALETTE_PaletteXColormap, &color);
1168 wine_tsx11_unlock();
1170 COLOR_sysPal[index] = entries[i];
1171 COLOR_sysPal[index].peFlags = flag;
1172 X11DRV_PALETTE_freeList[index] = 0;
1174 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1176 else if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
1178 index = X11DRV_PALETTE_ToPhysical( NULL,
1179 RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ));
1182 /* we have to map to existing entry in the system palette */
1184 index = X11DRV_SysPaletteLookupPixel( RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ),
1185 TRUE );
1188 if( X11DRV_PALETTE_PaletteToXPixel ) index = X11DRV_PALETTE_PaletteToXPixel[index];
1191 if( !prev_mapping || mapping[i] != index ) iRemapped++;
1192 mapping[i] = index;
1194 TRACE("entry %i (%x) -> pixel %i\n", i, *(COLORREF*)&entries[i], index);
1197 return iRemapped;
1201 /***********************************************************************
1202 * UnrealizePalette (X11DRV.@)
1204 BOOL X11DRV_UnrealizePalette( HPALETTE hpal )
1206 int *mapping = palette_get_mapping( hpal );
1208 if (mapping)
1210 wine_tsx11_lock();
1211 XDeleteContext( gdi_display, (XID)hpal, palette_context );
1212 wine_tsx11_unlock();
1213 HeapFree( GetProcessHeap(), 0, mapping );
1215 return TRUE;
1219 /***********************************************************************
1220 * GetSystemPaletteEntries (X11DRV.@)
1222 UINT X11DRV_GetSystemPaletteEntries( X11DRV_PDEVICE *physDev, UINT start, UINT count,
1223 LPPALETTEENTRY entries )
1225 UINT i;
1227 if (!entries) return palette_size;
1228 if (start >= palette_size) return 0;
1229 if (start + count >= palette_size) count = palette_size - start;
1231 for (i = 0; i < count; i++)
1233 entries[i].peRed = COLOR_sysPal[start + i].peRed;
1234 entries[i].peGreen = COLOR_sysPal[start + i].peGreen;
1235 entries[i].peBlue = COLOR_sysPal[start + i].peBlue;
1236 entries[i].peFlags = 0;
1237 TRACE("\tidx(%02x) -> RGB(%08x)\n", start + i, *(COLORREF*)(entries + i) );
1239 return count;
1243 /***********************************************************************
1244 * GetNearestColor (X11DRV.@)
1246 COLORREF X11DRV_GetNearestColor( X11DRV_PDEVICE *physDev, COLORREF color )
1248 unsigned char spec_type = color >> 24;
1249 COLORREF nearest;
1251 if (!palette_size) return color;
1253 if (spec_type == 1 || spec_type == 2)
1255 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
1257 UINT index;
1258 PALETTEENTRY entry;
1259 HPALETTE hpal = GetCurrentObject( physDev->hdc, OBJ_PAL );
1261 if (!hpal) hpal = GetStockObject( DEFAULT_PALETTE );
1263 if (spec_type == 2) /* PALETTERGB */
1264 index = GetNearestPaletteIndex( hpal, color );
1265 else /* PALETTEINDEX */
1266 index = LOWORD(color);
1268 if (!GetPaletteEntries( hpal, index, 1, &entry ))
1270 WARN("RGB(%x) : idx %d is out of bounds, assuming NULL\n", color, index );
1271 if (!GetPaletteEntries( hpal, 0, 1, &entry )) return CLR_INVALID;
1273 color = RGB( entry.peRed, entry.peGreen, entry.peBlue );
1275 color &= 0x00ffffff;
1276 nearest = (0x00ffffff & *(COLORREF*)(COLOR_sysPal + X11DRV_SysPaletteLookupPixel(color, FALSE)));
1278 TRACE("(%06x): returning %06x\n", color, nearest );
1279 return nearest;
1283 /***********************************************************************
1284 * RealizeDefaultPalette (X11DRV.@)
1286 UINT X11DRV_RealizeDefaultPalette( X11DRV_PDEVICE *physDev )
1288 UINT ret = 0;
1290 if (palette_size && GetObjectType(physDev->hdc) != OBJ_MEMDC)
1292 /* lookup is needed to account for SetSystemPaletteUse() stuff */
1293 int i, index, *mapping = palette_get_mapping( GetStockObject(DEFAULT_PALETTE) );
1294 PALETTEENTRY entries[NB_RESERVED_COLORS];
1296 GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, NB_RESERVED_COLORS, entries );
1297 for( i = 0; i < NB_RESERVED_COLORS; i++ )
1299 index = X11DRV_PALETTE_LookupSystemXPixel( RGB(entries[i].peRed,
1300 entries[i].peGreen,
1301 entries[i].peBlue) );
1302 /* mapping is allocated in COLOR_InitPalette() */
1303 if( index != mapping[i] )
1305 mapping[i]=index;
1306 ret++;
1310 return ret;