4 * Copyright 1993 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
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 the private colormap. Otherwise we
27 * have to map DC palette indices to real pixel values. With
28 * private colormaps it boils down to the identity mapping. The
29 * other special case is when we have a fixed color visual with
30 * the screendepth > 8 - we abandon palette mappings altogether
31 * because pixel values can be calculated without X server
34 * For some info about general Windows palette management read
35 * http://198.105.232.5/MSDN/LIBRARY/TECHNOTE/CH3.HTM
45 static CSPACE cSpace
= {0, 0, 0};
47 static int COLOR_Redshift
= 0; /* to handle abortive COLOR_VIRTUAL visuals */
48 static int COLOR_Redmax
= 0;
49 static int COLOR_Greenshift
= 0;
50 static int COLOR_Greenmax
= 0;
51 static int COLOR_Blueshift
= 0;
52 static int COLOR_Bluemax
= 0;
53 static int COLOR_Graymax
= 0;
55 /* System color space.
57 * First 10 and last 10 colors in COLOR_sysPalette are
58 * "guarded". RealizePalette changes only the rest of colorcells. For
59 * currently inactive window it changes only DC palette mappings.
62 #define NB_RESERVED_COLORS 20 /* number of fixed colors in system palette */
63 #define NB_COLORCUBE_START_INDEX 63
65 Visual
* visual
= NULL
;
67 static PALETTEENTRY
* COLOR_sysPal
= NULL
; /* current system palette */
68 static int COLOR_gapStart
= 256;
69 static int COLOR_gapEnd
= -1;
70 static int COLOR_gapFilled
= 0;
72 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
73 static int COLOR_firstFree
= 0;
74 static unsigned char COLOR_freeList
[256];
76 /* Maps entry in the system palette to X pixel value */
77 int* COLOR_PaletteToPixel
= NULL
;
79 /* Maps pixel to the entry in the system palette */
80 int* COLOR_PixelToPalette
= NULL
;
82 static const PALETTEENTRY __sysPalTemplate
[NB_RESERVED_COLORS
] =
84 /* first 10 entries in the system palette */
85 /* red green blue flags */
86 { 0x00, 0x00, 0x00, PC_SYS_USED
},
87 { 0x80, 0x00, 0x00, PC_SYS_USED
},
88 { 0x00, 0x80, 0x00, PC_SYS_USED
},
89 { 0x80, 0x80, 0x00, PC_SYS_USED
},
90 { 0x00, 0x00, 0x80, PC_SYS_USED
},
91 { 0x80, 0x00, 0x80, PC_SYS_USED
},
92 { 0x00, 0x80, 0x80, PC_SYS_USED
},
93 { 0xc0, 0xc0, 0xc0, PC_SYS_USED
},
94 { 0xc0, 0xdc, 0xc0, PC_SYS_USED
},
95 { 0xa6, 0xca, 0xf0, PC_SYS_USED
},
97 /* ... c_min/2 dynamic colorcells */
99 /* ... gap (for sparse palettes) */
101 /* ... c_min/2 dynamic colorcells */
103 { 0xff, 0xfb, 0xf0, PC_SYS_USED
},
104 { 0xa0, 0xa0, 0xa4, PC_SYS_USED
},
105 { 0x80, 0x80, 0x80, PC_SYS_USED
},
106 { 0xff, 0x00, 0x00, PC_SYS_USED
},
107 { 0x00, 0xff, 0x00, PC_SYS_USED
},
108 { 0xff, 0xff, 0x00, PC_SYS_USED
},
109 { 0x00, 0x00, 0xff, PC_SYS_USED
},
110 { 0xff, 0x00, 0xff, PC_SYS_USED
},
111 { 0x00, 0xff, 0xff, PC_SYS_USED
},
112 { 0xff, 0xff, 0xff, PC_SYS_USED
} /* last 10 */
115 /* Map an EGA index (0..15) to a pixel value in the system color space. */
117 int COLOR_mapEGAPixel
[16];
119 /***********************************************************************
120 * Misc auxiliary functions
122 Colormap
COLOR_GetColormap(void)
124 return cSpace
.colorMap
;
127 UINT16
COLOR_GetSystemPaletteSize(void)
129 return (cSpace
.flags
& COLOR_PRIVATE
) ? cSpace
.size
: 256;
132 UINT16
COLOR_GetSystemPaletteFlags(void)
137 COLORREF
COLOR_GetSystemPaletteEntry(BYTE i
)
139 return *(COLORREF
*)(COLOR_sysPal
+ i
) & 0x00ffffff;
142 void COLOR_FormatSystemPalette(void)
144 /* Build free list so we'd have an easy way to find
145 * out if there are any available colorcells.
148 int i
, j
= COLOR_firstFree
= NB_RESERVED_COLORS
/2;
150 COLOR_sysPal
[j
].peFlags
= 0;
151 for( i
= NB_RESERVED_COLORS
/2 + 1 ; i
< 256 - NB_RESERVED_COLORS
/2 ; i
++ )
152 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
154 COLOR_sysPal
[i
].peFlags
= 0; /* unused tag */
155 COLOR_freeList
[j
] = i
; /* next */
158 COLOR_freeList
[j
] = 0;
161 BOOL32
COLOR_CheckSysColor(COLORREF c
)
164 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
165 if( c
== (*(COLORREF
*)(__sysPalTemplate
+ i
) & 0x00ffffff) )
170 void COLOR_FillDefaultColors(void)
172 /* initialize unused entries to what Windows uses as a color
173 * cube - based on Greg Kreider's code.
177 int red
, no_r
, inc_r
;
178 int green
, no_g
, inc_g
;
179 int blue
, no_b
, inc_b
;
181 while (i
*i
*i
< (cSpace
.size
- NB_RESERVED_COLORS
)) i
++;
182 no_r
= no_g
= no_b
= --i
;
183 if ((no_r
* (no_g
+1) * no_b
) < (cSpace
.size
- NB_RESERVED_COLORS
)) no_g
++;
184 if ((no_r
* no_g
* (no_b
+1)) < (cSpace
.size
- NB_RESERVED_COLORS
)) no_b
++;
185 inc_r
= (255 - NB_COLORCUBE_START_INDEX
)/no_r
;
186 inc_g
= (255 - NB_COLORCUBE_START_INDEX
)/no_g
;
187 inc_b
= (255 - NB_COLORCUBE_START_INDEX
)/no_b
;
189 idx
= COLOR_firstFree
;
192 for (blue
= NB_COLORCUBE_START_INDEX
; blue
< 256 && idx
; blue
+= inc_b
)
193 for (green
= NB_COLORCUBE_START_INDEX
; green
< 256 && idx
; green
+= inc_g
)
194 for (red
= NB_COLORCUBE_START_INDEX
; red
< 256 && idx
; red
+= inc_r
)
198 if( red
== NB_COLORCUBE_START_INDEX
&& green
== red
&& blue
== green
) continue;
200 COLOR_sysPal
[idx
].peRed
= red
;
201 COLOR_sysPal
[idx
].peGreen
= green
;
202 COLOR_sysPal
[idx
].peBlue
= blue
;
206 if( cSpace
.flags
& COLOR_VIRTUAL
)
208 if (COLOR_Redmax
!= 255) no_r
= (red
* COLOR_Redmax
) / 255;
209 if (COLOR_Greenmax
!= 255) no_g
= (green
* COLOR_Greenmax
) / 255;
210 if (COLOR_Bluemax
!= 255) no_b
= (blue
* COLOR_Bluemax
) / 255;
212 COLOR_PaletteToPixel
[idx
] = (no_r
<< COLOR_Redshift
) | (no_g
<< COLOR_Greenshift
) | (no_b
<< COLOR_Blueshift
);
214 else if( !(cSpace
.flags
& COLOR_FIXED
) )
216 XColor color
= { color
.pixel
= (COLOR_PaletteToPixel
)? COLOR_PaletteToPixel
[idx
] : idx
,
217 COLOR_sysPal
[idx
].peRed
<< 8,
218 COLOR_sysPal
[idx
].peGreen
<< 8,
219 COLOR_sysPal
[idx
].peGreen
<< 8,
220 (DoRed
| DoGreen
| DoBlue
) };
221 XStoreColor(display
, cSpace
.colorMap
, &color
);
224 idx
= COLOR_freeList
[idx
];
227 /* try to fill some entries in the "gap" with
228 * what's already in the colormap - they will be
229 * mappable to but not changeable. */
231 if( COLOR_gapStart
< COLOR_gapEnd
&& COLOR_PixelToPalette
)
236 for ( i
= 0, idx
= COLOR_gapStart
; i
< 256 && idx
<= COLOR_gapEnd
; i
++ )
237 if( COLOR_PixelToPalette
[i
] == 0 )
241 XQueryColor(display
, cSpace
.colorMap
, &xc
);
242 r
= xc
.red
>>8; g
= xc
.green
>>8; b
= xc
.blue
>>8;
244 if( xc
.pixel
< 256 && COLOR_CheckSysColor(RGB(r
, g
, b
)) &&
245 XAllocColor(display
, cSpace
.colorMap
, &xc
) )
247 COLOR_PixelToPalette
[xc
.pixel
] = idx
;
248 COLOR_PaletteToPixel
[idx
] = xc
.pixel
;
249 *(COLORREF
*)(COLOR_sysPal
+ idx
) = RGB(r
, g
, b
);
250 COLOR_sysPal
[idx
++].peFlags
|= PC_SYS_USED
;
253 COLOR_gapFilled
= idx
- COLOR_gapStart
;
257 /***********************************************************************
258 * COLOR_BuildPrivateMap/COLOR_BuildSharedMap
260 * Allocate colorcells and initialize mapping tables.
262 static BOOL
COLOR_BuildPrivateMap(CSPACE
* cs
)
264 /* Private colormap - identity mapping */
269 COLOR_sysPal
= (PALETTEENTRY
*)xmalloc(sizeof(PALETTEENTRY
)*cs
->size
);
271 dprintf_palette(stddeb
,"\tbuilding private map - %i palette entries\n", cs
->size
);
273 /* Allocate system palette colors */
275 for( i
=0; i
< cs
->size
; i
++ )
277 if( i
< NB_RESERVED_COLORS
/2 )
279 color
.red
= __sysPalTemplate
[i
].peRed
* 65535 / 255;
280 color
.green
= __sysPalTemplate
[i
].peGreen
* 65535 / 255;
281 color
.blue
= __sysPalTemplate
[i
].peBlue
* 65535 / 255;
282 COLOR_sysPal
[i
] = __sysPalTemplate
[i
];
284 else if( i
>= cs
->size
- NB_RESERVED_COLORS
/2 )
286 int j
= NB_RESERVED_COLORS
+ i
- cs
->size
;
287 color
.red
= __sysPalTemplate
[j
].peRed
* 65535 / 255;
288 color
.green
= __sysPalTemplate
[j
].peGreen
* 65535 / 255;
289 color
.blue
= __sysPalTemplate
[j
].peBlue
* 65535 / 255;
290 COLOR_sysPal
[i
] = __sysPalTemplate
[j
];
293 color
.flags
= DoRed
| DoGreen
| DoBlue
;
295 XStoreColor(display
, cs
->colorMap
, &color
);
297 /* Set EGA mapping if color is from the first or last eight */
300 COLOR_mapEGAPixel
[i
] = color
.pixel
;
301 else if (i
>= cs
->size
- 8 )
302 COLOR_mapEGAPixel
[i
- (cs
->size
- 16)] = color
.pixel
;
305 COLOR_PixelToPalette
= COLOR_PaletteToPixel
= NULL
;
307 COLOR_gapStart
= 256; COLOR_gapEnd
= -1;
309 COLOR_firstFree
= (cs
->size
> NB_RESERVED_COLORS
)?NB_RESERVED_COLORS
/2 : -1;
313 static BOOL
COLOR_BuildSharedMap(CSPACE
* cs
)
316 unsigned long sysPixel
[NB_RESERVED_COLORS
];
317 unsigned long* pixDynMapping
= NULL
;
318 unsigned long plane_masks
[1];
320 int color_ini_max
= 256;
321 int diff
, r
, g
, b
, max
= 256, bp
= 0, wp
= 1;
324 /* read "AllocSystemColors" from wine.conf */
326 color_ini_max
= PROFILE_GetWineIniInt( "options", "AllocSystemColors", 256);
327 if (color_ini_max
> 256) color_ini_max
= 256;
328 if (color_ini_max
< 20) color_ini_max
= 20;
329 dprintf_palette(stddeb
,"COLOR_Init: %d colors configured.\n",color_ini_max
);
331 dprintf_palette(stddeb
,"\tbuilding shared map - %i palette entries\n", cs
->size
);
333 /* Be nice and allocate system colors as read-only */
335 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
337 color
.red
= __sysPalTemplate
[i
].peRed
* 65535 / 255;
338 color
.green
= __sysPalTemplate
[i
].peGreen
* 65535 / 255;
339 color
.blue
= __sysPalTemplate
[i
].peBlue
* 65535 / 255;
340 color
.flags
= DoRed
| DoGreen
| DoBlue
;
342 if (!XAllocColor( display
, cSpace
.colorMap
, &color
))
348 dprintf_palette(stddeb
, "Not enough colors for the full system palette.\n");
350 bp
= BlackPixel(display
, DefaultScreen(display
));
351 wp
= WhitePixel(display
, DefaultScreen(display
));
353 max
= (0xffffffff)>>(32 - screenDepth
);
361 /* reinit color (XAllocColor() may change it)
362 * and map to the best shared colorcell */
364 color
.red
= __sysPalTemplate
[i
].peRed
* 65535 / 255;
365 color
.green
= __sysPalTemplate
[i
].peGreen
* 65535 / 255;
366 color
.blue
= __sysPalTemplate
[i
].peBlue
* 65535 / 255;
368 best
.pixel
= best
.red
= best
.green
= best
.blue
= 0;
369 for( c
.pixel
= 0, diff
= 0x7fffffff; c
.pixel
< max
; c
.pixel
+= step
)
371 XQueryColor(display
, cSpace
.colorMap
, &c
);
372 r
= (c
.red
- color
.red
)>>8;
373 g
= (c
.green
- color
.green
)>>8;
374 b
= (c
.blue
- color
.blue
)>>8;
376 if( r
< diff
) { best
= c
; diff
= r
; }
379 if( XAllocColor(display
, cSpace
.colorMap
, &best
) )
380 color
.pixel
= best
.pixel
;
381 else color
.pixel
= (i
< NB_RESERVED_COLORS
/2)? bp
: wp
;
384 sysPixel
[i
] = color
.pixel
;
386 dprintf_palette(stddeb
,"\tsyscolor(%lx) -> pixel %i\n",
387 *(COLORREF
*)(__sysPalTemplate
+i
), (int)color
.pixel
);
389 /* Set EGA mapping if color in the first or last eight */
392 COLOR_mapEGAPixel
[i
] = color
.pixel
;
393 else if (i
>= NB_RESERVED_COLORS
- 8 )
394 COLOR_mapEGAPixel
[i
- (NB_RESERVED_COLORS
-16)] = color
.pixel
;
397 /* now allocate changeable set */
399 if( !(cSpace
.flags
& COLOR_FIXED
) )
401 int c_min
= 0, c_max
= cs
->size
, c_val
;
403 dprintf_palette(stddeb
,"\tdynamic colormap... ");
405 /* comment this out if you want to debug palette init */
407 XGrabServer(display
);
409 /* let's become the first client that actually follows
410 * X guidelines and does binary search...
413 pixDynMapping
= (unsigned long*)xmalloc(sizeof(long)*cs
->size
);
414 while( c_max
- c_min
> 0 )
416 c_val
= (c_max
+ c_min
)/2 + (c_max
+ c_min
)%2;
418 if( !XAllocColorCells(display
, cs
->colorMap
, False
,
419 plane_masks
, 0, pixDynMapping
, c_val
) )
423 XFreeColors(display
, cs
->colorMap
, pixDynMapping
, c_val
, 0);
428 if( c_min
> color_ini_max
- NB_RESERVED_COLORS
)
429 c_min
= color_ini_max
- NB_RESERVED_COLORS
;
431 c_min
= (c_min
/2) + (c_min
/2); /* need even set for split palette */
434 if( !XAllocColorCells(display
, cs
->colorMap
, False
,
435 plane_masks
, 0, pixDynMapping
, c_min
) )
437 fprintf(stderr
,"Inexplicable failure during colorcell allocation.\n");
441 cs
->size
= c_min
+ NB_RESERVED_COLORS
;
443 XUngrabServer(display
);
445 dprintf_palette(stddeb
,"adjusted size %i colorcells\n", cs
->size
);
447 else if( cSpace
.flags
& COLOR_VIRTUAL
)
449 /* virtual colorspace - ToPhysical takes care of
450 * color translations but we have to allocate full palette
451 * to maintain compatibility
454 dprintf_palette(stddeb
,"\tvirtual colorspace - screendepth %i\n", screenDepth
);
456 else cs
->size
= NB_RESERVED_COLORS
; /* system palette only - however we can alloc a bunch
457 * of colors and map to them */
459 dprintf_palette(stddeb
,"Shared system palette uses %i colors.\n", cs
->size
);
461 /* set gap to account for pixel shortage. It has to be right in the center
462 * of the system palette because otherwise raster ops get screwed. */
464 if( cs
->size
>= 256 )
465 { COLOR_gapStart
= 256; COLOR_gapEnd
= -1; }
467 { COLOR_gapStart
= cs
->size
/2; COLOR_gapEnd
= 255 - cs
->size
/2; }
469 COLOR_firstFree
= ( cs
->size
> NB_RESERVED_COLORS
&&
470 (cs
->flags
& COLOR_VIRTUAL
|| !(cs
->flags
& COLOR_FIXED
)) )
471 ? NB_RESERVED_COLORS
/2 : -1;
473 COLOR_sysPal
= (PALETTEENTRY
*)xmalloc(sizeof(PALETTEENTRY
)*256);
475 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
477 if( screenDepth
<= 8 )
479 COLOR_PixelToPalette
= (int*)xmalloc(sizeof(int)*256);
480 memset( COLOR_PixelToPalette
, 0, 256*sizeof(int) );
483 /* for hicolor visuals PaletteToPixel mapping is used to skip
484 * RGB->pixel calculation in COLOR_ToPhysical().
487 COLOR_PaletteToPixel
= (int*)xmalloc(sizeof(int)*256);
489 for( i
= j
= 0; i
< 256; i
++ )
491 if( i
>= COLOR_gapStart
&& i
<= COLOR_gapEnd
)
493 COLOR_PaletteToPixel
[i
] = 0;
494 COLOR_sysPal
[i
].peFlags
= 0; /* mark as unused */
498 if( i
< NB_RESERVED_COLORS
/2 )
500 COLOR_PaletteToPixel
[i
] = sysPixel
[i
];
501 COLOR_sysPal
[i
] = __sysPalTemplate
[i
];
503 else if( i
>= 256 - NB_RESERVED_COLORS
/2 )
505 COLOR_PaletteToPixel
[i
] = sysPixel
[(i
+ NB_RESERVED_COLORS
) - 256];
506 COLOR_sysPal
[i
] = __sysPalTemplate
[(i
+ NB_RESERVED_COLORS
) - 256];
508 else if( pixDynMapping
)
509 COLOR_PaletteToPixel
[i
] = pixDynMapping
[j
++];
511 COLOR_PaletteToPixel
[i
] = i
;
513 dprintf_palette(stddeb
,"\tindex %i -> pixel %i\n", i
, COLOR_PaletteToPixel
[i
]);
515 if( COLOR_PixelToPalette
)
516 COLOR_PixelToPalette
[COLOR_PaletteToPixel
[i
]] = i
;
519 if( pixDynMapping
) free(pixDynMapping
);
524 /***********************************************************************
527 * Create the system palette.
529 static HPALETTE16
COLOR_InitPalette(void)
536 memset(COLOR_freeList
, 0, 256*sizeof(unsigned char));
538 /* calculate max palette size */
540 cSpace
.size
= visual
->map_entries
;
542 if (cSpace
.flags
& COLOR_PRIVATE
)
543 COLOR_BuildPrivateMap( &cSpace
);
545 COLOR_BuildSharedMap( &cSpace
);
547 /* Build free list */
549 if( COLOR_firstFree
!= -1 )
550 COLOR_FormatSystemPalette();
552 COLOR_FillDefaultColors();
554 /* create default palette (20 system colors) */
556 palPtr
= xmalloc( sizeof(LOGPALETTE
) + (NB_RESERVED_COLORS
-1)*sizeof(PALETTEENTRY
) );
557 if (!palPtr
) return FALSE
;
559 palPtr
->palVersion
= 0x300;
560 palPtr
->palNumEntries
= NB_RESERVED_COLORS
;
561 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
563 palPtr
->palPalEntry
[i
].peRed
= __sysPalTemplate
[i
].peRed
;
564 palPtr
->palPalEntry
[i
].peGreen
= __sysPalTemplate
[i
].peGreen
;
565 palPtr
->palPalEntry
[i
].peBlue
= __sysPalTemplate
[i
].peBlue
;
566 palPtr
->palPalEntry
[i
].peFlags
= 0;
568 hpalette
= CreatePalette( palPtr
);
570 palObj
= (PALETTEOBJ
*) GDI_GetObjPtr( hpalette
, PALETTE_MAGIC
);
572 palObj
->mapping
= xmalloc( sizeof(int) * 20 );
579 /***********************************************************************
580 * COLOR_Computeshifts
582 * Calculate conversion parameters for direct mapped visuals
584 static void COLOR_Computeshifts(unsigned long maskbits
, int *shift
, int *max
)
595 for(i
=0;!(maskbits
&1);i
++)
603 /***********************************************************************
606 * Initialize color map and system palette.
609 HPALETTE16
COLOR_Init(void)
611 visual
= DefaultVisual( display
, DefaultScreen(display
) );
613 dprintf_palette(stddeb
,"COLOR_Init: initializing palette manager...");
615 switch(visual
->class)
618 cSpace
.flags
|= COLOR_VIRTUAL
;
621 if (Options
.usePrivateMap
)
623 XSetWindowAttributes win_attr
;
625 cSpace
.flags
|= COLOR_PRIVATE
;
626 cSpace
.colorMap
= XCreateColormap( display
, rootWindow
,
630 if( rootWindow
!= DefaultRootWindow(display
) )
632 win_attr
.colormap
= cSpace
.colorMap
;
633 XChangeWindowAttributes( display
, rootWindow
,
634 CWColormap
, &win_attr
);
639 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
643 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
644 cSpace
.flags
|= COLOR_FIXED
;
645 COLOR_Graymax
= (1<<screenDepth
)-1;
649 cSpace
.flags
|= COLOR_VIRTUAL
;
651 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
652 cSpace
.flags
|= COLOR_FIXED
;
653 COLOR_Computeshifts(visual
->red_mask
, &COLOR_Redshift
, &COLOR_Redmax
);
654 COLOR_Computeshifts(visual
->green_mask
, &COLOR_Greenshift
, &COLOR_Greenmax
);
655 COLOR_Computeshifts(visual
->blue_mask
, &COLOR_Blueshift
, &COLOR_Bluemax
);
659 dprintf_palette(stddeb
," visual class %i\n", visual
->class);
661 return COLOR_InitPalette();
664 /***********************************************************************
667 * Free external colors we grabbed in the FillDefaultPalette()
669 void COLOR_Cleanup(void)
671 if( COLOR_gapFilled
)
672 XFreeColors(display
, cSpace
.colorMap
,
673 (unsigned long*)(COLOR_PaletteToPixel
+ COLOR_gapStart
),
677 /***********************************************************************
680 * Check whether 'color' can be represented with a solid color.
682 BOOL32
COLOR_IsSolid( COLORREF color
)
685 const PALETTEENTRY
*pEntry
= COLOR_sysPal
;
687 if (color
& 0xff000000) return TRUE
; /* indexed color */
689 if (!color
|| (color
== 0xffffff)) return TRUE
; /* black or white */
691 for (i
= 0; i
< 256 ; i
++, pEntry
++)
693 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
694 if ((GetRValue(color
) == pEntry
->peRed
) &&
695 (GetGValue(color
) == pEntry
->peGreen
) &&
696 (GetBValue(color
) == pEntry
->peBlue
)) return TRUE
;
702 /***********************************************************************
703 * COLOR_PaletteLookupPixel
705 int COLOR_PaletteLookupPixel( PALETTEENTRY
* palPalEntry
, int size
,
706 int* mapping
, COLORREF col
, BOOL32 skipReserved
)
708 int i
, best
= 0, diff
= 0x7fffffff;
711 for( i
= 0; i
< size
&& diff
; i
++ )
713 if( !(palPalEntry
[i
].peFlags
& PC_SYS_USED
) ||
714 (skipReserved
&& palPalEntry
[i
].peFlags
& PC_SYS_RESERVED
) )
717 r
= palPalEntry
[i
].peRed
- GetRValue(col
);
718 g
= palPalEntry
[i
].peGreen
- GetGValue(col
);
719 b
= palPalEntry
[i
].peBlue
- GetBValue(col
);
723 if( r
< diff
) { best
= i
; diff
= r
; }
725 return (mapping
) ? mapping
[best
] : best
;
728 /***********************************************************************
729 * COLOR_LookupSystemPixel
731 int COLOR_LookupSystemPixel(COLORREF col
)
733 int i
, best
= 0, diff
= 0x7fffffff;
734 int size
= COLOR_GetSystemPaletteSize();
737 for( i
= 0; i
< size
&& diff
; i
++ )
739 if( i
== NB_RESERVED_COLORS
/2 ) i
= size
- NB_RESERVED_COLORS
/2;
741 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
742 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
743 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
747 if( r
< diff
) { best
= i
; diff
= r
; }
750 return (COLOR_PaletteToPixel
)? COLOR_PaletteToPixel
[best
] : best
;
753 /***********************************************************************
754 * COLOR_PaletteLookupExactIndex
756 int COLOR_PaletteLookupExactIndex( PALETTEENTRY
* palPalEntry
, int size
,
760 BYTE r
= GetRValue(col
), g
= GetGValue(col
), b
= GetBValue(col
);
761 for( i
= 0; i
< size
; i
++ )
763 if( palPalEntry
[i
].peFlags
& PC_SYS_USED
) /* skips gap */
764 if( palPalEntry
[i
].peRed
== r
&&
765 palPalEntry
[i
].peGreen
== g
&&
766 palPalEntry
[i
].peBlue
== b
)
772 /***********************************************************************
773 * COLOR_LookupNearestColor
775 COLORREF
COLOR_LookupNearestColor( PALETTEENTRY
* palPalEntry
, int size
, COLORREF color
)
777 unsigned char spec_type
= color
>> 24;
780 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
782 if( spec_type
== 2 ) /* PALETTERGB */
784 (palPalEntry
+ COLOR_PaletteLookupPixel(palPalEntry
,size
,NULL
,color
,FALSE
));
786 else if( spec_type
== 1 ) /* PALETTEINDEX */
787 if( (i
= color
& 0x0000ffff) >= size
)
789 fprintf(stderr
, "\tRGB(%lx) : idx %d is out of bounds, assuming NULL\n", color
, i
);
790 color
= *(COLORREF
*)palPalEntry
;
792 else color
= *(COLORREF
*)(palPalEntry
+ i
);
795 return (0x00ffffff & *(COLORREF
*)
796 (COLOR_sysPal
+ COLOR_PaletteLookupPixel(COLOR_sysPal
, 256, NULL
, color
, FALSE
)));
799 /***********************************************************************
802 * Return RGB color for given X pixel.
804 COLORREF
COLOR_ToLogical(int pixel
)
808 /* truecolor visual */
810 if (screenDepth
>= 24) return pixel
;
812 /* check for hicolor visuals first */
814 if ( cSpace
.flags
& COLOR_FIXED
&& !COLOR_Graymax
)
816 color
.red
= (pixel
>> COLOR_Redshift
) & COLOR_Redmax
;
817 color
.green
= (pixel
>> COLOR_Greenshift
) & COLOR_Greenmax
;
818 color
.blue
= (pixel
>> COLOR_Blueshift
) & COLOR_Bluemax
;
819 return RGB((color
.red
* 255)/COLOR_Redmax
,
820 (color
.green
* 255)/COLOR_Greenmax
,
821 (color
.blue
* 255)/COLOR_Bluemax
);
824 /* check if we can bypass X */
826 if ((screenDepth
<= 8) && (pixel
< 256) &&
827 !(cSpace
.flags
& (COLOR_VIRTUAL
| COLOR_FIXED
)) )
828 return ( *(COLORREF
*)(COLOR_sysPal
+
829 ((COLOR_PixelToPalette
)?COLOR_PixelToPalette
[pixel
]:pixel
)) ) & 0x00ffffff;
832 XQueryColor(display
, cSpace
.colorMap
, &color
);
833 return RGB(color
.red
>> 8, color
.green
>> 8, color
.blue
>> 8);
837 /***********************************************************************
840 * Return the physical color closest to 'color'.
842 int COLOR_ToPhysical( DC
*dc
, COLORREF color
)
845 unsigned char spec_type
;
846 HPALETTE16 hPal
= (dc
)? dc
->w
.hPalette
: STOCK_DEFAULT_PALETTE
;
848 spec_type
= color
>> 24;
850 if( spec_type
== 0xff )
852 spec_type
= 0; /* 'write' seems to need that for 'Page 1' text */
858 fprintf(stderr
, "COLOR_ToPhysical : invalid RGB specifier for: %08lx\n", color
);
862 if (dc
&& (dc
->w
.bitsPerPixel
== 1) && (spec_type
== 0))
865 if (((color
>> 16) & 0xff) +
866 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2)
867 return 1; /* white */
868 else return 0; /* black */
871 if ( cSpace
.flags
& COLOR_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
;
879 PALETTEOBJ
* palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hPal
, PALETTE_MAGIC
);
883 case 2: /* PALETTERGB - not sure if we really need to search palette */
885 idx
= COLOR_PaletteLookupPixel( palPtr
->logpalette
.palPalEntry
,
886 palPtr
->logpalette
.palNumEntries
,
889 if( palPtr
->mapping
) return palPtr
->mapping
[idx
];
891 color
= *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ idx
);
894 case 1: /* PALETTEINDEX */
896 if ( (idx
= color
& 0xffff) >= palPtr
->logpalette
.palNumEntries
)
898 fprintf(stderr
, "\tRGB(%lx) : idx %d is out of bounds, assuming black\n", color
, idx
);
902 if( palPtr
->mapping
) return palPtr
->mapping
[idx
];
904 color
= *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ idx
);
906 /* fall through and out */
912 red
= GetRValue(color
); green
= GetGValue(color
); blue
= GetBValue(color
);
916 /* grayscale only; return scaled value */
917 return ( (red
* 30 + green
* 69 + blue
* 11) * COLOR_Graymax
) / 25500;
921 /* scale each individually and construct the TrueColor pixel value */
922 if (COLOR_Redmax
!= 255) red
= (red
* COLOR_Redmax
) / 255;
923 if (COLOR_Greenmax
!= 255) green
= (green
* COLOR_Greenmax
) / 255;
924 if (COLOR_Bluemax
!= 255) blue
= (blue
* COLOR_Bluemax
) / 255;
926 return (red
<< COLOR_Redshift
) | (green
<< COLOR_Greenshift
) | (blue
<< COLOR_Blueshift
);
931 PALETTEOBJ
* palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hPal
, PALETTE_MAGIC
);
933 /* palPtr can be NULL when DC is being destroyed */
935 if( !palPtr
) return 0;
936 else if( !palPtr
->mapping
)
937 dprintf_palette(stddeb
,"\tpalette %04x is not realized\n", dc
->w
.hPalette
);
939 switch(spec_type
) /* we have to peruse DC and system palette */
943 index
= COLOR_PaletteLookupPixel( COLOR_sysPal
, 256,
944 COLOR_PaletteToPixel
, color
, FALSE
);
946 /* dprintf_palette(stddeb,"\tRGB(%lx) -> pixel %i\n", color, index);
949 case 1: /* PALETTEINDEX */
950 index
= color
& 0xffff;
952 if( index
>= palPtr
->logpalette
.palNumEntries
)
953 fprintf(stderr
, "\tRGB(%lx) : index %i is out of bounds\n", color
, index
);
954 else if( palPtr
->mapping
) index
= palPtr
->mapping
[index
];
956 /* dprintf_palette(stddeb,"\tPALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
959 case 2: /* PALETTERGB */
960 index
= COLOR_PaletteLookupPixel( palPtr
->logpalette
.palPalEntry
,
961 palPtr
->logpalette
.palNumEntries
,
962 palPtr
->mapping
, color
, FALSE
);
963 /* dprintf_palette(stddeb,"\tPALETTERGB(%lx) -> pixel %i\n", color, index);
972 /***********************************************************************
975 * Set the color-mapping table for selected palette.
976 * Return number of entries which mapping has changed.
978 int COLOR_SetMapping( PALETTEOBJ
* palPtr
, BOOL32 mapOnly
)
982 int prevMapping
= (palPtr
->mapping
) ? 1 : 0;
985 /* reset dynamic system palette entries */
987 if( !mapOnly
&& COLOR_firstFree
!= -1)
988 COLOR_FormatSystemPalette();
990 /* initialize palette mapping table */
992 palPtr
->mapping
= (int*)xrealloc(palPtr
->mapping
, sizeof(int)*
993 palPtr
->logpalette
.palNumEntries
);
995 for( i
= 0; i
< palPtr
->logpalette
.palNumEntries
; i
++ )
1000 switch( palPtr
->logpalette
.palPalEntry
[i
].peFlags
& 0x07 )
1002 case PC_EXPLICIT
: /* palette entries are indices into system palette */
1003 index
= *(WORD
*)(palPtr
->logpalette
.palPalEntry
+ i
);
1004 if( index
> 255 || (index
>= COLOR_gapStart
&& index
<= COLOR_gapEnd
) )
1006 fprintf(stderr
,"PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index
);
1011 case PC_RESERVED
: /* forbid future mappings to this entry */
1012 flag
|= PC_SYS_RESERVED
;
1015 default: /* try to collapse identical colors */
1016 index
= COLOR_PaletteLookupExactIndex(COLOR_sysPal
, 256,
1017 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
));
1022 if( COLOR_firstFree
> 0 && !(cSpace
.flags
& COLOR_FIXED
) )
1025 index
= COLOR_firstFree
; /* ought to be available */
1026 COLOR_firstFree
= COLOR_freeList
[index
];
1028 color
.pixel
= (COLOR_PaletteToPixel
) ? COLOR_PaletteToPixel
[index
] : index
;
1029 color
.red
= palPtr
->logpalette
.palPalEntry
[i
].peRed
<< 8;
1030 color
.green
= palPtr
->logpalette
.palPalEntry
[i
].peGreen
<< 8;
1031 color
.blue
= palPtr
->logpalette
.palPalEntry
[i
].peBlue
<< 8;
1032 color
.flags
= DoRed
| DoGreen
| DoBlue
;
1033 XStoreColor(display
, cSpace
.colorMap
, &color
);
1035 COLOR_sysPal
[index
] = palPtr
->logpalette
.palPalEntry
[i
];
1036 COLOR_sysPal
[index
].peFlags
= flag
;
1037 COLOR_freeList
[index
] = 0;
1039 if( COLOR_PaletteToPixel
) index
= COLOR_PaletteToPixel
[index
];
1042 else if ( cSpace
.flags
& COLOR_VIRTUAL
)
1044 index
= COLOR_ToPhysical( NULL
, 0x00ffffff &
1045 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
));
1049 /* we have to map to existing entry in the system palette */
1051 index
= COLOR_PaletteLookupPixel(COLOR_sysPal
, 256, NULL
,
1052 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
), TRUE
);
1054 palPtr
->logpalette
.palPalEntry
[i
].peFlags
|= PC_SYS_USED
;
1056 if( COLOR_PaletteToPixel
) index
= COLOR_PaletteToPixel
[index
];
1060 if( !prevMapping
|| palPtr
->mapping
[i
] != index
) iRemapped
++;
1061 palPtr
->mapping
[i
] = index
;
1063 dprintf_palette(stddeb
,"\tentry %i (%lx) -> pixel %i\n", i
,
1064 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
), index
);