4 * Copyright 1993 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
18 /* Palette indexed mode:
20 * logical palette -> mapping -> pixel
23 * Windows needs contiguous color space ( from 0 to n ) but
24 * it is possible only with the private colormap. Otherwise we
25 * have to map DC palette indices to real pixel values. With
26 * private colormaps it boils down to the identity mapping. The
27 * other special case is when we have a fixed color visual with
28 * the screendepth > 8 - we abandon palette mappings altogether
29 * because pixel values can be calculated without X server
32 * Windows palette manager is described in the
33 * http://premium.microsoft.com/msdn/library/techart/f30/f34/f40/d4d/sa942.htm
41 INT32 monoPlane
; /* bit plane different for white and black pixels */
43 INT32 (*mapColor
)( DC
*, COLORREF
);
46 static CSPACE cSpace
= {0, 0, 0};
48 static int COLOR_Redshift
= 0; /* to handle abortive COLOR_VIRTUAL visuals */
49 static int COLOR_Redmax
= 0;
50 static int COLOR_Greenshift
= 0;
51 static int COLOR_Greenmax
= 0;
52 static int COLOR_Blueshift
= 0;
53 static int COLOR_Bluemax
= 0;
54 static int COLOR_Graymax
= 0;
56 /* System color space.
58 * First 10 and last 10 colors in COLOR_sysPalette are
59 * "guarded". RealizePalette changes only the rest of colorcells. For
60 * currently inactive window it changes only DC palette mappings.
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;
71 static int COLOR_max
= 256;
73 /* First free dynamic color cell, 0 = full palette, -1 = fixed palette */
74 static int COLOR_firstFree
= 0;
75 static unsigned char COLOR_freeList
[256];
77 /* Maps entry in the system palette to X pixel value */
78 int* COLOR_PaletteToPixel
= NULL
;
80 /* Maps pixel to the entry in the system palette */
81 int* COLOR_PixelToPalette
= NULL
;
83 static const PALETTEENTRY __sysPalTemplate
[NB_RESERVED_COLORS
] =
85 /* first 10 entries in the system palette */
86 /* red green blue flags */
87 { 0x00, 0x00, 0x00, PC_SYS_USED
},
88 { 0x80, 0x00, 0x00, PC_SYS_USED
},
89 { 0x00, 0x80, 0x00, PC_SYS_USED
},
90 { 0x80, 0x80, 0x00, PC_SYS_USED
},
91 { 0x00, 0x00, 0x80, PC_SYS_USED
},
92 { 0x80, 0x00, 0x80, PC_SYS_USED
},
93 { 0x00, 0x80, 0x80, PC_SYS_USED
},
94 { 0xc0, 0xc0, 0xc0, PC_SYS_USED
},
95 { 0xc0, 0xdc, 0xc0, PC_SYS_USED
},
96 { 0xa6, 0xca, 0xf0, PC_SYS_USED
},
98 /* ... c_min/2 dynamic colorcells */
100 /* ... gap (for sparse palettes) */
102 /* ... c_min/2 dynamic colorcells */
104 { 0xff, 0xfb, 0xf0, PC_SYS_USED
},
105 { 0xa0, 0xa0, 0xa4, PC_SYS_USED
},
106 { 0x80, 0x80, 0x80, PC_SYS_USED
},
107 { 0xff, 0x00, 0x00, PC_SYS_USED
},
108 { 0x00, 0xff, 0x00, PC_SYS_USED
},
109 { 0xff, 0xff, 0x00, PC_SYS_USED
},
110 { 0x00, 0x00, 0xff, PC_SYS_USED
},
111 { 0xff, 0x00, 0xff, PC_SYS_USED
},
112 { 0x00, 0xff, 0xff, PC_SYS_USED
},
113 { 0xff, 0xff, 0xff, PC_SYS_USED
} /* last 10 */
116 /* Map an EGA index (0..15) to a pixel value in the system color space. */
118 int COLOR_mapEGAPixel
[16];
120 /***********************************************************************
121 * Misc auxiliary functions
123 Colormap
COLOR_GetColormap(void)
125 return cSpace
.colorMap
;
128 BOOL32
COLOR_GetMonoPlane(INT32
* plane
)
130 *plane
= cSpace
.monoPlane
;
131 return (cSpace
.flags
& COLOR_WHITESET
) ? TRUE
: FALSE
;
134 UINT16
COLOR_GetSystemPaletteSize(void)
136 return (cSpace
.flags
& COLOR_PRIVATE
) ? cSpace
.size
: 256;
139 UINT16
COLOR_GetSystemPaletteFlags(void)
144 const PALETTEENTRY
* COLOR_GetSystemPaletteTemplate(void)
146 return __sysPalTemplate
;
149 COLORREF
COLOR_GetSystemPaletteEntry(UINT32 i
)
151 return *(COLORREF
*)(COLOR_sysPal
+ i
) & 0x00ffffff;
154 void COLOR_FormatSystemPalette(void)
156 /* Build free list so we'd have an easy way to find
157 * out if there are any available colorcells.
160 int i
, j
= COLOR_firstFree
= NB_RESERVED_COLORS
/2;
162 COLOR_sysPal
[j
].peFlags
= 0;
163 for( i
= NB_RESERVED_COLORS
/2 + 1 ; i
< 256 - NB_RESERVED_COLORS
/2 ; i
++ )
164 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
166 COLOR_sysPal
[i
].peFlags
= 0; /* unused tag */
167 COLOR_freeList
[j
] = i
; /* next */
170 COLOR_freeList
[j
] = 0;
173 BOOL32
COLOR_CheckSysColor(COLORREF c
)
176 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
177 if( c
== (*(COLORREF
*)(__sysPalTemplate
+ i
) & 0x00ffffff) )
182 /***********************************************************************
183 * Colormap Initialization
185 static void COLOR_FillDefaultColors(void)
187 /* initialize unused entries to what Windows uses as a color
188 * cube - based on Greg Kreider's code.
192 int red
, no_r
, inc_r
;
193 int green
, no_g
, inc_g
;
194 int blue
, no_b
, inc_b
;
196 if (cSpace
.size
<= NB_RESERVED_COLORS
)
198 while (i
*i
*i
< (cSpace
.size
- NB_RESERVED_COLORS
)) i
++;
199 no_r
= no_g
= no_b
= --i
;
200 if ((no_r
* (no_g
+1) * no_b
) < (cSpace
.size
- NB_RESERVED_COLORS
)) no_g
++;
201 if ((no_r
* no_g
* (no_b
+1)) < (cSpace
.size
- NB_RESERVED_COLORS
)) no_b
++;
202 inc_r
= (255 - NB_COLORCUBE_START_INDEX
)/no_r
;
203 inc_g
= (255 - NB_COLORCUBE_START_INDEX
)/no_g
;
204 inc_b
= (255 - NB_COLORCUBE_START_INDEX
)/no_b
;
206 idx
= COLOR_firstFree
;
209 for (blue
= NB_COLORCUBE_START_INDEX
; blue
< 256 && idx
; blue
+= inc_b
)
210 for (green
= NB_COLORCUBE_START_INDEX
; green
< 256 && idx
; green
+= inc_g
)
211 for (red
= NB_COLORCUBE_START_INDEX
; red
< 256 && idx
; red
+= inc_r
)
215 if( red
== NB_COLORCUBE_START_INDEX
&& green
== red
&& blue
== green
) continue;
217 COLOR_sysPal
[idx
].peRed
= red
;
218 COLOR_sysPal
[idx
].peGreen
= green
;
219 COLOR_sysPal
[idx
].peBlue
= blue
;
223 if( cSpace
.flags
& COLOR_VIRTUAL
)
225 if (COLOR_Redmax
!= 255) no_r
= (red
* COLOR_Redmax
) / 255;
226 if (COLOR_Greenmax
!= 255) no_g
= (green
* COLOR_Greenmax
) / 255;
227 if (COLOR_Bluemax
!= 255) no_b
= (blue
* COLOR_Bluemax
) / 255;
229 COLOR_PaletteToPixel
[idx
] = (no_r
<< COLOR_Redshift
) | (no_g
<< COLOR_Greenshift
) | (no_b
<< COLOR_Blueshift
);
231 else if( !(cSpace
.flags
& COLOR_FIXED
) )
233 XColor color
= { color
.pixel
= (COLOR_PaletteToPixel
)? COLOR_PaletteToPixel
[idx
] : idx
,
234 COLOR_sysPal
[idx
].peRed
<< 8,
235 COLOR_sysPal
[idx
].peGreen
<< 8,
236 COLOR_sysPal
[idx
].peGreen
<< 8,
237 (DoRed
| DoGreen
| DoBlue
) };
238 TSXStoreColor(display
, cSpace
.colorMap
, &color
);
241 idx
= COLOR_freeList
[idx
];
244 /* try to fill some entries in the "gap" with
245 * what's already in the colormap - they will be
246 * mappable to but not changeable. */
248 if( COLOR_gapStart
< COLOR_gapEnd
&& COLOR_PixelToPalette
)
253 max
= COLOR_max
- (256 - (COLOR_gapEnd
- COLOR_gapStart
));
254 for ( i
= 0, idx
= COLOR_gapStart
; i
< 256 && idx
<= COLOR_gapEnd
; i
++ )
255 if( COLOR_PixelToPalette
[i
] == 0 )
259 TSXQueryColor(display
, cSpace
.colorMap
, &xc
);
260 r
= xc
.red
>>8; g
= xc
.green
>>8; b
= xc
.blue
>>8;
262 if( xc
.pixel
< 256 && COLOR_CheckSysColor(RGB(r
, g
, b
)) &&
263 TSXAllocColor(display
, cSpace
.colorMap
, &xc
) )
265 COLOR_PixelToPalette
[xc
.pixel
] = idx
;
266 COLOR_PaletteToPixel
[idx
] = xc
.pixel
;
267 *(COLORREF
*)(COLOR_sysPal
+ idx
) = RGB(r
, g
, b
);
268 COLOR_sysPal
[idx
++].peFlags
|= PC_SYS_USED
;
269 if( --max
<= 0 ) break;
272 COLOR_gapFilled
= idx
- COLOR_gapStart
;
276 /***********************************************************************
277 * COLOR_BuildPrivateMap/COLOR_BuildSharedMap
279 * Allocate colorcells and initialize mapping tables.
281 static BOOL32
COLOR_BuildPrivateMap(CSPACE
* cs
)
283 /* Private colormap - identity mapping */
288 COLOR_sysPal
= (PALETTEENTRY
*)xmalloc(sizeof(PALETTEENTRY
)*cs
->size
);
290 TRACE(palette
,"Building private map - %i palette entries\n", cs
->size
);
292 /* Allocate system palette colors */
294 for( i
=0; i
< cs
->size
; i
++ )
296 if( i
< NB_RESERVED_COLORS
/2 )
298 color
.red
= __sysPalTemplate
[i
].peRed
* 65535 / 255;
299 color
.green
= __sysPalTemplate
[i
].peGreen
* 65535 / 255;
300 color
.blue
= __sysPalTemplate
[i
].peBlue
* 65535 / 255;
301 COLOR_sysPal
[i
] = __sysPalTemplate
[i
];
303 else if( i
>= cs
->size
- NB_RESERVED_COLORS
/2 )
305 int j
= NB_RESERVED_COLORS
+ i
- cs
->size
;
306 color
.red
= __sysPalTemplate
[j
].peRed
* 65535 / 255;
307 color
.green
= __sysPalTemplate
[j
].peGreen
* 65535 / 255;
308 color
.blue
= __sysPalTemplate
[j
].peBlue
* 65535 / 255;
309 COLOR_sysPal
[i
] = __sysPalTemplate
[j
];
312 color
.flags
= DoRed
| DoGreen
| DoBlue
;
314 TSXStoreColor(display
, cs
->colorMap
, &color
);
316 /* Set EGA mapping if color is from the first or last eight */
319 COLOR_mapEGAPixel
[i
] = color
.pixel
;
320 else if (i
>= cs
->size
- 8 )
321 COLOR_mapEGAPixel
[i
- (cs
->size
- 16)] = color
.pixel
;
324 COLOR_PixelToPalette
= COLOR_PaletteToPixel
= NULL
;
326 COLOR_gapStart
= 256; COLOR_gapEnd
= -1;
328 COLOR_firstFree
= (cs
->size
> NB_RESERVED_COLORS
)?NB_RESERVED_COLORS
/2 : -1;
332 static BOOL32
COLOR_BuildSharedMap(CSPACE
* cs
)
335 unsigned long sysPixel
[NB_RESERVED_COLORS
];
336 unsigned long* pixDynMapping
= NULL
;
337 unsigned long plane_masks
[1];
339 int diff
, r
, g
, b
, max
= 256, bp
= 0, wp
= 1;
342 /* read "AllocSystemColors" from wine.conf */
344 COLOR_max
= PROFILE_GetWineIniInt( "options", "AllocSystemColors", 256);
345 if (COLOR_max
> 256) COLOR_max
= 256;
346 else if (COLOR_max
< 20) COLOR_max
= 20;
347 TRACE(palette
,"%d colors configured.\n", COLOR_max
);
349 TRACE(palette
,"Building shared map - %i palette entries\n", cs
->size
);
351 /* Be nice and allocate system colors as read-only */
353 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
355 color
.red
= __sysPalTemplate
[i
].peRed
* 65535 / 255;
356 color
.green
= __sysPalTemplate
[i
].peGreen
* 65535 / 255;
357 color
.blue
= __sysPalTemplate
[i
].peBlue
* 65535 / 255;
358 color
.flags
= DoRed
| DoGreen
| DoBlue
;
360 if (!TSXAllocColor( display
, cSpace
.colorMap
, &color
))
366 WARN(palette
, "Not enough colors for the full system palette.\n");
368 bp
= BlackPixel(display
, DefaultScreen(display
));
369 wp
= WhitePixel(display
, DefaultScreen(display
));
371 max
= (0xffffffff)>>(32 - screenDepth
);
379 /* reinit color (XAllocColor() may change it)
380 * and map to the best shared colorcell */
382 color
.red
= __sysPalTemplate
[i
].peRed
* 65535 / 255;
383 color
.green
= __sysPalTemplate
[i
].peGreen
* 65535 / 255;
384 color
.blue
= __sysPalTemplate
[i
].peBlue
* 65535 / 255;
386 best
.pixel
= best
.red
= best
.green
= best
.blue
= 0;
387 for( c
.pixel
= 0, diff
= 0x7fffffff; c
.pixel
< max
; c
.pixel
+= step
)
389 TSXQueryColor(display
, cSpace
.colorMap
, &c
);
390 r
= (c
.red
- color
.red
)>>8;
391 g
= (c
.green
- color
.green
)>>8;
392 b
= (c
.blue
- color
.blue
)>>8;
394 if( r
< diff
) { best
= c
; diff
= r
; }
397 if( TSXAllocColor(display
, cSpace
.colorMap
, &best
) )
398 color
.pixel
= best
.pixel
;
399 else color
.pixel
= (i
< NB_RESERVED_COLORS
/2)? bp
: wp
;
402 sysPixel
[i
] = color
.pixel
;
404 TRACE(palette
,"syscolor(%lx) -> pixel %i\n",
405 *(COLORREF
*)(__sysPalTemplate
+i
), (int)color
.pixel
);
407 /* Set EGA mapping if color in the first or last eight */
410 COLOR_mapEGAPixel
[i
] = color
.pixel
;
411 else if (i
>= NB_RESERVED_COLORS
- 8 )
412 COLOR_mapEGAPixel
[i
- (NB_RESERVED_COLORS
-16)] = color
.pixel
;
415 /* now allocate changeable set */
417 if( !(cSpace
.flags
& COLOR_FIXED
) )
419 int c_min
= 0, c_max
= cs
->size
, c_val
;
421 TRACE(palette
,"Dynamic colormap... \n");
423 /* comment this out if you want to debug palette init */
425 TSXGrabServer(display
);
427 /* let's become the first client that actually follows
428 * X guidelines and does binary search...
431 pixDynMapping
= (unsigned long*)xmalloc(sizeof(long)*cs
->size
);
432 while( c_max
- c_min
> 0 )
434 c_val
= (c_max
+ c_min
)/2 + (c_max
+ c_min
)%2;
436 if( !TSXAllocColorCells(display
, cs
->colorMap
, False
,
437 plane_masks
, 0, pixDynMapping
, c_val
) )
441 TSXFreeColors(display
, cs
->colorMap
, pixDynMapping
, c_val
, 0);
446 if( c_min
> COLOR_max
- NB_RESERVED_COLORS
)
447 c_min
= COLOR_max
- NB_RESERVED_COLORS
;
449 c_min
= (c_min
/2) + (c_min
/2); /* need even set for split palette */
452 if( !TSXAllocColorCells(display
, cs
->colorMap
, False
,
453 plane_masks
, 0, pixDynMapping
, c_min
) )
455 WARN(palette
,"Inexplicable failure during colorcell allocation.\n");
459 cs
->size
= c_min
+ NB_RESERVED_COLORS
;
461 TSXUngrabServer(display
);
463 TRACE(palette
,"adjusted size %i colorcells\n", cs
->size
);
465 else if( cSpace
.flags
& COLOR_VIRTUAL
)
467 /* virtual colorspace - ToPhysical takes care of
468 * color translations but we have to allocate full palette
469 * to maintain compatibility
472 TRACE(palette
,"Virtual colorspace - screendepth %i\n", screenDepth
);
474 else cs
->size
= NB_RESERVED_COLORS
; /* system palette only - however we can alloc a bunch
475 * of colors and map to them */
477 TRACE(palette
,"Shared system palette uses %i colors.\n", cs
->size
);
479 /* set gap to account for pixel shortage. It has to be right in the center
480 * of the system palette because otherwise raster ops get screwed. */
482 if( cs
->size
>= 256 )
483 { COLOR_gapStart
= 256; COLOR_gapEnd
= -1; }
485 { COLOR_gapStart
= cs
->size
/2; COLOR_gapEnd
= 255 - cs
->size
/2; }
487 COLOR_firstFree
= ( cs
->size
> NB_RESERVED_COLORS
&&
488 (cs
->flags
& COLOR_VIRTUAL
|| !(cs
->flags
& COLOR_FIXED
)) )
489 ? NB_RESERVED_COLORS
/2 : -1;
491 COLOR_sysPal
= (PALETTEENTRY
*)xmalloc(sizeof(PALETTEENTRY
)*256);
493 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
495 if( screenDepth
<= 8 )
497 COLOR_PixelToPalette
= (int*)xmalloc(sizeof(int)*256);
498 memset( COLOR_PixelToPalette
, 0, 256*sizeof(int) );
501 /* for hicolor visuals PaletteToPixel mapping is used to skip
502 * RGB->pixel calculation in COLOR_ToPhysical().
505 COLOR_PaletteToPixel
= (int*)xmalloc(sizeof(int)*256);
507 for( i
= j
= 0; i
< 256; i
++ )
509 if( i
>= COLOR_gapStart
&& i
<= COLOR_gapEnd
)
511 COLOR_PaletteToPixel
[i
] = 0;
512 COLOR_sysPal
[i
].peFlags
= 0; /* mark as unused */
516 if( i
< NB_RESERVED_COLORS
/2 )
518 COLOR_PaletteToPixel
[i
] = sysPixel
[i
];
519 COLOR_sysPal
[i
] = __sysPalTemplate
[i
];
521 else if( i
>= 256 - NB_RESERVED_COLORS
/2 )
523 COLOR_PaletteToPixel
[i
] = sysPixel
[(i
+ NB_RESERVED_COLORS
) - 256];
524 COLOR_sysPal
[i
] = __sysPalTemplate
[(i
+ NB_RESERVED_COLORS
) - 256];
526 else if( pixDynMapping
)
527 COLOR_PaletteToPixel
[i
] = pixDynMapping
[j
++];
529 COLOR_PaletteToPixel
[i
] = i
;
531 TRACE(palette
,"index %i -> pixel %i\n", i
, COLOR_PaletteToPixel
[i
]);
533 if( COLOR_PixelToPalette
)
534 COLOR_PixelToPalette
[COLOR_PaletteToPixel
[i
]] = i
;
537 if( pixDynMapping
) free(pixDynMapping
);
541 /***********************************************************************
542 * COLOR_Computeshifts
544 * Calculate conversion parameters for direct mapped visuals
546 static void COLOR_Computeshifts(unsigned long maskbits
, int *shift
, int *max
)
557 for(i
=0;!(maskbits
&1);i
++)
565 /***********************************************************************
568 * Initialize color management.
570 BOOL32
COLOR_Init(void)
572 int mask
, white
, black
;
574 visual
= DefaultVisual( display
, DefaultScreen(display
) );
576 TRACE(palette
,"initializing palette manager...\n");
578 white
= WhitePixelOfScreen( screen
);
579 black
= BlackPixelOfScreen( screen
);
580 cSpace
.monoPlane
= 1;
581 for( mask
= 1; !((white
& mask
)^(black
& mask
)); mask
<<= 1 )
583 cSpace
.flags
= (white
& mask
) ? COLOR_WHITESET
: 0;
584 cSpace
.size
= visual
->map_entries
;
586 switch(visual
->class)
589 cSpace
.flags
|= COLOR_VIRTUAL
;
592 if (Options
.usePrivateMap
)
594 XSetWindowAttributes win_attr
;
596 cSpace
.colorMap
= TSXCreateColormap( display
, rootWindow
,
600 cSpace
.flags
|= (COLOR_PRIVATE
| COLOR_WHITESET
);
602 cSpace
.monoPlane
= 1;
603 for( white
= cSpace
.size
- 1; !(white
& 1); white
>>= 1 )
606 if( rootWindow
!= DefaultRootWindow(display
) )
608 win_attr
.colormap
= cSpace
.colorMap
;
609 TSXChangeWindowAttributes( display
, rootWindow
,
610 CWColormap
, &win_attr
);
615 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
619 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
620 cSpace
.flags
|= COLOR_FIXED
;
621 COLOR_Graymax
= (1<<screenDepth
)-1;
625 cSpace
.flags
|= COLOR_VIRTUAL
;
627 int *depths
,nrofdepths
;
628 /* FIXME: hack to detect XFree32 XF_VGA16 ... We just have
631 depths
=TSXListDepths(display
,DefaultScreen(display
),&nrofdepths
);
632 if ((nrofdepths
==2) && ((depths
[0]==4) || depths
[1]==4)) {
633 cSpace
.monoPlane
= 1;
634 for( white
= cSpace
.size
- 1; !(white
& 1); white
>>= 1 )
636 cSpace
.flags
= (white
& mask
) ? COLOR_WHITESET
: 0;
637 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
642 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
643 cSpace
.flags
|= COLOR_FIXED
;
644 COLOR_Computeshifts(visual
->red_mask
, &COLOR_Redshift
, &COLOR_Redmax
);
645 COLOR_Computeshifts(visual
->green_mask
, &COLOR_Greenshift
, &COLOR_Greenmax
);
646 COLOR_Computeshifts(visual
->blue_mask
, &COLOR_Blueshift
, &COLOR_Bluemax
);
651 TRACE(palette
," visual class %i (%i)\n",
652 visual
->class, cSpace
.monoPlane
);
654 memset(COLOR_freeList
, 0, 256*sizeof(unsigned char));
656 if (cSpace
.flags
& COLOR_PRIVATE
)
657 COLOR_BuildPrivateMap( &cSpace
);
659 COLOR_BuildSharedMap( &cSpace
);
661 /* Build free list */
663 if( COLOR_firstFree
!= -1 )
664 COLOR_FormatSystemPalette();
666 COLOR_FillDefaultColors();
671 /***********************************************************************
674 * Free external colors we grabbed in the FillDefaultPalette()
676 void COLOR_Cleanup(void)
678 if( COLOR_gapFilled
)
679 TSXFreeColors(display
, cSpace
.colorMap
,
680 (unsigned long*)(COLOR_PaletteToPixel
+ COLOR_gapStart
),
684 /***********************************************************************
687 * Check whether 'color' can be represented with a solid color.
689 BOOL32
COLOR_IsSolid( COLORREF color
)
692 const PALETTEENTRY
*pEntry
= COLOR_sysPal
;
694 if (color
& 0xff000000) return TRUE
; /* indexed color */
696 if (!color
|| (color
== 0xffffff)) return TRUE
; /* black or white */
698 for (i
= 0; i
< 256 ; i
++, pEntry
++)
700 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
701 if ((GetRValue(color
) == pEntry
->peRed
) &&
702 (GetGValue(color
) == pEntry
->peGreen
) &&
703 (GetBValue(color
) == pEntry
->peBlue
)) return TRUE
;
709 /***********************************************************************
710 * COLOR_PaletteLookupPixel
712 int COLOR_PaletteLookupPixel( PALETTEENTRY
* palPalEntry
, int size
,
713 int* mapping
, COLORREF col
, BOOL32 skipReserved
)
715 int i
, best
= 0, diff
= 0x7fffffff;
718 for( i
= 0; i
< size
&& diff
; i
++ )
720 if( !(palPalEntry
[i
].peFlags
& PC_SYS_USED
) ||
721 (skipReserved
&& palPalEntry
[i
].peFlags
& PC_SYS_RESERVED
) )
724 r
= palPalEntry
[i
].peRed
- GetRValue(col
);
725 g
= palPalEntry
[i
].peGreen
- GetGValue(col
);
726 b
= palPalEntry
[i
].peBlue
- GetBValue(col
);
730 if( r
< diff
) { best
= i
; diff
= r
; }
732 return (mapping
) ? mapping
[best
] : best
;
735 /***********************************************************************
736 * COLOR_LookupSystemPixel
738 int COLOR_LookupSystemPixel(COLORREF col
)
740 int i
, best
= 0, diff
= 0x7fffffff;
741 int size
= COLOR_GetSystemPaletteSize();
744 for( i
= 0; i
< size
&& diff
; i
++ )
746 if( i
== NB_RESERVED_COLORS
/2 )
748 int newi
= size
- NB_RESERVED_COLORS
/2;
752 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
753 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
754 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
758 if( r
< diff
) { best
= i
; diff
= r
; }
761 return (COLOR_PaletteToPixel
)? COLOR_PaletteToPixel
[best
] : best
;
764 /***********************************************************************
765 * COLOR_PaletteLookupExactIndex
767 int COLOR_PaletteLookupExactIndex( PALETTEENTRY
* palPalEntry
, int size
,
771 BYTE r
= GetRValue(col
), g
= GetGValue(col
), b
= GetBValue(col
);
772 for( i
= 0; i
< size
; i
++ )
774 if( palPalEntry
[i
].peFlags
& PC_SYS_USED
) /* skips gap */
775 if( palPalEntry
[i
].peRed
== r
&&
776 palPalEntry
[i
].peGreen
== g
&&
777 palPalEntry
[i
].peBlue
== b
)
783 /***********************************************************************
784 * COLOR_LookupNearestColor
786 COLORREF
COLOR_LookupNearestColor( PALETTEENTRY
* palPalEntry
, int size
, COLORREF color
)
788 unsigned char spec_type
= color
>> 24;
791 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
793 if( spec_type
== 2 ) /* PALETTERGB */
795 (palPalEntry
+ COLOR_PaletteLookupPixel(palPalEntry
,size
,NULL
,color
,FALSE
));
797 else if( spec_type
== 1 ) /* PALETTEINDEX */
798 if( (i
= color
& 0x0000ffff) >= size
)
800 WARN(palette
, "RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color
, i
);
801 color
= *(COLORREF
*)palPalEntry
;
803 else color
= *(COLORREF
*)(palPalEntry
+ i
);
806 return (0x00ffffff & *(COLORREF
*)
807 (COLOR_sysPal
+ COLOR_PaletteLookupPixel(COLOR_sysPal
, 256, NULL
, color
, FALSE
)));
810 /***********************************************************************
813 * Return RGB color for given X pixel.
815 COLORREF
COLOR_ToLogical(int pixel
)
820 /* truecolor visual */
822 if (screenDepth
>= 24) return pixel
;
825 /* check for hicolor visuals first */
827 if ( cSpace
.flags
& COLOR_FIXED
&& !COLOR_Graymax
)
829 color
.red
= (pixel
>> COLOR_Redshift
) & COLOR_Redmax
;
830 color
.green
= (pixel
>> COLOR_Greenshift
) & COLOR_Greenmax
;
831 color
.blue
= (pixel
>> COLOR_Blueshift
) & COLOR_Bluemax
;
832 return RGB((color
.red
* 255)/COLOR_Redmax
,
833 (color
.green
* 255)/COLOR_Greenmax
,
834 (color
.blue
* 255)/COLOR_Bluemax
);
837 /* check if we can bypass X */
839 if ((screenDepth
<= 8) && (pixel
< 256) &&
840 !(cSpace
.flags
& (COLOR_VIRTUAL
| COLOR_FIXED
)) )
841 return ( *(COLORREF
*)(COLOR_sysPal
+
842 ((COLOR_PixelToPalette
)?COLOR_PixelToPalette
[pixel
]:pixel
)) ) & 0x00ffffff;
845 TSXQueryColor(display
, cSpace
.colorMap
, &color
);
846 return RGB(color
.red
>> 8, color
.green
>> 8, color
.blue
>> 8);
850 /***********************************************************************
853 * Return the physical color closest to 'color'.
855 int COLOR_ToPhysical( DC
*dc
, COLORREF color
)
858 HPALETTE16 hPal
= (dc
)? dc
->w
.hPalette
: STOCK_DEFAULT_PALETTE
;
859 unsigned char spec_type
= color
>> 24;
860 PALETTEOBJ
* palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hPal
, PALETTE_MAGIC
);
862 if ( cSpace
.flags
& COLOR_FIXED
)
864 /* there is no colormap limitation; we are going to have to compute
865 * the pixel value from the visual information stored earlier
868 unsigned long red
, green
, blue
;
873 case 2: /* PALETTERGB - not sure if we really need to search palette */
875 idx
= COLOR_PaletteLookupPixel( palPtr
->logpalette
.palPalEntry
,
876 palPtr
->logpalette
.palNumEntries
,
879 if( palPtr
->mapping
)
881 GDI_HEAP_UNLOCK( hPal
);
882 return palPtr
->mapping
[idx
];
885 color
= *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ idx
);
888 case 1: /* PALETTEINDEX */
890 if( (idx
= color
& 0xffff) >= palPtr
->logpalette
.palNumEntries
)
892 WARN(palette
, "RGB(%lx) : idx %d is out of bounds, assuming black\n", color
, idx
);
893 GDI_HEAP_UNLOCK( hPal
);
897 if( palPtr
->mapping
)
899 GDI_HEAP_UNLOCK( hPal
);
900 return palPtr
->mapping
[idx
];
902 color
= *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ idx
);
907 /* fall through to RGB */
910 if( dc
&& (dc
->w
.bitsPerPixel
== 1) )
912 GDI_HEAP_UNLOCK( hPal
);
913 return (((color
>> 16) & 0xff) +
914 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? 1 : 0;
919 red
= GetRValue(color
); green
= GetGValue(color
); blue
= GetBValue(color
);
923 /* grayscale only; return scaled value */
924 GDI_HEAP_UNLOCK( hPal
);
925 return ( (red
* 30 + green
* 69 + blue
* 11) * COLOR_Graymax
) / 25500;
929 /* scale each individually and construct the TrueColor pixel value */
930 if (COLOR_Redmax
!= 255) red
= (red
* COLOR_Redmax
) / 255;
931 if (COLOR_Greenmax
!= 255) green
= (green
* COLOR_Greenmax
) / 255;
932 if (COLOR_Bluemax
!= 255) blue
= (blue
* COLOR_Bluemax
) / 255;
934 GDI_HEAP_UNLOCK( hPal
);
935 return (red
<< COLOR_Redshift
) | (green
<< COLOR_Greenshift
) | (blue
<< COLOR_Blueshift
);
941 /* palPtr can be NULL when DC is being destroyed */
943 if( !palPtr
) return 0;
944 else if( !palPtr
->mapping
)
945 WARN(palette
, "Palette %04x is not realized\n", dc
->w
.hPalette
);
947 switch(spec_type
) /* we have to peruse DC and system palette */
951 /* fall through to RGB */
954 if( dc
&& (dc
->w
.bitsPerPixel
== 1) )
956 GDI_HEAP_UNLOCK( hPal
);
957 return (((color
>> 16) & 0xff) +
958 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2) ? 1 : 0;
961 index
= COLOR_PaletteLookupPixel( COLOR_sysPal
, 256,
962 COLOR_PaletteToPixel
, color
, FALSE
);
964 /* TRACE(palette,"RGB(%lx) -> pixel %i\n", color, index);
967 case 1: /* PALETTEINDEX */
968 index
= color
& 0xffff;
970 if( index
>= palPtr
->logpalette
.palNumEntries
)
971 WARN(palette
, "RGB(%lx) : index %i is out of bounds\n", color
, index
);
972 else if( palPtr
->mapping
) index
= palPtr
->mapping
[index
];
974 /* TRACE(palette,"PALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
977 case 2: /* PALETTERGB */
978 index
= COLOR_PaletteLookupPixel( palPtr
->logpalette
.palPalEntry
,
979 palPtr
->logpalette
.palNumEntries
,
980 palPtr
->mapping
, color
, FALSE
);
981 /* TRACE(palette,"PALETTERGB(%lx) -> pixel %i\n", color, index);
987 GDI_HEAP_UNLOCK( hPal
);
991 /***********************************************************************
994 * Set the color-mapping table for selected palette.
995 * Return number of entries which mapping has changed.
997 int COLOR_SetMapping( PALETTEOBJ
* palPtr
, UINT32 uStart
, UINT32 uNum
, BOOL32 mapOnly
)
1000 int prevMapping
= (palPtr
->mapping
) ? 1 : 0;
1001 int index
, iRemapped
= 0;
1003 /* reset dynamic system palette entries */
1005 if( !mapOnly
&& COLOR_firstFree
!= -1)
1006 COLOR_FormatSystemPalette();
1008 /* initialize palette mapping table */
1010 palPtr
->mapping
= (int*)xrealloc(palPtr
->mapping
, sizeof(int)*
1011 palPtr
->logpalette
.palNumEntries
);
1013 for( uNum
+= uStart
; uStart
< uNum
; uStart
++ )
1018 switch( palPtr
->logpalette
.palPalEntry
[uStart
].peFlags
& 0x07 )
1020 case PC_EXPLICIT
: /* palette entries are indices into system palette */
1021 index
= *(WORD
*)(palPtr
->logpalette
.palPalEntry
+ uStart
);
1022 if( index
> 255 || (index
>= COLOR_gapStart
&& index
<= COLOR_gapEnd
) )
1024 WARN(palette
,"PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index
);
1029 case PC_RESERVED
: /* forbid future mappings to this entry */
1030 flag
|= PC_SYS_RESERVED
;
1033 default: /* try to collapse identical colors */
1034 index
= COLOR_PaletteLookupExactIndex(COLOR_sysPal
, 256,
1035 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ uStart
));
1040 if( COLOR_firstFree
> 0 && !(cSpace
.flags
& COLOR_FIXED
) )
1043 index
= COLOR_firstFree
; /* ought to be available */
1044 COLOR_firstFree
= COLOR_freeList
[index
];
1046 color
.pixel
= (COLOR_PaletteToPixel
) ? COLOR_PaletteToPixel
[index
] : index
;
1047 color
.red
= palPtr
->logpalette
.palPalEntry
[uStart
].peRed
<< 8;
1048 color
.green
= palPtr
->logpalette
.palPalEntry
[uStart
].peGreen
<< 8;
1049 color
.blue
= palPtr
->logpalette
.palPalEntry
[uStart
].peBlue
<< 8;
1050 color
.flags
= DoRed
| DoGreen
| DoBlue
;
1051 TSXStoreColor(display
, cSpace
.colorMap
, &color
);
1053 COLOR_sysPal
[index
] = palPtr
->logpalette
.palPalEntry
[uStart
];
1054 COLOR_sysPal
[index
].peFlags
= flag
;
1055 COLOR_freeList
[index
] = 0;
1057 if( COLOR_PaletteToPixel
) index
= COLOR_PaletteToPixel
[index
];
1060 else if ( cSpace
.flags
& COLOR_VIRTUAL
)
1062 index
= COLOR_ToPhysical( NULL
, 0x00ffffff &
1063 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ uStart
));
1067 /* we have to map to existing entry in the system palette */
1069 index
= COLOR_PaletteLookupPixel(COLOR_sysPal
, 256, NULL
,
1070 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ uStart
), TRUE
);
1072 palPtr
->logpalette
.palPalEntry
[uStart
].peFlags
|= PC_SYS_USED
;
1074 if( COLOR_PaletteToPixel
) index
= COLOR_PaletteToPixel
[index
];
1078 if( !prevMapping
|| palPtr
->mapping
[uStart
] != index
) iRemapped
++;
1079 palPtr
->mapping
[uStart
] = index
;
1081 TRACE(palette
,"entry %i (%lx) -> pixel %i\n", uStart
,
1082 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ uStart
), index
);