Release 960805
[wine/multimedia.git] / objects / color.c
blob308d191b4f2c7af4ee0d75782ef06fb41c90020c
1 /*
2 * Color functions
4 * Copyright 1993 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
6 */
8 #include <stdlib.h>
9 #include <X11/Xlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include "windows.h"
13 #include "options.h"
14 #include "gdi.h"
15 #include "color.h"
16 #include "stddebug.h"
17 #include "debug.h"
18 #include "xmalloc.h"
20 /* Palette indexed mode:
22 * logical palette -> mapping -> pixel
25 * Windows needs contiguous color space ( from 0 to n ) but
26 * it is possible only with private colormap. Otherwise we
27 * have to map DC palette indices to real pixel values.
28 * With private colormap it boils down to identity mapping. The
29 * other special case is when we have fixed color visual with
30 * screendepth > 8 - we abandon mapping altogether (pixel
31 * values can be calculated without X server assistance).
33 * For info about general Windows palette management read
34 * http://198.105.232.5/MSDN/LIBRARY/TECHNOTE/CH3.HTM
37 typedef struct
39 Colormap colorMap;
40 UINT16 size;
41 UINT16 flags;
42 } CSPACE;
44 static CSPACE cSpace = {0, 0, 0};
46 static int COLOR_Redshift = 0; /* to handle abortive COLOR_VIRTUAL visuals */
47 static int COLOR_Redmax = 0;
48 static int COLOR_Greenshift = 0;
49 static int COLOR_Greenmax = 0;
50 static int COLOR_Blueshift = 0;
51 static int COLOR_Bluemax = 0;
52 static int COLOR_Graymax = 0;
54 /* System color space.
56 * First 10 and last 10 colors in COLOR_sysPalette are
57 * "guarded". RealizePalette changes only the rest of colorcells. For
58 * currently inactive window it changes only DC palette mappings.
61 #define NB_RESERVED_COLORS 20 /* number of fixed colors in system palette */
62 #define NB_COLORCUBE_START_INDEX 63
64 Visual* visual = NULL;
66 static PALETTEENTRY* COLOR_sysPal = NULL; /* current system palette */
67 static int COLOR_gapStart = 256;
68 static int COLOR_gapEnd = -1;
70 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
71 static int COLOR_firstFree = 0;
72 static unsigned char COLOR_freeList[256];
74 /* Maps entry in the system palette to X pixel value */
75 int* COLOR_PaletteToPixel = NULL;
77 /* Maps pixel to the entry in the system palette */
78 int* COLOR_PixelToPalette = NULL;
80 static const PALETTEENTRY __sysPalTemplate[NB_RESERVED_COLORS] =
82 /* first 10 entries in the system palette */
83 /* red green blue flags */
84 { 0x00, 0x00, 0x00, PC_SYS_USED },
85 { 0x80, 0x00, 0x00, PC_SYS_USED },
86 { 0x00, 0x80, 0x00, PC_SYS_USED },
87 { 0x80, 0x80, 0x00, PC_SYS_USED },
88 { 0x00, 0x00, 0x80, PC_SYS_USED },
89 { 0x80, 0x00, 0x80, PC_SYS_USED },
90 { 0x00, 0x80, 0x80, PC_SYS_USED },
91 { 0xc0, 0xc0, 0xc0, PC_SYS_USED },
92 { 0xc0, 0xdc, 0xc0, PC_SYS_USED },
93 { 0xa6, 0xca, 0xf0, PC_SYS_USED },
95 /* ... c_min/2 dynamic colorcells */
97 /* ... gap (for sparse palettes) */
99 /* ... c_min/2 dynamic colorcells */
101 { 0xff, 0xfb, 0xf0, PC_SYS_USED },
102 { 0xa0, 0xa0, 0xa4, PC_SYS_USED },
103 { 0x80, 0x80, 0x80, PC_SYS_USED },
104 { 0xff, 0x00, 0x00, PC_SYS_USED },
105 { 0x00, 0xff, 0x00, PC_SYS_USED },
106 { 0xff, 0xff, 0x00, PC_SYS_USED },
107 { 0x00, 0x00, 0xff, PC_SYS_USED },
108 { 0xff, 0x00, 0xff, PC_SYS_USED },
109 { 0x00, 0xff, 0xff, PC_SYS_USED },
110 { 0xff, 0xff, 0xff, PC_SYS_USED } /* last 10 */
113 /* Map an EGA index (0..15) to a pixel value in the system color space. */
115 int COLOR_mapEGAPixel[16];
117 /***********************************************************************
118 * Misc auxiliary functions
120 Colormap COLOR_GetColormap(void)
122 return cSpace.colorMap;
125 UINT16 COLOR_GetSystemPaletteSize(void)
127 return (cSpace.flags & COLOR_PRIVATE) ? cSpace.size : 256;
130 UINT16 COLOR_GetSystemPaletteFlags(void)
132 return cSpace.flags;
135 COLORREF COLOR_GetSystemPaletteEntry(BYTE i)
137 return *(COLORREF*)(COLOR_sysPal + i) & 0x00ffffff;
140 void COLOR_FormatSystemPalette(void)
142 /* Build free list so we'd have an easy way to find
143 * out if there are any available colorcells.
146 int i, j = COLOR_firstFree = NB_RESERVED_COLORS/2;
148 COLOR_sysPal[j].peFlags = 0;
149 for( i = NB_RESERVED_COLORS/2 + 1 ; i < 256 - NB_RESERVED_COLORS/2 ; i++ )
150 if( i < COLOR_gapStart || i > COLOR_gapEnd )
152 COLOR_sysPal[i].peFlags = 0; /* unused tag */
153 COLOR_freeList[j] = i; /* next */
154 j = i;
156 COLOR_freeList[j] = 0;
159 void COLOR_FillDefaultColors(void)
161 /* initialize unused entries to what Windows uses as a color
162 * cube - based on Greg Kreider's code.
165 int i = 0, idx = 0;
166 int red, no_r, inc_r;
167 int green, no_g, inc_g;
168 int blue, no_b, inc_b;
170 while (i*i*i < (cSpace.size - NB_RESERVED_COLORS)) i++;
171 no_r = no_g = no_b = --i;
172 if ((no_r * (no_g+1) * no_b) < (cSpace.size - NB_RESERVED_COLORS)) no_g++;
173 if ((no_r * no_g * (no_b+1)) < (cSpace.size - NB_RESERVED_COLORS)) no_b++;
174 inc_r = (255 - NB_COLORCUBE_START_INDEX)/no_r;
175 inc_g = (255 - NB_COLORCUBE_START_INDEX)/no_g;
176 inc_b = (255 - NB_COLORCUBE_START_INDEX)/no_b;
178 idx = COLOR_firstFree;
180 for (blue = NB_COLORCUBE_START_INDEX; blue < 256 && idx; blue += inc_b )
181 for (green = NB_COLORCUBE_START_INDEX; green < 256 && idx; green += inc_g )
182 for (red = NB_COLORCUBE_START_INDEX; red < 256 && idx; red += inc_r )
184 /* weird but true */
186 if( red == NB_COLORCUBE_START_INDEX && green == red && blue == green ) continue;
188 COLOR_sysPal[idx].peRed = red;
189 COLOR_sysPal[idx].peGreen = green;
190 COLOR_sysPal[idx].peBlue = blue;
192 /* set X color */
194 if( cSpace.flags & COLOR_VIRTUAL )
196 if (COLOR_Redmax != 255) no_r = (red * COLOR_Redmax) / 255;
197 if (COLOR_Greenmax != 255) no_g = (green * COLOR_Greenmax) / 255;
198 if (COLOR_Bluemax != 255) no_b = (blue * COLOR_Bluemax) / 255;
200 COLOR_PaletteToPixel[idx] = (no_r << COLOR_Redshift) | (no_g << COLOR_Greenshift) | (no_b << COLOR_Blueshift);
202 else if( !(cSpace.flags & COLOR_FIXED) )
204 XColor color = { color.pixel = (COLOR_PaletteToPixel)? COLOR_PaletteToPixel[idx] : idx ,
205 COLOR_sysPal[idx].peRed << 8,
206 COLOR_sysPal[idx].peGreen << 8,
207 COLOR_sysPal[idx].peGreen << 8,
208 (DoRed | DoGreen | DoBlue) };
209 XStoreColor(display, cSpace.colorMap, &color);
212 idx = COLOR_freeList[idx];
215 /* fill the rest with gray for now - only needed for
216 * sparse palette (in seamless mode)
219 for ( i = COLOR_gapStart; i <= COLOR_gapEnd; i++ )
221 *(COLORREF*)(COLOR_sysPal + i) = 0x00c0c0c0;
222 if( COLOR_PaletteToPixel )
223 COLOR_PaletteToPixel[i] = COLOR_PaletteToPixel[7];
227 /***********************************************************************
228 * COLOR_BuildPrivateMap/COLOR_BuildSharedMap
230 * Executed only one time so we don't care about sloppiness,
231 * the rest have to be very tight though...
233 static BOOL COLOR_BuildPrivateMap(CSPACE* cs)
235 /* Private colormap - identity mapping */
237 XColor color;
238 int i;
240 COLOR_sysPal = (PALETTEENTRY*)xmalloc(sizeof(PALETTEENTRY)*cs->size);
242 dprintf_palette(stddeb,"\tbuilding private map - %i palette entries\n", cs->size);
244 /* Allocate system palette colors */
246 for( i=0; i < cs->size; i++ )
248 if( i < NB_RESERVED_COLORS/2 )
250 color.red = __sysPalTemplate[i].peRed * 65535 / 255;
251 color.green = __sysPalTemplate[i].peGreen * 65535 / 255;
252 color.blue = __sysPalTemplate[i].peBlue * 65535 / 255;
253 COLOR_sysPal[i] = __sysPalTemplate[i];
255 else if( i >= cs->size - NB_RESERVED_COLORS/2 )
257 int j = NB_RESERVED_COLORS + i - cs->size;
258 color.red = __sysPalTemplate[j].peRed * 65535 / 255;
259 color.green = __sysPalTemplate[j].peGreen * 65535 / 255;
260 color.blue = __sysPalTemplate[j].peBlue * 65535 / 255;
261 COLOR_sysPal[i] = __sysPalTemplate[j];
264 color.flags = DoRed | DoGreen | DoBlue;
265 color.pixel = i;
266 XStoreColor(display, cs->colorMap, &color);
268 /* Set EGA mapping if color in the first or last eight */
270 if (i < 8)
271 COLOR_mapEGAPixel[i] = color.pixel;
272 else if (i >= cs->size - 8 )
273 COLOR_mapEGAPixel[i - (cs->size - 16)] = color.pixel;
276 COLOR_PixelToPalette = COLOR_PaletteToPixel = NULL;
278 COLOR_gapStart = 256; COLOR_gapEnd = -1;
280 COLOR_firstFree = (cs->size > NB_RESERVED_COLORS)?NB_RESERVED_COLORS/2 : -1;
281 return FALSE;
284 static BOOL COLOR_BuildSharedMap(CSPACE* cs)
286 XColor color;
287 unsigned long sysPixel[NB_RESERVED_COLORS];
288 unsigned long* pixDynMapping = NULL;
289 unsigned long plane_masks[1];
290 int i, j;
291 int color_ini_max = 256;
293 /* read "AllocSystemColors" from wine.conf */
295 color_ini_max = PROFILE_GetWineIniInt( "options", "AllocSystemColors", 256);
296 if (color_ini_max > 256) color_ini_max = 256;
297 if (color_ini_max < 20) color_ini_max = 20;
298 dprintf_palette(stddeb,"COLOR_Init: %d colors configured.\n",color_ini_max);
300 dprintf_palette(stddeb,"\tbuilding shared map - %i palette entries\n", cs->size);
302 /* Be nice and allocate system colors as read-only */
304 for( i = 0; i < NB_RESERVED_COLORS; i++ )
306 color.red = __sysPalTemplate[i].peRed * 65535 / 255;
307 color.green = __sysPalTemplate[i].peGreen * 65535 / 255;
308 color.blue = __sysPalTemplate[i].peBlue * 65535 / 255;
309 color.flags = DoRed | DoGreen | DoBlue;
311 if (!XAllocColor( display, cSpace.colorMap, &color ))
313 fprintf(stderr, "Warning: Not enough free colors for system palette.\n" );
314 color.pixel = color.red = color.green = color.blue = 0;
317 sysPixel[i] = color.pixel;
319 dprintf_palette(stddeb,"\tsyscolor(%lx) -> pixel %i\n",
320 *(COLORREF*)(__sysPalTemplate+i), (int)color.pixel);
322 /* Set EGA mapping if color in the first or last eight */
324 if (i < 8)
325 COLOR_mapEGAPixel[i] = color.pixel;
326 else if (i >= NB_RESERVED_COLORS - 8 )
327 COLOR_mapEGAPixel[i - (NB_RESERVED_COLORS-16)] = color.pixel;
330 if( !(cSpace.flags & COLOR_FIXED) )
332 int c_min = 0, c_max = cs->size, c_val;
334 dprintf_palette(stddeb,"\tdynamic colormap... ");
336 /* comment this out if you want to debug palette init */
338 XGrabServer(display);
340 /* let's become the first client that actually follows
341 * X guidelines and does binary search...
344 pixDynMapping = (unsigned long*)xmalloc(sizeof(long)*cs->size);
345 while( c_max - c_min > 0 )
347 c_val = (c_max + c_min)/2 + (c_max + c_min)%2;
349 if( !XAllocColorCells(display, cs->colorMap, False,
350 plane_masks, 0, pixDynMapping, c_val) )
351 c_max = c_val - 1;
352 else
354 XFreeColors(display, cs->colorMap, pixDynMapping, c_val, 0);
355 c_min = c_val;
359 if( c_min > color_ini_max - NB_RESERVED_COLORS)
360 c_min = color_ini_max - NB_RESERVED_COLORS;
362 c_min = (c_min/2) + (c_min/2); /* need even set for split palette */
364 if( c_min > 0 )
365 if( !XAllocColorCells(display, cs->colorMap, False,
366 plane_masks, 0, pixDynMapping, c_min) )
368 fprintf(stderr,"Inexplicable failure during colorcell allocation.\n");
369 c_min = 0;
372 cs->size = c_min + NB_RESERVED_COLORS;
374 XUngrabServer(display);
376 dprintf_palette(stddeb,"adjusted size %i colorcells\n", cs->size);
378 else if( cSpace.flags & COLOR_VIRTUAL )
380 /* virtual colorspace - ToPhysical takes care of
381 * color translations but we have to allocate full palette
382 * to maintain compatibility
384 cs->size = 256;
385 dprintf_palette(stddeb,"\tvirtual colorspace - screendepth %i\n", screenDepth);
387 else cs->size = NB_RESERVED_COLORS; /* system palette only - however we can alloc a bunch
388 * of colors and map to them */
390 dprintf_palette(stddeb,"Shared system palette uses %i colors.\n", cs->size);
392 /* Set gap to account for pixel shortage. It has to be right in the center
393 * of the system palette because otherwise raster ops get screwed.
394 * ( if we have 100 color palette and application does invert for pixel 1
395 * it gets pixel 254 - far beyond our pathetic palette unless these 100
396 * colors are mapped with the gap in the middle )
399 if( cs->size >= 256 )
400 { COLOR_gapStart = 256; COLOR_gapEnd = -1; }
401 else
402 { COLOR_gapStart = cs->size/2; COLOR_gapEnd = 255 - cs->size/2; }
404 COLOR_firstFree = ( cs->size > NB_RESERVED_COLORS &&
405 (cs->flags & COLOR_VIRTUAL || !(cs->flags & COLOR_FIXED)) )
406 ? NB_RESERVED_COLORS/2 : -1;
408 COLOR_sysPal = (PALETTEENTRY*)xmalloc(sizeof(PALETTEENTRY)*256);
410 /* Setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
412 if( screenDepth <= 8 )
414 COLOR_PixelToPalette = (int*)xmalloc(sizeof(int)*256);
415 memset( COLOR_PixelToPalette, 0, 256*sizeof(int) );
418 /* for hicolor visuals PaletteToPixel mapping is used to skip
419 * RGB->pixel calculation in COLOR_ToPhysical().
422 COLOR_PaletteToPixel = (int*)xmalloc(sizeof(int)*256);
424 for( i = j = 0; i < 256; i++ )
426 if( i >= COLOR_gapStart && i <= COLOR_gapEnd )
428 COLOR_PaletteToPixel[i] = 0;
429 COLOR_sysPal[i].peFlags = 0; /* mark as unused */
430 continue;
433 if( i < NB_RESERVED_COLORS/2 )
435 COLOR_PaletteToPixel[i] = sysPixel[i];
436 COLOR_sysPal[i] = __sysPalTemplate[i];
438 else if( i >= 256 - NB_RESERVED_COLORS/2 )
440 COLOR_PaletteToPixel[i] = sysPixel[(i + NB_RESERVED_COLORS) - 256];
441 COLOR_sysPal[i] = __sysPalTemplate[(i + NB_RESERVED_COLORS) - 256];
443 else if( pixDynMapping )
444 COLOR_PaletteToPixel[i] = pixDynMapping[j++];
445 else
446 COLOR_PaletteToPixel[i] = i;
448 dprintf_palette(stddeb,"\tindex %i -> pixel %i\n", i, COLOR_PaletteToPixel[i]);
450 if( COLOR_PixelToPalette )
451 COLOR_PixelToPalette[COLOR_PaletteToPixel[i]] = i;
454 if( pixDynMapping ) free(pixDynMapping);
455 return TRUE;
459 /***********************************************************************
460 * COLOR_InitPalette
462 * Create the system palette and initialize mapping tables.
464 static HPALETTE16 COLOR_InitPalette(void)
466 int i;
467 HPALETTE16 hpalette;
468 LOGPALETTE * palPtr;
469 PALETTEOBJ* palObj;
471 memset(COLOR_freeList, 0, 256*sizeof(unsigned char));
473 /* calculate max palette size */
475 cSpace.size = visual->map_entries;
477 if (cSpace.flags & COLOR_PRIVATE)
478 COLOR_BuildPrivateMap( &cSpace );
479 else
480 COLOR_BuildSharedMap( &cSpace );
482 /* Build free list */
484 if( COLOR_firstFree != -1 )
485 COLOR_FormatSystemPalette();
487 COLOR_FillDefaultColors();
489 /* create default palette (20 system colors) */
491 palPtr = xmalloc( sizeof(LOGPALETTE) + (NB_RESERVED_COLORS-1)*sizeof(PALETTEENTRY) );
492 if (!palPtr) return FALSE;
494 palPtr->palVersion = 0x300;
495 palPtr->palNumEntries = NB_RESERVED_COLORS;
496 for( i = 0; i < NB_RESERVED_COLORS; i ++ )
498 palPtr->palPalEntry[i].peRed = __sysPalTemplate[i].peRed;
499 palPtr->palPalEntry[i].peGreen = __sysPalTemplate[i].peGreen;
500 palPtr->palPalEntry[i].peBlue = __sysPalTemplate[i].peBlue;
501 palPtr->palPalEntry[i].peFlags = 0;
503 hpalette = CreatePalette( palPtr );
505 palObj = (PALETTEOBJ*) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
507 palObj->mapping = xmalloc( sizeof(int) * 20 );
509 free( palPtr );
510 return hpalette;
514 /***********************************************************************
515 * COLOR_Computeshifts
517 * Calculate conversion parameters for direct mapped visuals
519 void COLOR_Computeshifts(unsigned long maskbits, int *shift, int *max)
521 int i;
523 if (maskbits==0)
525 *shift=0;
526 *max=0;
527 return;
530 for(i=0;!(maskbits&1);i++)
531 maskbits >>= 1;
533 *shift = i;
534 *max = maskbits;
538 /***********************************************************************
539 * COLOR_Init
541 * Initialize color map and system palette.
544 HPALETTE16 COLOR_Init(void)
546 visual = DefaultVisual( display, DefaultScreen(display) );
548 dprintf_palette(stddeb,"COLOR_Init: initializing palette manager...");
550 switch(visual->class)
552 case DirectColor:
553 cSpace.flags |= COLOR_VIRTUAL;
554 case GrayScale:
555 case PseudoColor:
556 if (Options.usePrivateMap)
558 XSetWindowAttributes win_attr;
560 cSpace.flags |= COLOR_PRIVATE;
561 cSpace.colorMap = XCreateColormap( display, rootWindow,
562 visual, AllocAll );
563 if (cSpace.colorMap)
565 if( rootWindow != DefaultRootWindow(display) )
567 win_attr.colormap = cSpace.colorMap;
568 XChangeWindowAttributes( display, rootWindow,
569 CWColormap, &win_attr );
571 break;
574 cSpace.colorMap = DefaultColormapOfScreen( screen );
575 break;
577 case StaticGray:
578 cSpace.colorMap = DefaultColormapOfScreen( screen );
579 cSpace.flags |= COLOR_FIXED;
580 COLOR_Graymax = (1<<screenDepth)-1;
581 break;
583 case TrueColor:
584 cSpace.flags |= COLOR_VIRTUAL;
585 case StaticColor:
586 cSpace.colorMap = DefaultColormapOfScreen( screen );
587 cSpace.flags |= COLOR_FIXED;
588 COLOR_Computeshifts(visual->red_mask, &COLOR_Redshift, &COLOR_Redmax);
589 COLOR_Computeshifts(visual->green_mask, &COLOR_Greenshift, &COLOR_Greenmax);
590 COLOR_Computeshifts(visual->blue_mask, &COLOR_Blueshift, &COLOR_Bluemax);
591 break;
594 dprintf_palette(stddeb," visual class %i\n", visual->class);
596 return COLOR_InitPalette();
600 /***********************************************************************
601 * COLOR_IsSolid
603 * Check whether 'color' can be represented with a solid color.
605 BOOL COLOR_IsSolid( COLORREF color )
607 int i;
608 const PALETTEENTRY *pEntry = COLOR_sysPal;
610 if (color & 0xff000000) return TRUE; /* indexed color */
612 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
614 for (i = 0; i < 256 ; i++, pEntry++)
616 if( i < COLOR_gapStart || i > COLOR_gapEnd )
617 if ((GetRValue(color) == pEntry->peRed) &&
618 (GetGValue(color) == pEntry->peGreen) &&
619 (GetBValue(color) == pEntry->peBlue)) return TRUE;
621 return FALSE;
625 /***********************************************************************
626 * COLOR_PaletteLookupPixel
628 int COLOR_PaletteLookupPixel( PALETTEENTRY* palPalEntry, int size,
629 int* mapping, COLORREF col, BOOL skipReserved )
631 int i, best = 0, diff = 0x7fffffff;
632 int r,g,b;
634 for( i = 0; i < size && diff ; i++ )
636 if( !(palPalEntry[i].peFlags & PC_SYS_USED) ||
637 (skipReserved && palPalEntry[i].peFlags & PC_SYS_RESERVED) )
638 continue;
640 r = palPalEntry[i].peRed - GetRValue(col);
641 g = palPalEntry[i].peGreen - GetGValue(col);
642 b = palPalEntry[i].peBlue - GetBValue(col);
644 r = r*r + g*g + b*b;
646 if( r < diff ) { best = i; diff = r; }
648 return (mapping) ? mapping[best] : best;
651 /***********************************************************************
652 * COLOR_LookupSystemPixel
654 int COLOR_LookupSystemPixel(COLORREF col)
656 int i, best = 0, diff = 0x7fffffff;
657 int size = COLOR_GetSystemPaletteSize();
658 int r,g,b;
660 for( i = 0; i < size && diff ; i++ )
662 if( i == NB_RESERVED_COLORS/2 ) i = size - NB_RESERVED_COLORS/2;
664 r = COLOR_sysPal[i].peRed - GetRValue(col);
665 g = COLOR_sysPal[i].peGreen - GetGValue(col);
666 b = COLOR_sysPal[i].peBlue - GetBValue(col);
668 r = r*r + g*g + b*b;
670 if( r < diff ) { best = i; diff = r; }
673 return (COLOR_PaletteToPixel)? COLOR_PaletteToPixel[best] : best;
676 /***********************************************************************
677 * COLOR_PaletteLookupExactIndex
679 int COLOR_PaletteLookupExactIndex( PALETTEENTRY* palPalEntry, int size,
680 COLORREF col )
682 int i;
683 BYTE r = GetRValue(col), g = GetGValue(col), b = GetBValue(col);
684 for( i = 0; i < size; i++ )
686 if( palPalEntry[i].peFlags & PC_SYS_USED ) /* skips gap */
687 if( palPalEntry[i].peRed == r &&
688 palPalEntry[i].peGreen == g &&
689 palPalEntry[i].peBlue == b )
690 return i;
692 return -1;
695 /***********************************************************************
696 * COLOR_LookupNearestColor
698 COLORREF COLOR_LookupNearestColor( PALETTEENTRY* palPalEntry, int size, COLORREF color )
700 unsigned char spec_type = color >> 24;
701 int i;
703 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
705 if( spec_type == 2 ) /* PALETTERGB */
706 color = *(COLORREF*)
707 (palPalEntry + COLOR_PaletteLookupPixel(palPalEntry,size,NULL,color,FALSE));
709 else if( spec_type == 1 ) /* PALETTEINDEX */
710 if( (i = color & 0x0000ffff) >= size )
712 fprintf(stderr, "\tRGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, i);
713 color = *(COLORREF*)palPalEntry;
715 else color = *(COLORREF*)(palPalEntry + i);
717 color &= 0x00ffffff;
718 return (0x00ffffff & *(COLORREF*)
719 (COLOR_sysPal + COLOR_PaletteLookupPixel(COLOR_sysPal, 256, NULL, color, FALSE)));
722 /***********************************************************************
723 * COLOR_ToLogical
725 * Return RGB color for given X pixel.
727 COLORREF COLOR_ToLogical(int pixel)
729 XColor color;
731 /* truecolor visual */
733 if (screenDepth >= 24) return pixel;
735 /* check for hicolor visuals first */
737 if ( cSpace.flags & COLOR_FIXED && !COLOR_Graymax )
739 color.red = pixel >> COLOR_Redshift;
740 color.green = pixel >> COLOR_Greenshift;
741 color.blue = pixel >> COLOR_Blueshift;
743 else if ((screenDepth <= 8) && (pixel < 256) &&
744 !(cSpace.flags & (COLOR_VIRTUAL | COLOR_FIXED)) )
745 return ( *(COLORREF*)(COLOR_sysPal +
746 ((COLOR_PixelToPalette)?COLOR_PixelToPalette[pixel]:pixel)) ) & 0x00ffffff;
747 else
749 color.pixel = pixel;
750 XQueryColor(display, cSpace.colorMap, &color);
751 return RGB(color.red >> 8, color.green >> 8, color.blue >> 8);
754 return RGB((color.red * 255)/COLOR_Redmax,
755 (color.green * 255)/COLOR_Greenmax,
756 (color.blue * 255)/COLOR_Bluemax);
760 /***********************************************************************
761 * COLOR_ToPhysical
763 * Return the physical color closest to 'color'.
765 int COLOR_ToPhysical( DC *dc, COLORREF color )
767 WORD index = 0;
768 unsigned char spec_type;
769 HPALETTE16 hPal = (dc)? dc->w.hPalette: STOCK_DEFAULT_PALETTE;
771 spec_type = color >> 24;
773 if( spec_type == 0xff )
775 spec_type = 0; /* 'write' seems to need that for 'Page 1' text */
776 color &= 0xffffff;
779 if( spec_type > 2 )
781 fprintf(stderr, "COLOR_ToPhysical : invalid RGB specifier for: %08lx\n", color);
782 spec_type = 0;
785 if (dc && (dc->w.bitsPerPixel == 1) && (spec_type == 0))
787 /* monochrome */
788 if (((color >> 16) & 0xff) +
789 ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2)
790 return 1; /* white */
791 else return 0; /* black */
794 if ( cSpace.flags & COLOR_FIXED )
796 /* there is no colormap limitation; we are going to have to compute
797 * the pixel value from the visual information stored earlier
800 unsigned long red, green, blue;
801 unsigned idx = 0;
802 PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
804 switch(spec_type)
806 case 2: /* PALETTERGB - not sure if we really need to search palette */
808 idx = COLOR_PaletteLookupPixel( palPtr->logpalette.palPalEntry,
809 palPtr->logpalette.palNumEntries,
810 NULL, color, FALSE);
812 if( palPtr->mapping ) return palPtr->mapping[idx];
814 color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
815 break;
817 case 1: /* PALETTEINDEX */
819 if ( (idx = color & 0xffff) >= palPtr->logpalette.palNumEntries)
821 fprintf(stderr, "\tRGB(%lx) : idx %d is out of bounds, assuming black\n", color, idx);
822 return 0;
825 if( palPtr->mapping ) return palPtr->mapping[idx];
827 color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
829 /* fall through and out */
831 case 0: /* RGB */
832 default:
835 red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
837 if (COLOR_Graymax)
839 /* grayscale only; return scaled value */
840 return ( (red * 30 + green * 69 + blue * 11) * COLOR_Graymax) / 25500;
842 else
844 /* scale each individually and construct the TrueColor pixel value */
845 if (COLOR_Redmax != 255) red = (red * COLOR_Redmax) / 255;
846 if (COLOR_Greenmax != 255) green = (green * COLOR_Greenmax) / 255;
847 if (COLOR_Bluemax != 255) blue = (blue * COLOR_Bluemax) / 255;
849 return (red << COLOR_Redshift) | (green << COLOR_Greenshift) | (blue << COLOR_Blueshift);
852 else
854 PALETTEOBJ* palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC);
856 /* palPtr can be NULL when DC is being destroyed */
858 if( !palPtr ) return 0;
859 else if( !palPtr->mapping )
860 dprintf_palette(stddeb,"\tpalette %04x is not realized\n", dc->w.hPalette);
862 switch(spec_type) /* we have to peruse DC and system palette */
864 default:
865 case 0: /* RGB */
866 index = COLOR_PaletteLookupPixel( COLOR_sysPal, 256,
867 COLOR_PaletteToPixel, color, FALSE);
869 /* dprintf_palette(stddeb,"\tRGB(%lx) -> pixel %i\n", color, index);
871 break;
872 case 1: /* PALETTEINDEX */
873 index = color & 0xffff;
875 if( index >= palPtr->logpalette.palNumEntries )
876 fprintf(stderr, "\tRGB(%lx) : index %i is out of bounds\n", color, index);
877 else if( palPtr->mapping ) index = palPtr->mapping[index];
879 /* dprintf_palette(stddeb,"\tPALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
881 break;
882 case 2: /* PALETTERGB */
883 index = COLOR_PaletteLookupPixel( palPtr->logpalette.palPalEntry,
884 palPtr->logpalette.palNumEntries,
885 palPtr->mapping, color, FALSE);
886 /* dprintf_palette(stddeb,"\tPALETTERGB(%lx) -> pixel %i\n", color, index);
888 break;
892 return index;
895 /***********************************************************************
896 * COLOR_SetMapping
898 * Set the color-mapping table for selected palette.
899 * Return number of entries which mapping has changed.
901 int COLOR_SetMapping( PALETTEOBJ* palPtr, BOOL mapOnly )
903 int i, index;
904 char flag;
905 int prevMapping = (palPtr->mapping) ? 1 : 0;
906 int iRemapped = 0;
908 /* reset dynamic system palette entries */
910 if( !mapOnly && COLOR_firstFree != -1)
911 COLOR_FormatSystemPalette();
913 /* initialize palette mapping table */
915 palPtr->mapping = (int*)xrealloc(palPtr->mapping, sizeof(int)*
916 palPtr->logpalette.palNumEntries);
918 for( i = 0; i < palPtr->logpalette.palNumEntries; i++ )
920 index = -1;
921 flag = PC_SYS_USED;
923 switch( palPtr->logpalette.palPalEntry[i].peFlags & 0x07 )
925 case PC_EXPLICIT: /* palette entries are indices into system palette */
926 index = *(WORD*)(palPtr->logpalette.palPalEntry + i);
927 if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) )
929 fprintf(stderr,"PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index);
930 index = 0;
932 break;
934 case PC_RESERVED: /* forbid future mappings to this entry */
935 flag |= PC_SYS_RESERVED;
937 /* fall through */
938 default: /* try to collapse identical colors */
939 index = COLOR_PaletteLookupExactIndex(COLOR_sysPal, 256,
940 *(COLORREF*)(palPtr->logpalette.palPalEntry + i));
941 /* fall through */
942 case PC_NOCOLLAPSE:
943 if( index < 0 )
945 if( COLOR_firstFree > 0 && !(cSpace.flags & COLOR_FIXED) )
947 XColor color;
948 index = COLOR_firstFree; /* ought to be available */
949 COLOR_firstFree = COLOR_freeList[index];
951 color.pixel = (COLOR_PaletteToPixel) ? COLOR_PaletteToPixel[index] : index;
952 color.red = palPtr->logpalette.palPalEntry[i].peRed << 8;
953 color.green = palPtr->logpalette.palPalEntry[i].peGreen << 8;
954 color.blue = palPtr->logpalette.palPalEntry[i].peBlue << 8;
955 color.flags = DoRed | DoGreen | DoBlue;
956 XStoreColor(display, cSpace.colorMap, &color);
958 COLOR_sysPal[index] = palPtr->logpalette.palPalEntry[i];
959 COLOR_sysPal[index].peFlags = flag;
960 COLOR_freeList[index] = 0;
962 if( COLOR_PaletteToPixel ) index = COLOR_PaletteToPixel[index];
963 break;
965 else if ( cSpace.flags & COLOR_VIRTUAL )
967 index = COLOR_ToPhysical( NULL, 0x00ffffff &
968 *(COLORREF*)(palPtr->logpalette.palPalEntry + i));
969 break;
972 /* we have to map to existing entry in the system palette */
974 index = COLOR_PaletteLookupPixel(COLOR_sysPal, 256, NULL,
975 *(COLORREF*)(palPtr->logpalette.palPalEntry + i), TRUE);
977 palPtr->logpalette.palPalEntry[i].peFlags |= PC_SYS_USED;
979 if( COLOR_PaletteToPixel ) index = COLOR_PaletteToPixel[index];
980 break;
983 if( !prevMapping || palPtr->mapping[i] != index ) iRemapped++;
984 palPtr->mapping[i] = index;
986 dprintf_palette(stddeb,"\tentry %i (%lx) -> pixel %i\n", i,
987 *(COLORREF*)(palPtr->logpalette.palPalEntry + i), index);
990 return iRemapped;