Added further implementation of WINAPI EnumDateFormats() to support
[wine/multimedia.git] / objects / color.c
blobd0663dba2831f6aa267253981514534821f5b38e
1 /*
2 * Color functions
4 * Copyright 1993 Alexandre Julliard
5 * Copyright 1996 Alex Korobka
6 */
8 #include "color.h"
9 #include "debugtools.h"
10 #include "palette.h"
11 #include "windef.h"
13 DEFAULT_DEBUG_CHANNEL(palette)
16 /***********************************************************************
17 * System color space.
19 * First 10 and last 10 colors in COLOR_sysPalette are
20 * "guarded". RealizePalette changes only the rest of colorcells. For
21 * currently inactive window it changes only DC palette mappings.
24 PALETTEENTRY *COLOR_sysPal = NULL; /* current system palette */
26 int COLOR_gapStart = 256;
27 int COLOR_gapEnd = -1;
28 int COLOR_gapFilled = 0;
29 int COLOR_max = 256;
31 const PALETTEENTRY COLOR_sysPalTemplate[NB_RESERVED_COLORS] =
33 /* first 10 entries in the system palette */
34 /* red green blue flags */
35 { 0x00, 0x00, 0x00, PC_SYS_USED },
36 { 0x80, 0x00, 0x00, PC_SYS_USED },
37 { 0x00, 0x80, 0x00, PC_SYS_USED },
38 { 0x80, 0x80, 0x00, PC_SYS_USED },
39 { 0x00, 0x00, 0x80, PC_SYS_USED },
40 { 0x80, 0x00, 0x80, PC_SYS_USED },
41 { 0x00, 0x80, 0x80, PC_SYS_USED },
42 { 0xc0, 0xc0, 0xc0, PC_SYS_USED },
43 { 0xc0, 0xdc, 0xc0, PC_SYS_USED },
44 { 0xa6, 0xca, 0xf0, PC_SYS_USED },
46 /* ... c_min/2 dynamic colorcells */
48 /* ... gap (for sparse palettes) */
50 /* ... c_min/2 dynamic colorcells */
52 { 0xff, 0xfb, 0xf0, PC_SYS_USED },
53 { 0xa0, 0xa0, 0xa4, PC_SYS_USED },
54 { 0x80, 0x80, 0x80, PC_SYS_USED },
55 { 0xff, 0x00, 0x00, PC_SYS_USED },
56 { 0x00, 0xff, 0x00, PC_SYS_USED },
57 { 0xff, 0xff, 0x00, PC_SYS_USED },
58 { 0x00, 0x00, 0xff, PC_SYS_USED },
59 { 0xff, 0x00, 0xff, PC_SYS_USED },
60 { 0x00, 0xff, 0xff, PC_SYS_USED },
61 { 0xff, 0xff, 0xff, PC_SYS_USED } /* last 10 */
64 /***********************************************************************
65 * COLOR_GetSystemPaletteTemplate
67 const PALETTEENTRY* COLOR_GetSystemPaletteTemplate(void)
69 return COLOR_sysPalTemplate;
72 /***********************************************************************
73 * COLOR_GetSystemPaletteEntry
76 COLORREF COLOR_GetSystemPaletteEntry(UINT i)
78 return *(COLORREF*)(COLOR_sysPal + i) & 0x00ffffff;
81 /***********************************************************************
82 * COLOR_IsSolid
84 * Check whether 'color' can be represented with a solid color.
86 BOOL COLOR_IsSolid( COLORREF color )
88 int i;
89 const PALETTEENTRY *pEntry = COLOR_sysPal;
91 if (color & 0xff000000) return TRUE; /* indexed color */
93 if (!color || (color == 0xffffff)) return TRUE; /* black or white */
95 for (i = 0; i < 256 ; i++, pEntry++)
97 if( i < COLOR_gapStart || i > COLOR_gapEnd )
98 if ((GetRValue(color) == pEntry->peRed) &&
99 (GetGValue(color) == pEntry->peGreen) &&
100 (GetBValue(color) == pEntry->peBlue)) return TRUE;
102 return FALSE;
105 /***********************************************************************
106 * COLOR_PaletteLookupPixel
108 int COLOR_PaletteLookupPixel( PALETTEENTRY* palPalEntry, int size,
109 int* mapping, COLORREF col, BOOL skipReserved )
111 int i, best = 0, diff = 0x7fffffff;
112 int r,g,b;
114 for( i = 0; i < size && diff ; i++ )
116 if( !(palPalEntry[i].peFlags & PC_SYS_USED) ||
117 (skipReserved && palPalEntry[i].peFlags & PC_SYS_RESERVED) )
118 continue;
120 r = palPalEntry[i].peRed - GetRValue(col);
121 g = palPalEntry[i].peGreen - GetGValue(col);
122 b = palPalEntry[i].peBlue - GetBValue(col);
124 r = r*r + g*g + b*b;
126 if( r < diff ) { best = i; diff = r; }
128 return (mapping) ? mapping[best] : best;
131 /***********************************************************************
132 * COLOR_PaletteLookupExactIndex
134 int COLOR_PaletteLookupExactIndex( PALETTEENTRY* palPalEntry, int size,
135 COLORREF col )
137 int i;
138 BYTE r = GetRValue(col), g = GetGValue(col), b = GetBValue(col);
139 for( i = 0; i < size; i++ )
141 if( palPalEntry[i].peFlags & PC_SYS_USED ) /* skips gap */
142 if( palPalEntry[i].peRed == r &&
143 palPalEntry[i].peGreen == g &&
144 palPalEntry[i].peBlue == b )
145 return i;
147 return -1;
150 /***********************************************************************
151 * COLOR_LookupNearestColor
153 COLORREF COLOR_LookupNearestColor( PALETTEENTRY* palPalEntry, int size, COLORREF color )
155 unsigned char spec_type = color >> 24;
156 int i;
158 /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */
160 if( spec_type == 2 ) /* PALETTERGB */
161 color = *(COLORREF*)
162 (palPalEntry + COLOR_PaletteLookupPixel(palPalEntry,size,NULL,color,FALSE));
164 else if( spec_type == 1 ) /* PALETTEINDEX */
166 if( (i = color & 0x0000ffff) >= size )
168 WARN("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, i);
169 color = *(COLORREF*)palPalEntry;
171 else color = *(COLORREF*)(palPalEntry + i);
174 color &= 0x00ffffff;
175 return (0x00ffffff & *(COLORREF*)
176 (COLOR_sysPal + COLOR_PaletteLookupPixel(COLOR_sysPal, 256, NULL, color, FALSE)));