Alexandre Julliard | f0b2354 | 1993-09-29 12:21:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Color functions |
| 3 | * |
| 4 | * Copyright 1993 Alexandre Julliard |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 5 | * Copyright 1996 Alex Korobka |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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 |
Alexandre Julliard | af0bae5 | 1995-10-03 17:06:08 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Alexandre Julliard | 234bc24 | 1994-12-10 13:02:28 +0000 | [diff] [blame] | 22 | #include "color.h" |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 23 | #include "wine/debug.h" |
Patrik Stridvall | b87fe2e | 1999-04-01 08:16:08 +0000 | [diff] [blame] | 24 | #include "palette.h" |
| 25 | #include "windef.h" |
Alexandre Julliard | f0b2354 | 1993-09-29 12:21:49 +0000 | [diff] [blame] | 26 | |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 27 | WINE_DEFAULT_DEBUG_CHANNEL(palette); |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 28 | |
Alexandre Julliard | ff8331e | 1995-09-18 11:19:54 +0000 | [diff] [blame] | 29 | |
Patrik Stridvall | b87fe2e | 1999-04-01 08:16:08 +0000 | [diff] [blame] | 30 | /*********************************************************************** |
| 31 | * System color space. |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 32 | * |
| 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. |
| 36 | */ |
Alexandre Julliard | f0b2354 | 1993-09-29 12:21:49 +0000 | [diff] [blame] | 37 | |
Patrik Stridvall | b87fe2e | 1999-04-01 08:16:08 +0000 | [diff] [blame] | 38 | PALETTEENTRY *COLOR_sysPal = NULL; /* current system palette */ |
Alexandre Julliard | f0b2354 | 1993-09-29 12:21:49 +0000 | [diff] [blame] | 39 | |
Patrik Stridvall | b87fe2e | 1999-04-01 08:16:08 +0000 | [diff] [blame] | 40 | int COLOR_gapStart = 256; |
| 41 | int COLOR_gapEnd = -1; |
| 42 | int COLOR_gapFilled = 0; |
| 43 | int COLOR_max = 256; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 44 | |
Patrik Stridvall | b87fe2e | 1999-04-01 08:16:08 +0000 | [diff] [blame] | 45 | const PALETTEENTRY COLOR_sysPalTemplate[NB_RESERVED_COLORS] = |
Alexandre Julliard | f0b2354 | 1993-09-29 12:21:49 +0000 | [diff] [blame] | 46 | { |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 47 | /* first 10 entries in the system palette */ |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 48 | /* red green blue flags */ |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 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 }, |
Alexandre Julliard | f0b2354 | 1993-09-29 12:21:49 +0000 | [diff] [blame] | 59 | |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 60 | /* ... c_min/2 dynamic colorcells */ |
| 61 | |
| 62 | /* ... gap (for sparse palettes) */ |
| 63 | |
| 64 | /* ... c_min/2 dynamic colorcells */ |
| 65 | |
| 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 */ |
Alexandre Julliard | f0b2354 | 1993-09-29 12:21:49 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 78 | /*********************************************************************** |
Patrik Stridvall | b87fe2e | 1999-04-01 08:16:08 +0000 | [diff] [blame] | 79 | * COLOR_GetSystemPaletteTemplate |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 80 | */ |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 81 | const PALETTEENTRY* COLOR_GetSystemPaletteTemplate(void) |
| 82 | { |
Patrik Stridvall | b87fe2e | 1999-04-01 08:16:08 +0000 | [diff] [blame] | 83 | return COLOR_sysPalTemplate; |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Patrik Stridvall | b87fe2e | 1999-04-01 08:16:08 +0000 | [diff] [blame] | 86 | /*********************************************************************** |
| 87 | * COLOR_GetSystemPaletteEntry |
| 88 | */ |
| 89 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 90 | COLORREF COLOR_GetSystemPaletteEntry(UINT i) |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 91 | { |
Patrik Stridvall | b87fe2e | 1999-04-01 08:16:08 +0000 | [diff] [blame] | 92 | return *(COLORREF*)(COLOR_sysPal + i) & 0x00ffffff; |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 93 | } |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 94 | |
| 95 | /*********************************************************************** |
| 96 | * COLOR_IsSolid |
| 97 | * |
| 98 | * Check whether 'color' can be represented with a solid color. |
| 99 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 100 | BOOL COLOR_IsSolid( COLORREF color ) |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 101 | { |
| 102 | int i; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 103 | const PALETTEENTRY *pEntry = COLOR_sysPal; |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 104 | |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 105 | if (color & 0xff000000) return TRUE; /* indexed color */ |
| 106 | |
| 107 | if (!color || (color == 0xffffff)) return TRUE; /* black or white */ |
| 108 | |
| 109 | for (i = 0; i < 256 ; i++, pEntry++) |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 110 | { |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 111 | if( i < COLOR_gapStart || i > COLOR_gapEnd ) |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 112 | if ((GetRValue(color) == pEntry->peRed) && |
| 113 | (GetGValue(color) == pEntry->peGreen) && |
| 114 | (GetBValue(color) == pEntry->peBlue)) return TRUE; |
| 115 | } |
| 116 | return FALSE; |
| 117 | } |
| 118 | |
Alexandre Julliard | 8d24ae6 | 1994-04-05 21:42:43 +0000 | [diff] [blame] | 119 | /*********************************************************************** |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 120 | * COLOR_PaletteLookupPixel |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 121 | */ |
| 122 | int COLOR_PaletteLookupPixel( PALETTEENTRY* palPalEntry, int size, |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 123 | int* mapping, COLORREF col, BOOL skipReserved ) |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 124 | { |
| 125 | int i, best = 0, diff = 0x7fffffff; |
| 126 | int r,g,b; |
| 127 | |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 128 | for( i = 0; i < size && diff ; i++ ) |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 129 | { |
| 130 | if( !(palPalEntry[i].peFlags & PC_SYS_USED) || |
| 131 | (skipReserved && palPalEntry[i].peFlags & PC_SYS_RESERVED) ) |
| 132 | continue; |
| 133 | |
| 134 | r = palPalEntry[i].peRed - GetRValue(col); |
| 135 | g = palPalEntry[i].peGreen - GetGValue(col); |
| 136 | b = palPalEntry[i].peBlue - GetBValue(col); |
| 137 | |
| 138 | r = r*r + g*g + b*b; |
| 139 | |
| 140 | if( r < diff ) { best = i; diff = r; } |
| 141 | } |
| 142 | return (mapping) ? mapping[best] : best; |
| 143 | } |
| 144 | |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 145 | /*********************************************************************** |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 146 | * COLOR_PaletteLookupExactIndex |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 147 | */ |
| 148 | int COLOR_PaletteLookupExactIndex( PALETTEENTRY* palPalEntry, int size, |
| 149 | COLORREF col ) |
| 150 | { |
| 151 | int i; |
| 152 | BYTE r = GetRValue(col), g = GetGValue(col), b = GetBValue(col); |
| 153 | for( i = 0; i < size; i++ ) |
| 154 | { |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 155 | if( palPalEntry[i].peFlags & PC_SYS_USED ) /* skips gap */ |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 156 | if( palPalEntry[i].peRed == r && |
| 157 | palPalEntry[i].peGreen == g && |
| 158 | palPalEntry[i].peBlue == b ) |
| 159 | return i; |
| 160 | } |
| 161 | return -1; |
| 162 | } |
| 163 | |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 164 | /*********************************************************************** |
| 165 | * COLOR_LookupNearestColor |
| 166 | */ |
| 167 | COLORREF COLOR_LookupNearestColor( PALETTEENTRY* palPalEntry, int size, COLORREF color ) |
| 168 | { |
| 169 | unsigned char spec_type = color >> 24; |
| 170 | int i; |
| 171 | |
| 172 | /* we need logical palette for PALETTERGB and PALETTEINDEX colorrefs */ |
| 173 | |
| 174 | if( spec_type == 2 ) /* PALETTERGB */ |
| 175 | color = *(COLORREF*) |
| 176 | (palPalEntry + COLOR_PaletteLookupPixel(palPalEntry,size,NULL,color,FALSE)); |
| 177 | |
| 178 | else if( spec_type == 1 ) /* PALETTEINDEX */ |
Jesper Skov | 5c3e457 | 1998-11-01 19:27:22 +0000 | [diff] [blame] | 179 | { |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 180 | if( (i = color & 0x0000ffff) >= size ) |
| 181 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 182 | WARN("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, i); |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 183 | color = *(COLORREF*)palPalEntry; |
| 184 | } |
| 185 | else color = *(COLORREF*)(palPalEntry + i); |
Jesper Skov | 5c3e457 | 1998-11-01 19:27:22 +0000 | [diff] [blame] | 186 | } |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 187 | |
| 188 | color &= 0x00ffffff; |
| 189 | return (0x00ffffff & *(COLORREF*) |
| 190 | (COLOR_sysPal + COLOR_PaletteLookupPixel(COLOR_sysPal, 256, NULL, color, FALSE))); |
| 191 | } |