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;
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 UINT16
COLOR_GetSystemPaletteSize(void)
130 return (cSpace
.flags
& COLOR_PRIVATE
) ? cSpace
.size
: 256;
133 UINT16
COLOR_GetSystemPaletteFlags(void)
138 COLORREF
COLOR_GetSystemPaletteEntry(UINT32 i
)
140 return *(COLORREF
*)(COLOR_sysPal
+ i
) & 0x00ffffff;
143 void COLOR_FormatSystemPalette(void)
145 /* Build free list so we'd have an easy way to find
146 * out if there are any available colorcells.
149 int i
, j
= COLOR_firstFree
= NB_RESERVED_COLORS
/2;
151 COLOR_sysPal
[j
].peFlags
= 0;
152 for( i
= NB_RESERVED_COLORS
/2 + 1 ; i
< 256 - NB_RESERVED_COLORS
/2 ; i
++ )
153 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
155 COLOR_sysPal
[i
].peFlags
= 0; /* unused tag */
156 COLOR_freeList
[j
] = i
; /* next */
159 COLOR_freeList
[j
] = 0;
162 BOOL32
COLOR_CheckSysColor(COLORREF c
)
165 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
166 if( c
== (*(COLORREF
*)(__sysPalTemplate
+ i
) & 0x00ffffff) )
171 void COLOR_FillDefaultColors(void)
173 /* initialize unused entries to what Windows uses as a color
174 * cube - based on Greg Kreider's code.
178 int red
, no_r
, inc_r
;
179 int green
, no_g
, inc_g
;
180 int blue
, no_b
, inc_b
;
182 while (i
*i
*i
< (cSpace
.size
- NB_RESERVED_COLORS
)) i
++;
183 no_r
= no_g
= no_b
= --i
;
184 if ((no_r
* (no_g
+1) * no_b
) < (cSpace
.size
- NB_RESERVED_COLORS
)) no_g
++;
185 if ((no_r
* no_g
* (no_b
+1)) < (cSpace
.size
- NB_RESERVED_COLORS
)) no_b
++;
186 inc_r
= (255 - NB_COLORCUBE_START_INDEX
)/no_r
;
187 inc_g
= (255 - NB_COLORCUBE_START_INDEX
)/no_g
;
188 inc_b
= (255 - NB_COLORCUBE_START_INDEX
)/no_b
;
190 idx
= COLOR_firstFree
;
193 for (blue
= NB_COLORCUBE_START_INDEX
; blue
< 256 && idx
; blue
+= inc_b
)
194 for (green
= NB_COLORCUBE_START_INDEX
; green
< 256 && idx
; green
+= inc_g
)
195 for (red
= NB_COLORCUBE_START_INDEX
; red
< 256 && idx
; red
+= inc_r
)
199 if( red
== NB_COLORCUBE_START_INDEX
&& green
== red
&& blue
== green
) continue;
201 COLOR_sysPal
[idx
].peRed
= red
;
202 COLOR_sysPal
[idx
].peGreen
= green
;
203 COLOR_sysPal
[idx
].peBlue
= blue
;
207 if( cSpace
.flags
& COLOR_VIRTUAL
)
209 if (COLOR_Redmax
!= 255) no_r
= (red
* COLOR_Redmax
) / 255;
210 if (COLOR_Greenmax
!= 255) no_g
= (green
* COLOR_Greenmax
) / 255;
211 if (COLOR_Bluemax
!= 255) no_b
= (blue
* COLOR_Bluemax
) / 255;
213 COLOR_PaletteToPixel
[idx
] = (no_r
<< COLOR_Redshift
) | (no_g
<< COLOR_Greenshift
) | (no_b
<< COLOR_Blueshift
);
215 else if( !(cSpace
.flags
& COLOR_FIXED
) )
217 XColor color
= { color
.pixel
= (COLOR_PaletteToPixel
)? COLOR_PaletteToPixel
[idx
] : idx
,
218 COLOR_sysPal
[idx
].peRed
<< 8,
219 COLOR_sysPal
[idx
].peGreen
<< 8,
220 COLOR_sysPal
[idx
].peGreen
<< 8,
221 (DoRed
| DoGreen
| DoBlue
) };
222 XStoreColor(display
, cSpace
.colorMap
, &color
);
225 idx
= COLOR_freeList
[idx
];
228 /* try to fill some entries in the "gap" with
229 * what's already in the colormap - they will be
230 * mappable to but not changeable. */
232 if( COLOR_gapStart
< COLOR_gapEnd
&& COLOR_PixelToPalette
)
237 max
= COLOR_max
- (256 - (COLOR_gapEnd
- COLOR_gapStart
));
238 for ( i
= 0, idx
= COLOR_gapStart
; i
< 256 && idx
<= COLOR_gapEnd
; i
++ )
239 if( COLOR_PixelToPalette
[i
] == 0 )
243 XQueryColor(display
, cSpace
.colorMap
, &xc
);
244 r
= xc
.red
>>8; g
= xc
.green
>>8; b
= xc
.blue
>>8;
246 if( xc
.pixel
< 256 && COLOR_CheckSysColor(RGB(r
, g
, b
)) &&
247 XAllocColor(display
, cSpace
.colorMap
, &xc
) )
249 COLOR_PixelToPalette
[xc
.pixel
] = idx
;
250 COLOR_PaletteToPixel
[idx
] = xc
.pixel
;
251 *(COLORREF
*)(COLOR_sysPal
+ idx
) = RGB(r
, g
, b
);
252 COLOR_sysPal
[idx
++].peFlags
|= PC_SYS_USED
;
253 if( --max
<= 0 ) break;
256 COLOR_gapFilled
= idx
- COLOR_gapStart
;
260 /***********************************************************************
261 * COLOR_BuildPrivateMap/COLOR_BuildSharedMap
263 * Allocate colorcells and initialize mapping tables.
265 static BOOL32
COLOR_BuildPrivateMap(CSPACE
* cs
)
267 /* Private colormap - identity mapping */
272 COLOR_sysPal
= (PALETTEENTRY
*)xmalloc(sizeof(PALETTEENTRY
)*cs
->size
);
274 dprintf_palette(stddeb
,"\tbuilding private map - %i palette entries\n", cs
->size
);
276 /* Allocate system palette colors */
278 for( i
=0; i
< cs
->size
; i
++ )
280 if( i
< NB_RESERVED_COLORS
/2 )
282 color
.red
= __sysPalTemplate
[i
].peRed
* 65535 / 255;
283 color
.green
= __sysPalTemplate
[i
].peGreen
* 65535 / 255;
284 color
.blue
= __sysPalTemplate
[i
].peBlue
* 65535 / 255;
285 COLOR_sysPal
[i
] = __sysPalTemplate
[i
];
287 else if( i
>= cs
->size
- NB_RESERVED_COLORS
/2 )
289 int j
= NB_RESERVED_COLORS
+ i
- cs
->size
;
290 color
.red
= __sysPalTemplate
[j
].peRed
* 65535 / 255;
291 color
.green
= __sysPalTemplate
[j
].peGreen
* 65535 / 255;
292 color
.blue
= __sysPalTemplate
[j
].peBlue
* 65535 / 255;
293 COLOR_sysPal
[i
] = __sysPalTemplate
[j
];
296 color
.flags
= DoRed
| DoGreen
| DoBlue
;
298 XStoreColor(display
, cs
->colorMap
, &color
);
300 /* Set EGA mapping if color is from the first or last eight */
303 COLOR_mapEGAPixel
[i
] = color
.pixel
;
304 else if (i
>= cs
->size
- 8 )
305 COLOR_mapEGAPixel
[i
- (cs
->size
- 16)] = color
.pixel
;
308 COLOR_PixelToPalette
= COLOR_PaletteToPixel
= NULL
;
310 COLOR_gapStart
= 256; COLOR_gapEnd
= -1;
312 COLOR_firstFree
= (cs
->size
> NB_RESERVED_COLORS
)?NB_RESERVED_COLORS
/2 : -1;
316 static BOOL32
COLOR_BuildSharedMap(CSPACE
* cs
)
319 unsigned long sysPixel
[NB_RESERVED_COLORS
];
320 unsigned long* pixDynMapping
= NULL
;
321 unsigned long plane_masks
[1];
323 int diff
, r
, g
, b
, max
= 256, bp
= 0, wp
= 1;
326 /* read "AllocSystemColors" from wine.conf */
328 COLOR_max
= PROFILE_GetWineIniInt( "options", "AllocSystemColors", 256);
329 if (COLOR_max
> 256) COLOR_max
= 256;
330 else if (COLOR_max
< 20) COLOR_max
= 20;
331 dprintf_palette(stddeb
,"COLOR_Init: %d colors configured.\n", COLOR_max
);
333 dprintf_palette(stddeb
,"\tbuilding shared map - %i palette entries\n", cs
->size
);
335 /* Be nice and allocate system colors as read-only */
337 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
339 color
.red
= __sysPalTemplate
[i
].peRed
* 65535 / 255;
340 color
.green
= __sysPalTemplate
[i
].peGreen
* 65535 / 255;
341 color
.blue
= __sysPalTemplate
[i
].peBlue
* 65535 / 255;
342 color
.flags
= DoRed
| DoGreen
| DoBlue
;
344 if (!XAllocColor( display
, cSpace
.colorMap
, &color
))
350 dprintf_palette(stddeb
, "Not enough colors for the full system palette.\n");
352 bp
= BlackPixel(display
, DefaultScreen(display
));
353 wp
= WhitePixel(display
, DefaultScreen(display
));
355 max
= (0xffffffff)>>(32 - screenDepth
);
363 /* reinit color (XAllocColor() may change it)
364 * and map to the best shared colorcell */
366 color
.red
= __sysPalTemplate
[i
].peRed
* 65535 / 255;
367 color
.green
= __sysPalTemplate
[i
].peGreen
* 65535 / 255;
368 color
.blue
= __sysPalTemplate
[i
].peBlue
* 65535 / 255;
370 best
.pixel
= best
.red
= best
.green
= best
.blue
= 0;
371 for( c
.pixel
= 0, diff
= 0x7fffffff; c
.pixel
< max
; c
.pixel
+= step
)
373 XQueryColor(display
, cSpace
.colorMap
, &c
);
374 r
= (c
.red
- color
.red
)>>8;
375 g
= (c
.green
- color
.green
)>>8;
376 b
= (c
.blue
- color
.blue
)>>8;
378 if( r
< diff
) { best
= c
; diff
= r
; }
381 if( XAllocColor(display
, cSpace
.colorMap
, &best
) )
382 color
.pixel
= best
.pixel
;
383 else color
.pixel
= (i
< NB_RESERVED_COLORS
/2)? bp
: wp
;
386 sysPixel
[i
] = color
.pixel
;
388 dprintf_palette(stddeb
,"\tsyscolor(%lx) -> pixel %i\n",
389 *(COLORREF
*)(__sysPalTemplate
+i
), (int)color
.pixel
);
391 /* Set EGA mapping if color in the first or last eight */
394 COLOR_mapEGAPixel
[i
] = color
.pixel
;
395 else if (i
>= NB_RESERVED_COLORS
- 8 )
396 COLOR_mapEGAPixel
[i
- (NB_RESERVED_COLORS
-16)] = color
.pixel
;
399 /* now allocate changeable set */
401 if( !(cSpace
.flags
& COLOR_FIXED
) )
403 int c_min
= 0, c_max
= cs
->size
, c_val
;
405 dprintf_palette(stddeb
,"\tdynamic colormap... ");
407 /* comment this out if you want to debug palette init */
409 XGrabServer(display
);
411 /* let's become the first client that actually follows
412 * X guidelines and does binary search...
415 pixDynMapping
= (unsigned long*)xmalloc(sizeof(long)*cs
->size
);
416 while( c_max
- c_min
> 0 )
418 c_val
= (c_max
+ c_min
)/2 + (c_max
+ c_min
)%2;
420 if( !XAllocColorCells(display
, cs
->colorMap
, False
,
421 plane_masks
, 0, pixDynMapping
, c_val
) )
425 XFreeColors(display
, cs
->colorMap
, pixDynMapping
, c_val
, 0);
430 if( c_min
> COLOR_max
- NB_RESERVED_COLORS
)
431 c_min
= COLOR_max
- NB_RESERVED_COLORS
;
433 c_min
= (c_min
/2) + (c_min
/2); /* need even set for split palette */
436 if( !XAllocColorCells(display
, cs
->colorMap
, False
,
437 plane_masks
, 0, pixDynMapping
, c_min
) )
439 fprintf(stderr
,"Inexplicable failure during colorcell allocation.\n");
443 cs
->size
= c_min
+ NB_RESERVED_COLORS
;
445 XUngrabServer(display
);
447 dprintf_palette(stddeb
,"adjusted size %i colorcells\n", cs
->size
);
449 else if( cSpace
.flags
& COLOR_VIRTUAL
)
451 /* virtual colorspace - ToPhysical takes care of
452 * color translations but we have to allocate full palette
453 * to maintain compatibility
456 dprintf_palette(stddeb
,"\tvirtual colorspace - screendepth %i\n", screenDepth
);
458 else cs
->size
= NB_RESERVED_COLORS
; /* system palette only - however we can alloc a bunch
459 * of colors and map to them */
461 dprintf_palette(stddeb
,"Shared system palette uses %i colors.\n", cs
->size
);
463 /* set gap to account for pixel shortage. It has to be right in the center
464 * of the system palette because otherwise raster ops get screwed. */
466 if( cs
->size
>= 256 )
467 { COLOR_gapStart
= 256; COLOR_gapEnd
= -1; }
469 { COLOR_gapStart
= cs
->size
/2; COLOR_gapEnd
= 255 - cs
->size
/2; }
471 COLOR_firstFree
= ( cs
->size
> NB_RESERVED_COLORS
&&
472 (cs
->flags
& COLOR_VIRTUAL
|| !(cs
->flags
& COLOR_FIXED
)) )
473 ? NB_RESERVED_COLORS
/2 : -1;
475 COLOR_sysPal
= (PALETTEENTRY
*)xmalloc(sizeof(PALETTEENTRY
)*256);
477 /* setup system palette entry <-> pixel mappings and fill in 20 fixed entries */
479 if( screenDepth
<= 8 )
481 COLOR_PixelToPalette
= (int*)xmalloc(sizeof(int)*256);
482 memset( COLOR_PixelToPalette
, 0, 256*sizeof(int) );
485 /* for hicolor visuals PaletteToPixel mapping is used to skip
486 * RGB->pixel calculation in COLOR_ToPhysical().
489 COLOR_PaletteToPixel
= (int*)xmalloc(sizeof(int)*256);
491 for( i
= j
= 0; i
< 256; i
++ )
493 if( i
>= COLOR_gapStart
&& i
<= COLOR_gapEnd
)
495 COLOR_PaletteToPixel
[i
] = 0;
496 COLOR_sysPal
[i
].peFlags
= 0; /* mark as unused */
500 if( i
< NB_RESERVED_COLORS
/2 )
502 COLOR_PaletteToPixel
[i
] = sysPixel
[i
];
503 COLOR_sysPal
[i
] = __sysPalTemplate
[i
];
505 else if( i
>= 256 - NB_RESERVED_COLORS
/2 )
507 COLOR_PaletteToPixel
[i
] = sysPixel
[(i
+ NB_RESERVED_COLORS
) - 256];
508 COLOR_sysPal
[i
] = __sysPalTemplate
[(i
+ NB_RESERVED_COLORS
) - 256];
510 else if( pixDynMapping
)
511 COLOR_PaletteToPixel
[i
] = pixDynMapping
[j
++];
513 COLOR_PaletteToPixel
[i
] = i
;
515 dprintf_palette(stddeb
,"\tindex %i -> pixel %i\n", i
, COLOR_PaletteToPixel
[i
]);
517 if( COLOR_PixelToPalette
)
518 COLOR_PixelToPalette
[COLOR_PaletteToPixel
[i
]] = i
;
521 if( pixDynMapping
) free(pixDynMapping
);
526 /***********************************************************************
529 * Create the system palette.
531 static HPALETTE16
COLOR_InitPalette(void)
538 memset(COLOR_freeList
, 0, 256*sizeof(unsigned char));
540 /* calculate max palette size */
542 cSpace
.size
= visual
->map_entries
;
544 if (cSpace
.flags
& COLOR_PRIVATE
)
545 COLOR_BuildPrivateMap( &cSpace
);
547 COLOR_BuildSharedMap( &cSpace
);
549 /* Build free list */
551 if( COLOR_firstFree
!= -1 )
552 COLOR_FormatSystemPalette();
554 COLOR_FillDefaultColors();
556 /* create default palette (20 system colors) */
558 palPtr
= xmalloc( sizeof(LOGPALETTE
) + (NB_RESERVED_COLORS
-1)*sizeof(PALETTEENTRY
) );
559 if (!palPtr
) return FALSE
;
561 palPtr
->palVersion
= 0x300;
562 palPtr
->palNumEntries
= NB_RESERVED_COLORS
;
563 for( i
= 0; i
< NB_RESERVED_COLORS
; i
++ )
565 palPtr
->palPalEntry
[i
].peRed
= __sysPalTemplate
[i
].peRed
;
566 palPtr
->palPalEntry
[i
].peGreen
= __sysPalTemplate
[i
].peGreen
;
567 palPtr
->palPalEntry
[i
].peBlue
= __sysPalTemplate
[i
].peBlue
;
568 palPtr
->palPalEntry
[i
].peFlags
= 0;
570 hpalette
= CreatePalette16( palPtr
);
572 palObj
= (PALETTEOBJ
*) GDI_GetObjPtr( hpalette
, PALETTE_MAGIC
);
574 palObj
->mapping
= xmalloc( sizeof(int) * 20 );
581 /***********************************************************************
582 * COLOR_Computeshifts
584 * Calculate conversion parameters for direct mapped visuals
586 static void COLOR_Computeshifts(unsigned long maskbits
, int *shift
, int *max
)
597 for(i
=0;!(maskbits
&1);i
++)
605 /***********************************************************************
608 * Initialize color map and system palette.
611 HPALETTE16
COLOR_Init(void)
613 visual
= DefaultVisual( display
, DefaultScreen(display
) );
615 dprintf_palette(stddeb
,"COLOR_Init: initializing palette manager...");
617 switch(visual
->class)
620 cSpace
.flags
|= COLOR_VIRTUAL
;
623 if (Options
.usePrivateMap
)
625 XSetWindowAttributes win_attr
;
627 cSpace
.flags
|= COLOR_PRIVATE
;
628 cSpace
.colorMap
= XCreateColormap( display
, rootWindow
,
632 if( rootWindow
!= DefaultRootWindow(display
) )
634 win_attr
.colormap
= cSpace
.colorMap
;
635 XChangeWindowAttributes( display
, rootWindow
,
636 CWColormap
, &win_attr
);
641 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
645 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
646 cSpace
.flags
|= COLOR_FIXED
;
647 COLOR_Graymax
= (1<<screenDepth
)-1;
651 cSpace
.flags
|= COLOR_VIRTUAL
;
653 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
654 cSpace
.flags
|= COLOR_FIXED
;
655 COLOR_Computeshifts(visual
->red_mask
, &COLOR_Redshift
, &COLOR_Redmax
);
656 COLOR_Computeshifts(visual
->green_mask
, &COLOR_Greenshift
, &COLOR_Greenmax
);
657 COLOR_Computeshifts(visual
->blue_mask
, &COLOR_Blueshift
, &COLOR_Bluemax
);
661 dprintf_palette(stddeb
," visual class %i\n", visual
->class);
663 return COLOR_InitPalette();
666 /***********************************************************************
669 * Free external colors we grabbed in the FillDefaultPalette()
671 void COLOR_Cleanup(void)
673 if( COLOR_gapFilled
)
674 XFreeColors(display
, cSpace
.colorMap
,
675 (unsigned long*)(COLOR_PaletteToPixel
+ COLOR_gapStart
),
679 /***********************************************************************
682 * Check whether 'color' can be represented with a solid color.
684 BOOL32
COLOR_IsSolid( COLORREF color
)
687 const PALETTEENTRY
*pEntry
= COLOR_sysPal
;
689 if (color
& 0xff000000) return TRUE
; /* indexed color */
691 if (!color
|| (color
== 0xffffff)) return TRUE
; /* black or white */
693 for (i
= 0; i
< 256 ; i
++, pEntry
++)
695 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
696 if ((GetRValue(color
) == pEntry
->peRed
) &&
697 (GetGValue(color
) == pEntry
->peGreen
) &&
698 (GetBValue(color
) == pEntry
->peBlue
)) return TRUE
;
704 /***********************************************************************
705 * COLOR_PaletteLookupPixel
707 int COLOR_PaletteLookupPixel( PALETTEENTRY
* palPalEntry
, int size
,
708 int* mapping
, COLORREF col
, BOOL32 skipReserved
)
710 int i
, best
= 0, diff
= 0x7fffffff;
713 for( i
= 0; i
< size
&& diff
; i
++ )
715 if( !(palPalEntry
[i
].peFlags
& PC_SYS_USED
) ||
716 (skipReserved
&& palPalEntry
[i
].peFlags
& PC_SYS_RESERVED
) )
719 r
= palPalEntry
[i
].peRed
- GetRValue(col
);
720 g
= palPalEntry
[i
].peGreen
- GetGValue(col
);
721 b
= palPalEntry
[i
].peBlue
- GetBValue(col
);
725 if( r
< diff
) { best
= i
; diff
= r
; }
727 return (mapping
) ? mapping
[best
] : best
;
730 /***********************************************************************
731 * COLOR_LookupSystemPixel
733 int COLOR_LookupSystemPixel(COLORREF col
)
735 int i
, best
= 0, diff
= 0x7fffffff;
736 int size
= COLOR_GetSystemPaletteSize();
739 for( i
= 0; i
< size
&& diff
; i
++ )
741 if( i
== NB_RESERVED_COLORS
/2 )
743 int newi
= size
- NB_RESERVED_COLORS
/2;
747 r
= COLOR_sysPal
[i
].peRed
- GetRValue(col
);
748 g
= COLOR_sysPal
[i
].peGreen
- GetGValue(col
);
749 b
= COLOR_sysPal
[i
].peBlue
- GetBValue(col
);
753 if( r
< diff
) { best
= i
; diff
= r
; }
756 return (COLOR_PaletteToPixel
)? COLOR_PaletteToPixel
[best
] : best
;
759 /***********************************************************************
760 * COLOR_PaletteLookupExactIndex
762 int COLOR_PaletteLookupExactIndex( PALETTEENTRY
* palPalEntry
, int size
,
766 BYTE r
= GetRValue(col
), g
= GetGValue(col
), b
= GetBValue(col
);
767 for( i
= 0; i
< size
; i
++ )
769 if( palPalEntry
[i
].peFlags
& PC_SYS_USED
) /* skips gap */
770 if( palPalEntry
[i
].peRed
== r
&&
771 palPalEntry
[i
].peGreen
== g
&&
772 palPalEntry
[i
].peBlue
== b
)
778 /***********************************************************************
779 * COLOR_LookupNearestColor
781 COLORREF
COLOR_LookupNearestColor( PALETTEENTRY
* palPalEntry
, int size
, COLORREF color
)
783 unsigned char spec_type
= color
>> 24;
786 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
788 if( spec_type
== 2 ) /* PALETTERGB */
790 (palPalEntry
+ COLOR_PaletteLookupPixel(palPalEntry
,size
,NULL
,color
,FALSE
));
792 else if( spec_type
== 1 ) /* PALETTEINDEX */
793 if( (i
= color
& 0x0000ffff) >= size
)
795 fprintf(stderr
, "\tRGB(%lx) : idx %d is out of bounds, assuming NULL\n", color
, i
);
796 color
= *(COLORREF
*)palPalEntry
;
798 else color
= *(COLORREF
*)(palPalEntry
+ i
);
801 return (0x00ffffff & *(COLORREF
*)
802 (COLOR_sysPal
+ COLOR_PaletteLookupPixel(COLOR_sysPal
, 256, NULL
, color
, FALSE
)));
805 /***********************************************************************
808 * Return RGB color for given X pixel.
810 COLORREF
COLOR_ToLogical(int pixel
)
814 /* truecolor visual */
816 if (screenDepth
>= 24) return pixel
;
818 /* check for hicolor visuals first */
820 if ( cSpace
.flags
& COLOR_FIXED
&& !COLOR_Graymax
)
822 color
.red
= (pixel
>> COLOR_Redshift
) & COLOR_Redmax
;
823 color
.green
= (pixel
>> COLOR_Greenshift
) & COLOR_Greenmax
;
824 color
.blue
= (pixel
>> COLOR_Blueshift
) & COLOR_Bluemax
;
825 return RGB((color
.red
* 255)/COLOR_Redmax
,
826 (color
.green
* 255)/COLOR_Greenmax
,
827 (color
.blue
* 255)/COLOR_Bluemax
);
830 /* check if we can bypass X */
832 if ((screenDepth
<= 8) && (pixel
< 256) &&
833 !(cSpace
.flags
& (COLOR_VIRTUAL
| COLOR_FIXED
)) )
834 return ( *(COLORREF
*)(COLOR_sysPal
+
835 ((COLOR_PixelToPalette
)?COLOR_PixelToPalette
[pixel
]:pixel
)) ) & 0x00ffffff;
838 XQueryColor(display
, cSpace
.colorMap
, &color
);
839 return RGB(color
.red
>> 8, color
.green
>> 8, color
.blue
>> 8);
843 /***********************************************************************
846 * Return the physical color closest to 'color'.
848 int COLOR_ToPhysical( DC
*dc
, COLORREF color
)
851 unsigned char spec_type
;
852 HPALETTE16 hPal
= (dc
)? dc
->w
.hPalette
: STOCK_DEFAULT_PALETTE
;
854 spec_type
= color
>> 24;
856 if( spec_type
== 0xff )
858 spec_type
= 0; /* 'write' seems to need that for 'Page 1' text */
864 fprintf(stderr
, "COLOR_ToPhysical : invalid RGB specifier for: %08lx\n", color
);
868 if (dc
&& (dc
->w
.bitsPerPixel
== 1) && (spec_type
== 0))
871 if (((color
>> 16) & 0xff) +
872 ((color
>> 8) & 0xff) + (color
& 0xff) > 255*3/2)
873 return 1; /* white */
874 else return 0; /* black */
877 if ( cSpace
.flags
& COLOR_FIXED
)
879 /* there is no colormap limitation; we are going to have to compute
880 * the pixel value from the visual information stored earlier
883 unsigned long red
, green
, blue
;
885 PALETTEOBJ
* palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hPal
, PALETTE_MAGIC
);
889 case 2: /* PALETTERGB - not sure if we really need to search palette */
891 idx
= COLOR_PaletteLookupPixel( palPtr
->logpalette
.palPalEntry
,
892 palPtr
->logpalette
.palNumEntries
,
895 if( palPtr
->mapping
) return palPtr
->mapping
[idx
];
897 color
= *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ idx
);
900 case 1: /* PALETTEINDEX */
902 if ( (idx
= color
& 0xffff) >= palPtr
->logpalette
.palNumEntries
)
904 fprintf(stderr
, "\tRGB(%lx) : idx %d is out of bounds, assuming black\n", color
, idx
);
908 if( palPtr
->mapping
) return palPtr
->mapping
[idx
];
910 color
= *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ idx
);
912 /* fall through and out */
918 red
= GetRValue(color
); green
= GetGValue(color
); blue
= GetBValue(color
);
922 /* grayscale only; return scaled value */
923 return ( (red
* 30 + green
* 69 + blue
* 11) * COLOR_Graymax
) / 25500;
927 /* scale each individually and construct the TrueColor pixel value */
928 if (COLOR_Redmax
!= 255) red
= (red
* COLOR_Redmax
) / 255;
929 if (COLOR_Greenmax
!= 255) green
= (green
* COLOR_Greenmax
) / 255;
930 if (COLOR_Bluemax
!= 255) blue
= (blue
* COLOR_Bluemax
) / 255;
932 return (red
<< COLOR_Redshift
) | (green
<< COLOR_Greenshift
) | (blue
<< COLOR_Blueshift
);
937 PALETTEOBJ
* palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hPal
, PALETTE_MAGIC
);
939 /* palPtr can be NULL when DC is being destroyed */
941 if( !palPtr
) return 0;
942 else if( !palPtr
->mapping
)
943 dprintf_palette(stddeb
,"\tpalette %04x is not realized\n", dc
->w
.hPalette
);
945 switch(spec_type
) /* we have to peruse DC and system palette */
949 index
= COLOR_PaletteLookupPixel( COLOR_sysPal
, 256,
950 COLOR_PaletteToPixel
, color
, FALSE
);
952 /* dprintf_palette(stddeb,"\tRGB(%lx) -> pixel %i\n", color, index);
955 case 1: /* PALETTEINDEX */
956 index
= color
& 0xffff;
958 if( index
>= palPtr
->logpalette
.palNumEntries
)
959 fprintf(stderr
, "\tRGB(%lx) : index %i is out of bounds\n", color
, index
);
960 else if( palPtr
->mapping
) index
= palPtr
->mapping
[index
];
962 /* dprintf_palette(stddeb,"\tPALETTEINDEX(%04x) -> pixel %i\n", (WORD)color, index);
965 case 2: /* PALETTERGB */
966 index
= COLOR_PaletteLookupPixel( palPtr
->logpalette
.palPalEntry
,
967 palPtr
->logpalette
.palNumEntries
,
968 palPtr
->mapping
, color
, FALSE
);
969 /* dprintf_palette(stddeb,"\tPALETTERGB(%lx) -> pixel %i\n", color, index);
978 /***********************************************************************
981 * Set the color-mapping table for selected palette.
982 * Return number of entries which mapping has changed.
984 int COLOR_SetMapping( PALETTEOBJ
* palPtr
, BOOL32 mapOnly
)
988 int prevMapping
= (palPtr
->mapping
) ? 1 : 0;
991 /* reset dynamic system palette entries */
993 if( !mapOnly
&& COLOR_firstFree
!= -1)
994 COLOR_FormatSystemPalette();
996 /* initialize palette mapping table */
998 palPtr
->mapping
= (int*)xrealloc(palPtr
->mapping
, sizeof(int)*
999 palPtr
->logpalette
.palNumEntries
);
1001 for( i
= 0; i
< palPtr
->logpalette
.palNumEntries
; i
++ )
1006 switch( palPtr
->logpalette
.palPalEntry
[i
].peFlags
& 0x07 )
1008 case PC_EXPLICIT
: /* palette entries are indices into system palette */
1009 index
= *(WORD
*)(palPtr
->logpalette
.palPalEntry
+ i
);
1010 if( index
> 255 || (index
>= COLOR_gapStart
&& index
<= COLOR_gapEnd
) )
1012 fprintf(stderr
,"PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index
);
1017 case PC_RESERVED
: /* forbid future mappings to this entry */
1018 flag
|= PC_SYS_RESERVED
;
1021 default: /* try to collapse identical colors */
1022 index
= COLOR_PaletteLookupExactIndex(COLOR_sysPal
, 256,
1023 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
));
1028 if( COLOR_firstFree
> 0 && !(cSpace
.flags
& COLOR_FIXED
) )
1031 index
= COLOR_firstFree
; /* ought to be available */
1032 COLOR_firstFree
= COLOR_freeList
[index
];
1034 color
.pixel
= (COLOR_PaletteToPixel
) ? COLOR_PaletteToPixel
[index
] : index
;
1035 color
.red
= palPtr
->logpalette
.palPalEntry
[i
].peRed
<< 8;
1036 color
.green
= palPtr
->logpalette
.palPalEntry
[i
].peGreen
<< 8;
1037 color
.blue
= palPtr
->logpalette
.palPalEntry
[i
].peBlue
<< 8;
1038 color
.flags
= DoRed
| DoGreen
| DoBlue
;
1039 XStoreColor(display
, cSpace
.colorMap
, &color
);
1041 COLOR_sysPal
[index
] = palPtr
->logpalette
.palPalEntry
[i
];
1042 COLOR_sysPal
[index
].peFlags
= flag
;
1043 COLOR_freeList
[index
] = 0;
1045 if( COLOR_PaletteToPixel
) index
= COLOR_PaletteToPixel
[index
];
1048 else if ( cSpace
.flags
& COLOR_VIRTUAL
)
1050 index
= COLOR_ToPhysical( NULL
, 0x00ffffff &
1051 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
));
1055 /* we have to map to existing entry in the system palette */
1057 index
= COLOR_PaletteLookupPixel(COLOR_sysPal
, 256, NULL
,
1058 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
), TRUE
);
1060 palPtr
->logpalette
.palPalEntry
[i
].peFlags
|= PC_SYS_USED
;
1062 if( COLOR_PaletteToPixel
) index
= COLOR_PaletteToPixel
[index
];
1066 if( !prevMapping
|| palPtr
->mapping
[i
] != index
) iRemapped
++;
1067 palPtr
->mapping
[i
] = index
;
1069 dprintf_palette(stddeb
,"\tentry %i (%lx) -> pixel %i\n", i
,
1070 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
), index
);