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 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
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)
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 */
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.
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
)
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
;
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 */
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
;
266 XStoreColor(display
, cs
->colorMap
, &color
);
268 /* Set EGA mapping if color in the first or last eight */
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;
284 static BOOL
COLOR_BuildSharedMap(CSPACE
* cs
)
287 unsigned long sysPixel
[NB_RESERVED_COLORS
];
288 unsigned long* pixDynMapping
= NULL
;
289 unsigned long plane_masks
[1];
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 */
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
) )
354 XFreeColors(display
, cs
->colorMap
, pixDynMapping
, c_val
, 0);
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 */
365 if( !XAllocColorCells(display
, cs
->colorMap
, False
,
366 plane_masks
, 0, pixDynMapping
, c_min
) )
368 fprintf(stderr
,"Inexplicable failure during colorcell allocation.\n");
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
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; }
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 */
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
++];
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
);
459 /***********************************************************************
462 * Create the system palette and initialize mapping tables.
464 static HPALETTE16
COLOR_InitPalette(void)
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
);
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 );
514 /***********************************************************************
515 * COLOR_Computeshifts
517 * Calculate conversion parameters for direct mapped visuals
519 void COLOR_Computeshifts(unsigned long maskbits
, int *shift
, int *max
)
530 for(i
=0;!(maskbits
&1);i
++)
538 /***********************************************************************
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)
553 cSpace
.flags
|= COLOR_VIRTUAL
;
556 if (Options
.usePrivateMap
)
558 XSetWindowAttributes win_attr
;
560 cSpace
.flags
|= COLOR_PRIVATE
;
561 cSpace
.colorMap
= XCreateColormap( display
, rootWindow
,
565 if( rootWindow
!= DefaultRootWindow(display
) )
567 win_attr
.colormap
= cSpace
.colorMap
;
568 XChangeWindowAttributes( display
, rootWindow
,
569 CWColormap
, &win_attr
);
574 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
578 cSpace
.colorMap
= DefaultColormapOfScreen( screen
);
579 cSpace
.flags
|= COLOR_FIXED
;
580 COLOR_Graymax
= (1<<screenDepth
)-1;
584 cSpace
.flags
|= COLOR_VIRTUAL
;
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
);
594 dprintf_palette(stddeb
," visual class %i\n", visual
->class);
596 return COLOR_InitPalette();
600 /***********************************************************************
603 * Check whether 'color' can be represented with a solid color.
605 BOOL
COLOR_IsSolid( COLORREF color
)
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
;
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;
634 for( i
= 0; i
< size
&& diff
; i
++ )
636 if( !(palPalEntry
[i
].peFlags
& PC_SYS_USED
) ||
637 (skipReserved
&& palPalEntry
[i
].peFlags
& PC_SYS_RESERVED
) )
640 r
= palPalEntry
[i
].peRed
- GetRValue(col
);
641 g
= palPalEntry
[i
].peGreen
- GetGValue(col
);
642 b
= palPalEntry
[i
].peBlue
- GetBValue(col
);
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();
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
);
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
,
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
)
695 /***********************************************************************
696 * COLOR_LookupNearestColor
698 COLORREF
COLOR_LookupNearestColor( PALETTEENTRY
* palPalEntry
, int size
, COLORREF color
)
700 unsigned char spec_type
= color
>> 24;
703 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
705 if( spec_type
== 2 ) /* PALETTERGB */
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
);
718 return (0x00ffffff & *(COLORREF
*)
719 (COLOR_sysPal
+ COLOR_PaletteLookupPixel(COLOR_sysPal
, 256, NULL
, color
, FALSE
)));
722 /***********************************************************************
725 * Return RGB color for given X pixel.
727 COLORREF
COLOR_ToLogical(int pixel
)
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;
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 /***********************************************************************
763 * Return the physical color closest to 'color'.
765 int COLOR_ToPhysical( DC
*dc
, COLORREF color
)
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 */
781 fprintf(stderr
, "COLOR_ToPhysical : invalid RGB specifier for: %08lx\n", color
);
785 if (dc
&& (dc
->w
.bitsPerPixel
== 1) && (spec_type
== 0))
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
;
802 PALETTEOBJ
* palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hPal
, PALETTE_MAGIC
);
806 case 2: /* PALETTERGB - not sure if we really need to search palette */
808 idx
= COLOR_PaletteLookupPixel( palPtr
->logpalette
.palPalEntry
,
809 palPtr
->logpalette
.palNumEntries
,
812 if( palPtr
->mapping
) return palPtr
->mapping
[idx
];
814 color
= *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ idx
);
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
);
825 if( palPtr
->mapping
) return palPtr
->mapping
[idx
];
827 color
= *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ idx
);
829 /* fall through and out */
835 red
= GetRValue(color
); green
= GetGValue(color
); blue
= GetBValue(color
);
839 /* grayscale only; return scaled value */
840 return ( (red
* 30 + green
* 69 + blue
* 11) * COLOR_Graymax
) / 25500;
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
);
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 */
866 index
= COLOR_PaletteLookupPixel( COLOR_sysPal
, 256,
867 COLOR_PaletteToPixel
, color
, FALSE
);
869 /* dprintf_palette(stddeb,"\tRGB(%lx) -> pixel %i\n", color, index);
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);
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);
895 /***********************************************************************
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
)
905 int prevMapping
= (palPtr
->mapping
) ? 1 : 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
++ )
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
);
934 case PC_RESERVED
: /* forbid future mappings to this entry */
935 flag
|= PC_SYS_RESERVED
;
938 default: /* try to collapse identical colors */
939 index
= COLOR_PaletteLookupExactIndex(COLOR_sysPal
, 256,
940 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
));
945 if( COLOR_firstFree
> 0 && !(cSpace
.flags
& COLOR_FIXED
) )
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
];
965 else if ( cSpace
.flags
& COLOR_VIRTUAL
)
967 index
= COLOR_ToPhysical( NULL
, 0x00ffffff &
968 *(COLORREF
*)(palPtr
->logpalette
.palPalEntry
+ i
));
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
];
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
);