4 * Copyright 1993 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(palette
);
30 /***********************************************************************
33 * First 10 and last 10 colors in COLOR_sysPalette are
34 * "guarded". RealizePalette changes only the rest of colorcells. For
35 * currently inactive window it changes only DC palette mappings.
38 PALETTEENTRY
*COLOR_sysPal
= NULL
; /* current system palette */
40 int COLOR_gapStart
= 256;
41 int COLOR_gapEnd
= -1;
42 int COLOR_gapFilled
= 0;
45 const PALETTEENTRY COLOR_sysPalTemplate
[NB_RESERVED_COLORS
] =
47 /* first 10 entries in the system palette */
48 /* red green blue flags */
49 { 0x00, 0x00, 0x00, PC_SYS_USED
},
50 { 0x80, 0x00, 0x00, PC_SYS_USED
},
51 { 0x00, 0x80, 0x00, PC_SYS_USED
},
52 { 0x80, 0x80, 0x00, PC_SYS_USED
},
53 { 0x00, 0x00, 0x80, PC_SYS_USED
},
54 { 0x80, 0x00, 0x80, PC_SYS_USED
},
55 { 0x00, 0x80, 0x80, PC_SYS_USED
},
56 { 0xc0, 0xc0, 0xc0, PC_SYS_USED
},
57 { 0xc0, 0xdc, 0xc0, PC_SYS_USED
},
58 { 0xa6, 0xca, 0xf0, PC_SYS_USED
},
60 /* ... c_min/2 dynamic colorcells */
62 /* ... gap (for sparse palettes) */
64 /* ... c_min/2 dynamic colorcells */
66 { 0xff, 0xfb, 0xf0, PC_SYS_USED
},
67 { 0xa0, 0xa0, 0xa4, PC_SYS_USED
},
68 { 0x80, 0x80, 0x80, PC_SYS_USED
},
69 { 0xff, 0x00, 0x00, PC_SYS_USED
},
70 { 0x00, 0xff, 0x00, PC_SYS_USED
},
71 { 0xff, 0xff, 0x00, PC_SYS_USED
},
72 { 0x00, 0x00, 0xff, PC_SYS_USED
},
73 { 0xff, 0x00, 0xff, PC_SYS_USED
},
74 { 0x00, 0xff, 0xff, PC_SYS_USED
},
75 { 0xff, 0xff, 0xff, PC_SYS_USED
} /* last 10 */
78 /***********************************************************************
79 * COLOR_GetSystemPaletteTemplate
81 const PALETTEENTRY
* COLOR_GetSystemPaletteTemplate(void)
83 return COLOR_sysPalTemplate
;
86 /***********************************************************************
87 * COLOR_GetSystemPaletteEntry
90 COLORREF
COLOR_GetSystemPaletteEntry(UINT i
)
92 return *(COLORREF
*)(COLOR_sysPal
+ i
) & 0x00ffffff;
95 /***********************************************************************
98 * Check whether 'color' can be represented with a solid color.
100 BOOL
COLOR_IsSolid( COLORREF color
)
103 const PALETTEENTRY
*pEntry
= COLOR_sysPal
;
105 if (color
& 0xff000000) return TRUE
; /* indexed color */
107 if (!color
|| (color
== 0xffffff)) return TRUE
; /* black or white */
109 for (i
= 0; i
< 256 ; i
++, pEntry
++)
111 if( i
< COLOR_gapStart
|| i
> COLOR_gapEnd
)
112 if ((GetRValue(color
) == pEntry
->peRed
) &&
113 (GetGValue(color
) == pEntry
->peGreen
) &&
114 (GetBValue(color
) == pEntry
->peBlue
)) return TRUE
;
119 /***********************************************************************
120 * COLOR_PaletteLookupPixel
122 int COLOR_PaletteLookupPixel( PALETTEENTRY
* palPalEntry
, int size
,
123 int* mapping
, COLORREF col
, BOOL skipReserved
)
125 int i
, best
= 0, diff
= 0x7fffffff;
128 for( i
= 0; i
< size
&& diff
; i
++ )
130 if( !(palPalEntry
[i
].peFlags
& PC_SYS_USED
) ||
131 (skipReserved
&& palPalEntry
[i
].peFlags
& PC_SYS_RESERVED
) )
134 r
= palPalEntry
[i
].peRed
- GetRValue(col
);
135 g
= palPalEntry
[i
].peGreen
- GetGValue(col
);
136 b
= palPalEntry
[i
].peBlue
- GetBValue(col
);
140 if( r
< diff
) { best
= i
; diff
= r
; }
142 return (mapping
) ? mapping
[best
] : best
;
145 /***********************************************************************
146 * COLOR_PaletteLookupExactIndex
148 int COLOR_PaletteLookupExactIndex( PALETTEENTRY
* palPalEntry
, int size
,
152 BYTE r
= GetRValue(col
), g
= GetGValue(col
), b
= GetBValue(col
);
153 for( i
= 0; i
< size
; i
++ )
155 if( palPalEntry
[i
].peFlags
& PC_SYS_USED
) /* skips gap */
156 if( palPalEntry
[i
].peRed
== r
&&
157 palPalEntry
[i
].peGreen
== g
&&
158 palPalEntry
[i
].peBlue
== b
)
164 /***********************************************************************
165 * COLOR_LookupNearestColor
167 COLORREF
COLOR_LookupNearestColor( PALETTEENTRY
* palPalEntry
, int size
, COLORREF color
)
169 unsigned char spec_type
= color
>> 24;
172 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
174 if( spec_type
== 2 ) /* PALETTERGB */
176 (palPalEntry
+ COLOR_PaletteLookupPixel(palPalEntry
,size
,NULL
,color
,FALSE
));
178 else if( spec_type
== 1 ) /* PALETTEINDEX */
180 if( (i
= color
& 0x0000ffff) >= size
)
182 WARN("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color
, i
);
183 color
= *(COLORREF
*)palPalEntry
;
185 else color
= *(COLORREF
*)(palPalEntry
+ i
);
189 return (0x00ffffff & *(COLORREF
*)
190 (COLOR_sysPal
+ COLOR_PaletteLookupPixel(COLOR_sysPal
, 256, NULL
, color
, FALSE
)));