1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                       CCCC   OOO   L       OOO   RRRR                       %
6 %                      C      O   O  L      O   O  R   R                      %
7 %                      C      O   O  L      O   O  RRRR                       %
8 %                      C      O   O  L      O   O  R R                        %
9 %                       CCCC   OOO   LLLLL   OOO   R  R                       %
10 %                                                                             %
11 %                                                                             %
12 %                          MagickCore Color Methods                           %
13 %                                                                             %
14 %                              Software Design                                %
15 %                                   Cristy                                    %
16 %                                 July 1992                                   %
17 %                                                                             %
18 %                                                                             %
19 %  Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization      %
20 %  dedicated to making software imaging solutions freely available.           %
21 %                                                                             %
22 %  You may not use this file except in compliance with the License.  You may  %
23 %  obtain a copy of the License at                                            %
24 %                                                                             %
25 %    https://imagemagick.org/script/license.php                               %
26 %                                                                             %
27 %  Unless required by applicable law or agreed to in writing, software        %
28 %  distributed under the License is distributed on an "AS IS" BASIS,          %
29 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
30 %  See the License for the specific language governing permissions and        %
31 %  limitations under the License.                                             %
32 %                                                                             %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 %
35 %  We use linked-lists because splay-trees do not currently support duplicate
36 %  key / value pairs (.e.g X11 green compliance and SVG green compliance).
37 %
38 */
39 
40 /*
41   Include declarations.
42 */
43 #include "MagickCore/studio.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/cache-view.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace-private.h"
50 #include "MagickCore/client.h"
51 #include "MagickCore/configure.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/gem.h"
55 #include "MagickCore/gem-private.h"
56 #include "MagickCore/geometry.h"
57 #include "MagickCore/image-private.h"
58 #include "MagickCore/memory_.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/option.h"
62 #include "MagickCore/pixel-accessor.h"
63 #include "MagickCore/quantize.h"
64 #include "MagickCore/quantum.h"
65 #include "MagickCore/quantum-private.h"
66 #include "MagickCore/semaphore.h"
67 #include "MagickCore/string_.h"
68 #include "MagickCore/string-private.h"
69 #include "MagickCore/token.h"
70 #include "MagickCore/utility.h"
71 #include "MagickCore/utility-private.h"
72 #include "MagickCore/xml-tree.h"
73 #include "MagickCore/xml-tree-private.h"
74 
75 /*
76   Define declarations.
77 */
78 #define ColorFilename  "colors.xml"
79 
80 /*
81   Typedef declarations.
82 */
83 typedef struct _ColormapInfo
84 {
85   const char
86     name[21];
87 
88   const unsigned char
89     red,
90     green,
91     blue;
92 
93   const float
94     alpha;
95 
96   const ssize_t
97     compliance;
98 } ColormapInfo;
99 
100 /*
101   Static declarations.
102 */
103 static const ColormapInfo
104   Colormap[] =
105   {
106     { "none", 0, 0, 0, 0, SVGCompliance | XPMCompliance },
107     { "black", 0, 0, 0, 1, SVGCompliance | X11Compliance | XPMCompliance },
108     { "red", 255, 0, 0, 1, SVGCompliance | X11Compliance | XPMCompliance },
109     { "magenta", 255, 0, 255, 1, SVGCompliance | X11Compliance | XPMCompliance },
110     { "green", 0, 128, 0, 1, SVGCompliance },
111     { "cyan", 0, 255, 255, 1, SVGCompliance | X11Compliance | XPMCompliance },
112     { "blue", 0, 0, 255, 1, SVGCompliance | X11Compliance | XPMCompliance },
113     { "yellow", 255, 255, 0, 1, SVGCompliance | X11Compliance | XPMCompliance },
114     { "white", 255, 255, 255, 1, SVGCompliance | X11Compliance | XPMCompliance },
115     { "AliceBlue", 240, 248, 255, 1, SVGCompliance | X11Compliance | XPMCompliance },
116     { "AntiqueWhite", 250, 235, 215, 1, SVGCompliance | X11Compliance | XPMCompliance },
117     { "AntiqueWhite1", 255, 239, 219, 1, X11Compliance },
118     { "AntiqueWhite2", 238, 223, 204, 1, X11Compliance },
119     { "AntiqueWhite3", 205, 192, 176, 1, X11Compliance },
120     { "AntiqueWhite4", 139, 131, 120, 1, X11Compliance },
121     { "aqua", 0, 255, 255, 1, SVGCompliance },
122     { "aquamarine", 127, 255, 212, 1, SVGCompliance | X11Compliance | XPMCompliance },
123     { "aquamarine1", 127, 255, 212, 1, X11Compliance },
124     { "aquamarine2", 118, 238, 198, 1, X11Compliance },
125     { "aquamarine3", 102, 205, 170, 1, X11Compliance },
126     { "aquamarine4", 69, 139, 116, 1, X11Compliance },
127     { "azure", 240, 255, 255, 1, SVGCompliance | X11Compliance | XPMCompliance },
128     { "azure1", 240, 255, 255, 1, X11Compliance },
129     { "azure2", 224, 238, 238, 1, X11Compliance },
130     { "azure3", 193, 205, 205, 1, X11Compliance },
131     { "azure4", 131, 139, 139, 1, X11Compliance },
132     { "beige", 245, 245, 220, 1, SVGCompliance | X11Compliance | XPMCompliance },
133     { "bisque", 255, 228, 196, 1, SVGCompliance | X11Compliance | XPMCompliance },
134     { "bisque1", 255, 228, 196, 1, X11Compliance },
135     { "bisque2", 238, 213, 183, 1, X11Compliance },
136     { "bisque3", 205, 183, 158, 1, X11Compliance },
137     { "bisque4", 139, 125, 107, 1, X11Compliance },
138     { "BlanchedAlmond", 255, 235, 205, 1, SVGCompliance | X11Compliance | XPMCompliance },
139     { "blue1", 0, 0, 255, 1, X11Compliance },
140     { "blue2", 0, 0, 238, 1, X11Compliance },
141     { "blue3", 0, 0, 205, 1, X11Compliance },
142     { "blue4", 0, 0, 139, 1, X11Compliance },
143     { "BlueViolet", 138, 43, 226, 1, SVGCompliance | X11Compliance | XPMCompliance },
144     { "brown", 165, 42, 42, 1, SVGCompliance | X11Compliance | XPMCompliance },
145     { "brown1", 255, 64, 64, 1, X11Compliance },
146     { "brown2", 238, 59, 59, 1, X11Compliance },
147     { "brown3", 205, 51, 51, 1, X11Compliance },
148     { "brown4", 139, 35, 35, 1, X11Compliance },
149     { "burlywood", 222, 184, 135, 1, SVGCompliance | X11Compliance | XPMCompliance },
150     { "burlywood1", 255, 211, 155, 1, X11Compliance },
151     { "burlywood2", 238, 197, 145, 1, X11Compliance },
152     { "burlywood3", 205, 170, 125, 1, X11Compliance },
153     { "burlywood4", 139, 115, 85, 1, X11Compliance },
154     { "CadetBlue", 95, 158, 160, 1, SVGCompliance | X11Compliance | XPMCompliance },
155     { "CadetBlue1", 152, 245, 255, 1, X11Compliance },
156     { "CadetBlue2", 142, 229, 238, 1, X11Compliance },
157     { "CadetBlue3", 122, 197, 205, 1, X11Compliance },
158     { "CadetBlue4", 83, 134, 139, 1, X11Compliance },
159     { "chartreuse", 127, 255, 0, 1, SVGCompliance | X11Compliance | XPMCompliance },
160     { "chartreuse1", 127, 255, 0, 1, X11Compliance },
161     { "chartreuse2", 118, 238, 0, 1, X11Compliance },
162     { "chartreuse3", 102, 205, 0, 1, X11Compliance },
163     { "chartreuse4", 69, 139, 0, 1, X11Compliance },
164     { "chocolate", 210, 105, 30, 1, SVGCompliance | X11Compliance | XPMCompliance },
165     { "chocolate1", 255, 127, 36, 1, X11Compliance },
166     { "chocolate2", 238, 118, 33, 1, X11Compliance },
167     { "chocolate3", 205, 102, 29, 1, X11Compliance },
168     { "chocolate4", 139, 69, 19, 1, X11Compliance },
169     { "coral", 255, 127, 80, 1, SVGCompliance | X11Compliance | XPMCompliance },
170     { "coral1", 255, 114, 86, 1, X11Compliance },
171     { "coral2", 238, 106, 80, 1, X11Compliance },
172     { "coral3", 205, 91, 69, 1, X11Compliance },
173     { "coral4", 139, 62, 47, 1, X11Compliance },
174     { "CornflowerBlue", 100, 149, 237, 1, SVGCompliance | X11Compliance | XPMCompliance },
175     { "cornsilk", 255, 248, 220, 1, SVGCompliance | X11Compliance | XPMCompliance },
176     { "cornsilk1", 255, 248, 220, 1, X11Compliance },
177     { "cornsilk2", 238, 232, 205, 1, X11Compliance },
178     { "cornsilk3", 205, 200, 177, 1, X11Compliance },
179     { "cornsilk4", 139, 136, 120, 1, X11Compliance },
180     { "crimson", 220, 20, 60, 1, SVGCompliance },
181     { "cyan1", 0, 255, 255, 1, X11Compliance },
182     { "cyan2", 0, 238, 238, 1, X11Compliance },
183     { "cyan3", 0, 205, 205, 1, X11Compliance },
184     { "cyan4", 0, 139, 139, 1, X11Compliance },
185     { "DarkBlue", 0, 0, 139, 1, SVGCompliance | X11Compliance },
186     { "DarkCyan", 0, 139, 139, 1, SVGCompliance | X11Compliance },
187     { "DarkGoldenrod", 184, 134, 11, 1, SVGCompliance | X11Compliance | XPMCompliance },
188     { "DarkGoldenrod1", 255, 185, 15, 1, X11Compliance },
189     { "DarkGoldenrod2", 238, 173, 14, 1, X11Compliance },
190     { "DarkGoldenrod3", 205, 149, 12, 1, X11Compliance },
191     { "DarkGoldenrod4", 139, 101, 8, 1, X11Compliance },
192     { "DarkGray", 169, 169, 169, 1, SVGCompliance | X11Compliance },
193     { "DarkGreen", 0, 100, 0, 1, SVGCompliance | X11Compliance | XPMCompliance },
194     { "DarkGrey", 169, 169, 169, 1, SVGCompliance | X11Compliance },
195     { "DarkKhaki", 189, 183, 107, 1, SVGCompliance | X11Compliance | XPMCompliance },
196     { "DarkMagenta", 139, 0, 139, 1, SVGCompliance | X11Compliance },
197     { "DarkOliveGreen", 85, 107, 47, 1, SVGCompliance | X11Compliance | XPMCompliance },
198     { "DarkOliveGreen1", 202, 255, 112, 1, X11Compliance },
199     { "DarkOliveGreen2", 188, 238, 104, 1, X11Compliance },
200     { "DarkOliveGreen3", 162, 205, 90, 1, X11Compliance },
201     { "DarkOliveGreen4", 110, 139, 61, 1, X11Compliance },
202     { "DarkOrange", 255, 140, 0, 1, SVGCompliance | X11Compliance | XPMCompliance },
203     { "DarkOrange1", 255, 127, 0, 1, X11Compliance },
204     { "DarkOrange2", 238, 118, 0, 1, X11Compliance },
205     { "DarkOrange3", 205, 102, 0, 1, X11Compliance },
206     { "DarkOrange4", 139, 69, 0, 1, X11Compliance },
207     { "DarkOrchid", 153, 50, 204, 1, SVGCompliance | X11Compliance | XPMCompliance },
208     { "DarkOrchid1", 191, 62, 255, 1, X11Compliance },
209     { "DarkOrchid2", 178, 58, 238, 1, X11Compliance },
210     { "DarkOrchid3", 154, 50, 205, 1, X11Compliance },
211     { "DarkOrchid4", 104, 34, 139, 1, X11Compliance },
212     { "DarkRed", 139, 0, 0, 1, SVGCompliance | X11Compliance },
213     { "DarkSalmon", 233, 150, 122, 1, SVGCompliance | X11Compliance | XPMCompliance },
214     { "DarkSeaGreen", 143, 188, 143, 1, SVGCompliance | X11Compliance | XPMCompliance },
215     { "DarkSeaGreen1", 193, 255, 193, 1, X11Compliance },
216     { "DarkSeaGreen2", 180, 238, 180, 1, X11Compliance },
217     { "DarkSeaGreen3", 155, 205, 155, 1, X11Compliance },
218     { "DarkSeaGreen4", 105, 139, 105, 1, X11Compliance },
219     { "DarkSlateBlue", 72, 61, 139, 1, SVGCompliance | X11Compliance | XPMCompliance },
220     { "DarkSlateGray", 47, 79, 79, 1, SVGCompliance | X11Compliance | XPMCompliance },
221     { "DarkSlateGray1", 151, 255, 255, 1, X11Compliance },
222     { "DarkSlateGray2", 141, 238, 238, 1, X11Compliance },
223     { "DarkSlateGray3", 121, 205, 205, 1, X11Compliance },
224     { "DarkSlateGray4", 82, 139, 139, 1, X11Compliance },
225     { "DarkSlateGrey", 47, 79, 79, 1, SVGCompliance | X11Compliance },
226     { "DarkTurquoise", 0, 206, 209, 1, SVGCompliance | X11Compliance | XPMCompliance },
227     { "DarkViolet", 148, 0, 211, 1, SVGCompliance | X11Compliance | XPMCompliance },
228     { "DeepPink", 255, 20, 147, 1, SVGCompliance | X11Compliance | XPMCompliance },
229     { "DeepPink1", 255, 20, 147, 1, X11Compliance },
230     { "DeepPink2", 238, 18, 137, 1, X11Compliance },
231     { "DeepPink3", 205, 16, 118, 1, X11Compliance },
232     { "DeepPink4", 139, 10, 80, 1, X11Compliance },
233     { "DeepSkyBlue", 0, 191, 255, 1, SVGCompliance | X11Compliance | XPMCompliance },
234     { "DeepSkyBlue1", 0, 191, 255, 1, X11Compliance },
235     { "DeepSkyBlue2", 0, 178, 238, 1, X11Compliance },
236     { "DeepSkyBlue3", 0, 154, 205, 1, X11Compliance },
237     { "DeepSkyBlue4", 0, 104, 139, 1, X11Compliance },
238     { "DimGray", 105, 105, 105, 1, SVGCompliance | X11Compliance | XPMCompliance },
239     { "DimGrey", 105, 105, 105, 1, SVGCompliance | X11Compliance },
240     { "DodgerBlue", 30, 144, 255, 1, SVGCompliance | X11Compliance | XPMCompliance },
241     { "DodgerBlue1", 30, 144, 255, 1, X11Compliance },
242     { "DodgerBlue2", 28, 134, 238, 1, X11Compliance },
243     { "DodgerBlue3", 24, 116, 205, 1, X11Compliance },
244     { "DodgerBlue4", 16, 78, 139, 1, X11Compliance },
245     { "firebrick", 178, 34, 34, 1, SVGCompliance | X11Compliance | XPMCompliance },
246     { "firebrick1", 255, 48, 48, 1, X11Compliance },
247     { "firebrick2", 238, 44, 44, 1, X11Compliance },
248     { "firebrick3", 205, 38, 38, 1, X11Compliance },
249     { "firebrick4", 139, 26, 26, 1, X11Compliance },
250     { "FloralWhite", 255, 250, 240, 1, SVGCompliance | X11Compliance | XPMCompliance },
251     { "ForestGreen", 34, 139, 34, 1, SVGCompliance | X11Compliance | XPMCompliance },
252     { "fractal", 128, 128, 128, 1, SVGCompliance },
253     { "freeze", 0, 0, 0, 0, SVGCompliance },
254     { "fuchsia", 255, 0, 255, 1, SVGCompliance },
255     { "gainsboro", 220, 220, 220, 1, SVGCompliance | X11Compliance | XPMCompliance },
256     { "GhostWhite", 248, 248, 255, 1, SVGCompliance | X11Compliance | XPMCompliance },
257     { "gold", 255, 215, 0, 1, SVGCompliance | X11Compliance | XPMCompliance },
258     { "gold1", 255, 215, 0, 1, X11Compliance },
259     { "gold2", 238, 201, 0, 1, X11Compliance },
260     { "gold3", 205, 173, 0, 1, X11Compliance },
261     { "gold4", 139, 117, 0, 1, X11Compliance },
262     { "goldenrod", 218, 165, 32, 1, SVGCompliance | X11Compliance | XPMCompliance },
263     { "goldenrod1", 255, 193, 37, 1, X11Compliance },
264     { "goldenrod2", 238, 180, 34, 1, X11Compliance },
265     { "goldenrod3", 205, 155, 29, 1, X11Compliance },
266     { "goldenrod4", 139, 105, 20, 1, X11Compliance },
267     { "gray", 126, 126, 126, 1, SVGCompliance },
268     { "gray", 190, 190, 190, 1, X11Compliance | XPMCompliance },
269     { "gray0", 0, 0, 0, 1, X11Compliance | XPMCompliance },
270     { "gray1", 3, 3, 3, 1, X11Compliance | XPMCompliance },
271     { "gray10", 26, 26, 26, 1, X11Compliance | XPMCompliance },
272     { "gray100", 255, 255, 255, 1, X11Compliance | XPMCompliance },
273     { "gray100", 255, 255, 255, 1, X11Compliance | XPMCompliance },
274     { "gray11", 28, 28, 28, 1, X11Compliance | XPMCompliance },
275     { "gray12", 31, 31, 31, 1, X11Compliance | XPMCompliance },
276     { "gray13", 33, 33, 33, 1, X11Compliance | XPMCompliance },
277     { "gray14", 36, 36, 36, 1, X11Compliance | XPMCompliance },
278     { "gray15", 38, 38, 38, 1, X11Compliance | XPMCompliance },
279     { "gray16", 41, 41, 41, 1, X11Compliance | XPMCompliance },
280     { "gray17", 43, 43, 43, 1, X11Compliance | XPMCompliance },
281     { "gray18", 46, 46, 46, 1, X11Compliance | XPMCompliance },
282     { "gray19", 48, 48, 48, 1, X11Compliance | XPMCompliance },
283     { "gray2", 5, 5, 5, 1, X11Compliance | XPMCompliance },
284     { "gray20", 51, 51, 51, 1, X11Compliance | XPMCompliance },
285     { "gray21", 54, 54, 54, 1, X11Compliance | XPMCompliance },
286     { "gray22", 56, 56, 56, 1, X11Compliance | XPMCompliance },
287     { "gray23", 59, 59, 59, 1, X11Compliance | XPMCompliance },
288     { "gray24", 61, 61, 61, 1, X11Compliance | XPMCompliance },
289     { "gray25", 64, 64, 64, 1, X11Compliance | XPMCompliance },
290     { "gray26", 66, 66, 66, 1, X11Compliance | XPMCompliance },
291     { "gray27", 69, 69, 69, 1, X11Compliance | XPMCompliance },
292     { "gray28", 71, 71, 71, 1, X11Compliance | XPMCompliance },
293     { "gray29", 74, 74, 74, 1, X11Compliance | XPMCompliance },
294     { "gray3", 8, 8, 8, 1, X11Compliance | XPMCompliance },
295     { "gray30", 77, 77, 77, 1, X11Compliance | XPMCompliance },
296     { "gray31", 79, 79, 79, 1, X11Compliance | XPMCompliance },
297     { "gray32", 82, 82, 82, 1, X11Compliance | XPMCompliance },
298     { "gray33", 84, 84, 84, 1, X11Compliance | XPMCompliance },
299     { "gray34", 87, 87, 87, 1, X11Compliance | XPMCompliance },
300     { "gray35", 89, 89, 89, 1, X11Compliance | XPMCompliance },
301     { "gray36", 92, 92, 92, 1, X11Compliance | XPMCompliance },
302     { "gray37", 94, 94, 94, 1, X11Compliance | XPMCompliance },
303     { "gray38", 97, 97, 97, 1, X11Compliance | XPMCompliance },
304     { "gray39", 99, 99, 99, 1, X11Compliance | XPMCompliance },
305     { "gray4", 10, 10, 10, 1, X11Compliance | XPMCompliance },
306     { "gray40", 102, 102, 102, 1, X11Compliance | XPMCompliance },
307     { "gray41", 105, 105, 105, 1, X11Compliance | XPMCompliance },
308     { "gray42", 107, 107, 107, 1, X11Compliance | XPMCompliance },
309     { "gray43", 110, 110, 110, 1, X11Compliance | XPMCompliance },
310     { "gray44", 112, 112, 112, 1, X11Compliance | XPMCompliance },
311     { "gray45", 115, 115, 115, 1, X11Compliance | XPMCompliance },
312     { "gray46", 117, 117, 117, 1, X11Compliance | XPMCompliance },
313     { "gray47", 120, 120, 120, 1, X11Compliance | XPMCompliance },
314     { "gray48", 122, 122, 122, 1, X11Compliance | XPMCompliance },
315     { "gray49", 125, 125, 125, 1, X11Compliance | XPMCompliance },
316     { "gray5", 13, 13, 13, 1, X11Compliance | XPMCompliance },
317     { "gray50", 127, 127, 127, 1, X11Compliance | XPMCompliance },
318     { "gray51", 130, 130, 130, 1, X11Compliance | XPMCompliance },
319     { "gray52", 133, 133, 133, 1, X11Compliance | XPMCompliance },
320     { "gray53", 135, 135, 135, 1, X11Compliance | XPMCompliance },
321     { "gray54", 138, 138, 138, 1, X11Compliance | XPMCompliance },
322     { "gray55", 140, 140, 140, 1, X11Compliance | XPMCompliance },
323     { "gray56", 143, 143, 143, 1, X11Compliance | XPMCompliance },
324     { "gray57", 145, 145, 145, 1, X11Compliance | XPMCompliance },
325     { "gray58", 148, 148, 148, 1, X11Compliance | XPMCompliance },
326     { "gray59", 150, 150, 150, 1, X11Compliance | XPMCompliance },
327     { "gray6", 15, 15, 15, 1, X11Compliance | XPMCompliance },
328     { "gray60", 153, 153, 153, 1, X11Compliance | XPMCompliance },
329     { "gray61", 156, 156, 156, 1, X11Compliance | XPMCompliance },
330     { "gray62", 158, 158, 158, 1, X11Compliance | XPMCompliance },
331     { "gray63", 161, 161, 161, 1, X11Compliance | XPMCompliance },
332     { "gray64", 163, 163, 163, 1, X11Compliance | XPMCompliance },
333     { "gray65", 166, 166, 166, 1, X11Compliance | XPMCompliance },
334     { "gray66", 168, 168, 168, 1, X11Compliance | XPMCompliance },
335     { "gray67", 171, 171, 171, 1, X11Compliance | XPMCompliance },
336     { "gray68", 173, 173, 173, 1, X11Compliance | XPMCompliance },
337     { "gray69", 176, 176, 176, 1, X11Compliance | XPMCompliance },
338     { "gray7", 18, 18, 18, 1, X11Compliance | XPMCompliance },
339     { "gray70", 179, 179, 179, 1, X11Compliance | XPMCompliance },
340     { "gray71", 181, 181, 181, 1, X11Compliance | XPMCompliance },
341     { "gray72", 184, 184, 184, 1, X11Compliance | XPMCompliance },
342     { "gray73", 186, 186, 186, 1, X11Compliance | XPMCompliance },
343     { "gray74", 189, 189, 189, 1, X11Compliance | XPMCompliance },
344     { "gray75", 191, 191, 191, 1, X11Compliance | XPMCompliance },
345     { "gray76", 194, 194, 194, 1, X11Compliance | XPMCompliance },
346     { "gray77", 196, 196, 196, 1, X11Compliance | XPMCompliance },
347     { "gray78", 199, 199, 199, 1, X11Compliance | XPMCompliance },
348     { "gray79", 201, 201, 201, 1, X11Compliance | XPMCompliance },
349     { "gray8", 20, 20, 20, 1, X11Compliance | XPMCompliance },
350     { "gray80", 204, 204, 204, 1, X11Compliance | XPMCompliance },
351     { "gray81", 207, 207, 207, 1, X11Compliance | XPMCompliance },
352     { "gray82", 209, 209, 209, 1, X11Compliance | XPMCompliance },
353     { "gray83", 212, 212, 212, 1, X11Compliance | XPMCompliance },
354     { "gray84", 214, 214, 214, 1, X11Compliance | XPMCompliance },
355     { "gray85", 217, 217, 217, 1, X11Compliance | XPMCompliance },
356     { "gray86", 219, 219, 219, 1, X11Compliance | XPMCompliance },
357     { "gray87", 222, 222, 222, 1, X11Compliance | XPMCompliance },
358     { "gray88", 224, 224, 224, 1, X11Compliance | XPMCompliance },
359     { "gray89", 227, 227, 227, 1, X11Compliance | XPMCompliance },
360     { "gray9", 23, 23, 23, 1, X11Compliance | XPMCompliance },
361     { "gray90", 229, 229, 229, 1, X11Compliance | XPMCompliance },
362     { "gray91", 232, 232, 232, 1, X11Compliance | XPMCompliance },
363     { "gray92", 235, 235, 235, 1, X11Compliance | XPMCompliance },
364     { "gray93", 237, 237, 237, 1, X11Compliance | XPMCompliance },
365     { "gray94", 240, 240, 240, 1, X11Compliance | XPMCompliance },
366     { "gray95", 242, 242, 242, 1, X11Compliance | XPMCompliance },
367     { "gray96", 245, 245, 245, 1, X11Compliance | XPMCompliance },
368     { "gray97", 247, 247, 247, 1, X11Compliance | XPMCompliance },
369     { "gray98", 250, 250, 250, 1, X11Compliance | XPMCompliance },
370     { "gray99", 252, 252, 252, 1, X11Compliance | XPMCompliance },
371     { "green", 0, 255, 0, 1, X11Compliance | XPMCompliance },
372     { "green1", 0, 255, 0, 1, X11Compliance },
373     { "green2", 0, 238, 0, 1, X11Compliance },
374     { "green3", 0, 205, 0, 1, X11Compliance },
375     { "green4", 0, 139, 0, 1, X11Compliance },
376     { "GreenYellow", 173, 255, 47, 1, X11Compliance | XPMCompliance },
377     { "grey", 190, 190, 190, 1, SVGCompliance | X11Compliance },
378     { "grey0", 0, 0, 0, 1, SVGCompliance | X11Compliance },
379     { "grey1", 3, 3, 3, 1, SVGCompliance | X11Compliance },
380     { "grey10", 26, 26, 26, 1, SVGCompliance | X11Compliance },
381     { "grey100", 255, 255, 255, 1, SVGCompliance | X11Compliance },
382     { "grey11", 28, 28, 28, 1, SVGCompliance | X11Compliance },
383     { "grey12", 31, 31, 31, 1, SVGCompliance | X11Compliance },
384     { "grey13", 33, 33, 33, 1, SVGCompliance | X11Compliance },
385     { "grey14", 36, 36, 36, 1, SVGCompliance | X11Compliance },
386     { "grey15", 38, 38, 38, 1, SVGCompliance | X11Compliance },
387     { "grey16", 41, 41, 41, 1, SVGCompliance | X11Compliance },
388     { "grey17", 43, 43, 43, 1, SVGCompliance | X11Compliance },
389     { "grey18", 46, 46, 46, 1, SVGCompliance | X11Compliance },
390     { "grey19", 48, 48, 48, 1, SVGCompliance | X11Compliance },
391     { "grey2", 5, 5, 5, 1, SVGCompliance | X11Compliance },
392     { "grey20", 51, 51, 51, 1, SVGCompliance | X11Compliance },
393     { "grey21", 54, 54, 54, 1, SVGCompliance | X11Compliance },
394     { "grey22", 56, 56, 56, 1, SVGCompliance | X11Compliance },
395     { "grey23", 59, 59, 59, 1, SVGCompliance | X11Compliance },
396     { "grey24", 61, 61, 61, 1, SVGCompliance | X11Compliance },
397     { "grey25", 64, 64, 64, 1, SVGCompliance | X11Compliance },
398     { "grey26", 66, 66, 66, 1, SVGCompliance | X11Compliance },
399     { "grey27", 69, 69, 69, 1, SVGCompliance | X11Compliance },
400     { "grey28", 71, 71, 71, 1, SVGCompliance | X11Compliance },
401     { "grey29", 74, 74, 74, 1, SVGCompliance | X11Compliance },
402     { "grey3", 8, 8, 8, 1, SVGCompliance | X11Compliance },
403     { "grey30", 77, 77, 77, 1, SVGCompliance | X11Compliance },
404     { "grey31", 79, 79, 79, 1, SVGCompliance | X11Compliance },
405     { "grey32", 82, 82, 82, 1, SVGCompliance | X11Compliance },
406     { "grey33", 84, 84, 84, 1, SVGCompliance | X11Compliance },
407     { "grey34", 87, 87, 87, 1, SVGCompliance | X11Compliance },
408     { "grey35", 89, 89, 89, 1, SVGCompliance | X11Compliance },
409     { "grey36", 92, 92, 92, 1, SVGCompliance | X11Compliance },
410     { "grey37", 94, 94, 94, 1, SVGCompliance | X11Compliance },
411     { "grey38", 97, 97, 97, 1, SVGCompliance | X11Compliance },
412     { "grey39", 99, 99, 99, 1, SVGCompliance | X11Compliance },
413     { "grey4", 10, 10, 10, 1, SVGCompliance | X11Compliance },
414     { "grey40", 102, 102, 102, 1, SVGCompliance | X11Compliance },
415     { "grey41", 105, 105, 105, 1, SVGCompliance | X11Compliance },
416     { "grey42", 107, 107, 107, 1, SVGCompliance | X11Compliance },
417     { "grey43", 110, 110, 110, 1, SVGCompliance | X11Compliance },
418     { "grey44", 112, 112, 112, 1, SVGCompliance | X11Compliance },
419     { "grey45", 115, 115, 115, 1, SVGCompliance | X11Compliance },
420     { "grey46", 117, 117, 117, 1, SVGCompliance | X11Compliance },
421     { "grey47", 120, 120, 120, 1, SVGCompliance | X11Compliance },
422     { "grey48", 122, 122, 122, 1, SVGCompliance | X11Compliance },
423     { "grey49", 125, 125, 125, 1, SVGCompliance | X11Compliance },
424     { "grey5", 13, 13, 13, 1, SVGCompliance | X11Compliance },
425     { "grey50", 127, 127, 127, 1, SVGCompliance | X11Compliance },
426     { "grey51", 130, 130, 130, 1, SVGCompliance | X11Compliance },
427     { "grey52", 133, 133, 133, 1, SVGCompliance | X11Compliance },
428     { "grey53", 135, 135, 135, 1, SVGCompliance | X11Compliance },
429     { "grey54", 138, 138, 138, 1, SVGCompliance | X11Compliance },
430     { "grey55", 140, 140, 140, 1, SVGCompliance | X11Compliance },
431     { "grey56", 143, 143, 143, 1, SVGCompliance | X11Compliance },
432     { "grey57", 145, 145, 145, 1, SVGCompliance | X11Compliance },
433     { "grey58", 148, 148, 148, 1, SVGCompliance | X11Compliance },
434     { "grey59", 150, 150, 150, 1, SVGCompliance | X11Compliance },
435     { "grey6", 15, 15, 15, 1, SVGCompliance | X11Compliance },
436     { "grey60", 153, 153, 153, 1, SVGCompliance | X11Compliance },
437     { "grey61", 156, 156, 156, 1, SVGCompliance | X11Compliance },
438     { "grey62", 158, 158, 158, 1, SVGCompliance | X11Compliance },
439     { "grey63", 161, 161, 161, 1, SVGCompliance | X11Compliance },
440     { "grey64", 163, 163, 163, 1, SVGCompliance | X11Compliance },
441     { "grey65", 166, 166, 166, 1, SVGCompliance | X11Compliance },
442     { "grey66", 168, 168, 168, 1, SVGCompliance | X11Compliance },
443     { "grey67", 171, 171, 171, 1, SVGCompliance | X11Compliance },
444     { "grey68", 173, 173, 173, 1, SVGCompliance | X11Compliance },
445     { "grey69", 176, 176, 176, 1, SVGCompliance | X11Compliance },
446     { "grey7", 18, 18, 18, 1, SVGCompliance | X11Compliance },
447     { "grey70", 179, 179, 179, 1, SVGCompliance | X11Compliance },
448     { "grey71", 181, 181, 181, 1, SVGCompliance | X11Compliance },
449     { "grey72", 184, 184, 184, 1, SVGCompliance | X11Compliance },
450     { "grey73", 186, 186, 186, 1, SVGCompliance | X11Compliance },
451     { "grey74", 189, 189, 189, 1, SVGCompliance | X11Compliance },
452     { "grey75", 191, 191, 191, 1, SVGCompliance | X11Compliance },
453     { "grey76", 194, 194, 194, 1, SVGCompliance | X11Compliance },
454     { "grey77", 196, 196, 196, 1, SVGCompliance | X11Compliance },
455     { "grey78", 199, 199, 199, 1, SVGCompliance | X11Compliance },
456     { "grey79", 201, 201, 201, 1, SVGCompliance | X11Compliance },
457     { "grey8", 20, 20, 20, 1, SVGCompliance | X11Compliance },
458     { "grey80", 204, 204, 204, 1, SVGCompliance | X11Compliance },
459     { "grey81", 207, 207, 207, 1, SVGCompliance | X11Compliance },
460     { "grey82", 209, 209, 209, 1, SVGCompliance | X11Compliance },
461     { "grey83", 212, 212, 212, 1, SVGCompliance | X11Compliance },
462     { "grey84", 214, 214, 214, 1, SVGCompliance | X11Compliance },
463     { "grey85", 217, 217, 217, 1, SVGCompliance | X11Compliance },
464     { "grey86", 219, 219, 219, 1, SVGCompliance | X11Compliance },
465     { "grey87", 222, 222, 222, 1, SVGCompliance | X11Compliance },
466     { "grey88", 224, 224, 224, 1, SVGCompliance | X11Compliance },
467     { "grey89", 227, 227, 227, 1, SVGCompliance | X11Compliance },
468     { "grey9", 23, 23, 23, 1, SVGCompliance | X11Compliance },
469     { "grey90", 229, 229, 229, 1, SVGCompliance | X11Compliance },
470     { "grey91", 232, 232, 232, 1, SVGCompliance | X11Compliance },
471     { "grey92", 235, 235, 235, 1, SVGCompliance | X11Compliance },
472     { "grey93", 237, 237, 237, 1, SVGCompliance | X11Compliance },
473     { "grey94", 240, 240, 240, 1, SVGCompliance | X11Compliance },
474     { "grey95", 242, 242, 242, 1, SVGCompliance | X11Compliance },
475     { "grey96", 245, 245, 245, 1, SVGCompliance | X11Compliance },
476     { "grey97", 247, 247, 247, 1, SVGCompliance | X11Compliance },
477     { "grey98", 250, 250, 250, 1, SVGCompliance | X11Compliance },
478     { "grey99", 252, 252, 252, 1, SVGCompliance | X11Compliance },
479     { "honeydew", 240, 255, 240, 1, SVGCompliance | X11Compliance | XPMCompliance },
480     { "honeydew1", 240, 255, 240, 1, X11Compliance },
481     { "honeydew2", 224, 238, 224, 1, X11Compliance },
482     { "honeydew3", 193, 205, 193, 1, X11Compliance },
483     { "honeydew4", 131, 139, 131, 1, X11Compliance },
484     { "HotPink", 255, 105, 180, 1, SVGCompliance | X11Compliance | XPMCompliance },
485     { "HotPink1", 255, 110, 180, 1, X11Compliance },
486     { "HotPink2", 238, 106, 167, 1, X11Compliance },
487     { "HotPink3", 205, 96, 144, 1, X11Compliance },
488     { "HotPink4", 139, 58, 98, 1, X11Compliance },
489     { "IndianRed", 205, 92, 92, 1, SVGCompliance | X11Compliance | XPMCompliance },
490     { "IndianRed1", 255, 106, 106, 1, X11Compliance },
491     { "IndianRed2", 238, 99, 99, 1, X11Compliance },
492     { "IndianRed3", 205, 85, 85, 1, X11Compliance },
493     { "IndianRed4", 139, 58, 58, 1, X11Compliance },
494     { "indigo", 75, 0, 130, 1, SVGCompliance },
495     { "ivory", 255, 255, 240, 1, SVGCompliance | X11Compliance | XPMCompliance },
496     { "ivory1", 255, 255, 240, 1, X11Compliance },
497     { "ivory2", 238, 238, 224, 1, X11Compliance },
498     { "ivory3", 205, 205, 193, 1, X11Compliance },
499     { "ivory4", 139, 139, 131, 1, X11Compliance },
500     { "khaki", 240, 230, 140, 1, SVGCompliance | X11Compliance | XPMCompliance },
501     { "khaki1", 255, 246, 143, 1, X11Compliance },
502     { "khaki2", 238, 230, 133, 1, X11Compliance },
503     { "khaki3", 205, 198, 115, 1, X11Compliance },
504     { "khaki4", 139, 134, 78, 1, X11Compliance },
505     { "lavender", 230, 230, 250, 1, SVGCompliance | X11Compliance | XPMCompliance },
506     { "LavenderBlush", 255, 240, 245, 1, SVGCompliance | X11Compliance | XPMCompliance },
507     { "LavenderBlush1", 255, 240, 245, 1, X11Compliance },
508     { "LavenderBlush2", 238, 224, 229, 1, X11Compliance },
509     { "LavenderBlush3", 205, 193, 197, 1, X11Compliance },
510     { "LavenderBlush4", 139, 131, 134, 1, X11Compliance },
511     { "LawnGreen", 124, 252, 0, 1, SVGCompliance | X11Compliance | XPMCompliance },
512     { "LemonChiffon", 255, 250, 205, 1, SVGCompliance | X11Compliance | XPMCompliance },
513     { "LemonChiffon1", 255, 250, 205, 1, X11Compliance },
514     { "LemonChiffon2", 238, 233, 191, 1, X11Compliance },
515     { "LemonChiffon3", 205, 201, 165, 1, X11Compliance },
516     { "LemonChiffon4", 139, 137, 112, 1, X11Compliance },
517     { "LightBlue", 173, 216, 230, 1, SVGCompliance | X11Compliance | XPMCompliance },
518     { "LightBlue1", 191, 239, 255, 1, X11Compliance },
519     { "LightBlue2", 178, 223, 238, 1, X11Compliance },
520     { "LightBlue3", 154, 192, 205, 1, X11Compliance },
521     { "LightBlue4", 104, 131, 139, 1, X11Compliance },
522     { "LightCoral", 240, 128, 128, 1, SVGCompliance | X11Compliance | XPMCompliance },
523     { "LightCyan", 224, 255, 255, 1, SVGCompliance | X11Compliance | XPMCompliance },
524     { "LightCyan1", 224, 255, 255, 1, X11Compliance },
525     { "LightCyan2", 209, 238, 238, 1, X11Compliance },
526     { "LightCyan3", 180, 205, 205, 1, X11Compliance },
527     { "LightCyan4", 122, 139, 139, 1, X11Compliance },
528     { "LightGoldenrod", 238, 221, 130, 1, X11Compliance | XPMCompliance },
529     { "LightGoldenrod1", 255, 236, 139, 1, X11Compliance },
530     { "LightGoldenrod2", 238, 220, 130, 1, X11Compliance },
531     { "LightGoldenrod3", 205, 190, 112, 1, X11Compliance },
532     { "LightGoldenrod4", 139, 129, 76, 1, X11Compliance },
533     { "LightGoldenrodYellow", 250, 250, 210, 1, SVGCompliance | X11Compliance | XPMCompliance },
534     { "LightGray", 211, 211, 211, 1, SVGCompliance | X11Compliance | XPMCompliance },
535     { "LightGreen", 144, 238, 144, 1, SVGCompliance | X11Compliance },
536     { "LightGrey", 211, 211, 211, 1, SVGCompliance | X11Compliance },
537     { "LightPink", 255, 182, 193, 1, SVGCompliance | X11Compliance | XPMCompliance },
538     { "LightPink1", 255, 174, 185, 1, X11Compliance },
539     { "LightPink2", 238, 162, 173, 1, X11Compliance },
540     { "LightPink3", 205, 140, 149, 1, X11Compliance },
541     { "LightPink4", 139, 95, 101, 1, X11Compliance },
542     { "LightSalmon", 255, 160, 122, 1, SVGCompliance | X11Compliance | XPMCompliance },
543     { "LightSalmon1", 255, 160, 122, 1, X11Compliance },
544     { "LightSalmon2", 238, 149, 114, 1, X11Compliance },
545     { "LightSalmon3", 205, 129, 98, 1, X11Compliance },
546     { "LightSalmon4", 139, 87, 66, 1, X11Compliance },
547     { "LightSeaGreen", 32, 178, 170, 1, SVGCompliance | X11Compliance | XPMCompliance },
548     { "LightSkyBlue", 135, 206, 250, 1, SVGCompliance | X11Compliance | XPMCompliance },
549     { "LightSkyBlue1", 176, 226, 255, 1, X11Compliance },
550     { "LightSkyBlue2", 164, 211, 238, 1, X11Compliance },
551     { "LightSkyBlue3", 141, 182, 205, 1, X11Compliance },
552     { "LightSkyBlue4", 96, 123, 139, 1, X11Compliance },
553     { "LightSlateBlue", 132, 112, 255, 1, X11Compliance | XPMCompliance },
554     { "LightSlateGray", 119, 136, 153, 1, SVGCompliance | X11Compliance | XPMCompliance },
555     { "LightSlateGrey", 119, 136, 153, 1, SVGCompliance | X11Compliance },
556     { "LightSteelBlue", 176, 196, 222, 1, SVGCompliance | X11Compliance | XPMCompliance },
557     { "LightSteelBlue1", 202, 225, 255, 1, X11Compliance },
558     { "LightSteelBlue2", 188, 210, 238, 1, X11Compliance },
559     { "LightSteelBlue3", 162, 181, 205, 1, X11Compliance },
560     { "LightSteelBlue4", 110, 123, 139, 1, X11Compliance },
561     { "LightYellow", 255, 255, 224, 1, SVGCompliance | X11Compliance | XPMCompliance },
562     { "LightYellow1", 255, 255, 224, 1, X11Compliance },
563     { "LightYellow2", 238, 238, 209, 1, X11Compliance },
564     { "LightYellow3", 205, 205, 180, 1, X11Compliance },
565     { "LightYellow4", 139, 139, 122, 1, X11Compliance },
566     { "lime", 0, 255, 0, 1, SVGCompliance },
567     { "LimeGreen", 50, 205, 50, 1, SVGCompliance | X11Compliance | XPMCompliance },
568     { "linen", 250, 240, 230, 1, SVGCompliance | X11Compliance | XPMCompliance },
569     { "magenta1", 255, 0, 255, 1, X11Compliance },
570     { "magenta2", 238, 0, 238, 1, X11Compliance },
571     { "magenta3", 205, 0, 205, 1, X11Compliance },
572     { "magenta4", 139, 0, 139, 1, X11Compliance },
573     { "maroon", 128, 0, 0, 1, SVGCompliance },
574     { "maroon", 176, 48, 96, 1, X11Compliance | XPMCompliance },
575     { "maroon1", 255, 52, 179, 1, X11Compliance },
576     { "maroon2", 238, 48, 167, 1, X11Compliance },
577     { "maroon3", 205, 41, 144, 1, X11Compliance },
578     { "maroon4", 139, 28, 98, 1, X11Compliance },
579     { "MediumAquamarine", 102, 205, 170, 1, SVGCompliance | X11Compliance | XPMCompliance },
580     { "MediumBlue", 0, 0, 205, 1, SVGCompliance | X11Compliance | XPMCompliance },
581     { "MediumForestGreen", 50, 129, 75, 1, X11Compliance | XPMCompliance },
582     { "MediumGoldenRod", 209, 193, 102, 1, X11Compliance | XPMCompliance },
583     { "MediumOrchid", 186, 85, 211, 1, SVGCompliance | X11Compliance | XPMCompliance },
584     { "MediumOrchid1", 224, 102, 255, 1, X11Compliance },
585     { "MediumOrchid2", 209, 95, 238, 1, X11Compliance },
586     { "MediumOrchid3", 180, 82, 205, 1, X11Compliance },
587     { "MediumOrchid4", 122, 55, 139, 1, X11Compliance },
588     { "MediumPurple", 147, 112, 219, 1, SVGCompliance | X11Compliance | XPMCompliance },
589     { "MediumPurple1", 171, 130, 255, 1, X11Compliance },
590     { "MediumPurple2", 159, 121, 238, 1, X11Compliance },
591     { "MediumPurple3", 137, 104, 205, 1, X11Compliance },
592     { "MediumPurple4", 93, 71, 139, 1, X11Compliance },
593     { "MediumSeaGreen", 60, 179, 113, 1, SVGCompliance | X11Compliance | XPMCompliance },
594     { "MediumSlateBlue", 123, 104, 238, 1, SVGCompliance | X11Compliance | XPMCompliance },
595     { "MediumSpringGreen", 0, 250, 154, 1, SVGCompliance | X11Compliance | XPMCompliance },
596     { "MediumTurquoise", 72, 209, 204, 1, SVGCompliance | X11Compliance | XPMCompliance },
597     { "MediumVioletRed", 199, 21, 133, 1, SVGCompliance | X11Compliance | XPMCompliance },
598     { "MidnightBlue", 25, 25, 112, 1, SVGCompliance | X11Compliance | XPMCompliance },
599     { "MintCream", 245, 255, 250, 1, SVGCompliance | X11Compliance | XPMCompliance },
600     { "MistyRose", 255, 228, 225, 1, SVGCompliance | X11Compliance | XPMCompliance },
601     { "MistyRose1", 255, 228, 225, 1, X11Compliance },
602     { "MistyRose2", 238, 213, 210, 1, X11Compliance },
603     { "MistyRose3", 205, 183, 181, 1, X11Compliance },
604     { "MistyRose4", 139, 125, 123, 1, X11Compliance },
605     { "moccasin", 255, 228, 181, 1, SVGCompliance | X11Compliance | XPMCompliance },
606     { "NavajoWhite", 255, 222, 173, 1, SVGCompliance | X11Compliance | XPMCompliance },
607     { "NavajoWhite1", 255, 222, 173, 1, X11Compliance },
608     { "NavajoWhite2", 238, 207, 161, 1, X11Compliance },
609     { "NavajoWhite3", 205, 179, 139, 1, X11Compliance },
610     { "NavajoWhite4", 139, 121, 94, 1, X11Compliance },
611     { "navy", 0, 0, 128, 1, SVGCompliance | X11Compliance | XPMCompliance },
612     { "NavyBlue", 0, 0, 128, 1, X11Compliance | XPMCompliance },
613     { "matte", 0, 0, 0, 0, SVGCompliance },
614     { "OldLace", 253, 245, 230, 1, SVGCompliance | X11Compliance | XPMCompliance },
615     { "olive", 128, 128, 0, 1, SVGCompliance },
616     { "OliveDrab", 107, 142, 35, 1, SVGCompliance | X11Compliance | XPMCompliance },
617     { "OliveDrab1", 192, 255, 62, 1, X11Compliance },
618     { "OliveDrab2", 179, 238, 58, 1, X11Compliance },
619     { "OliveDrab3", 154, 205, 50, 1, X11Compliance },
620     { "OliveDrab4", 105, 139, 34, 1, X11Compliance },
621     { "opaque", 0, 0, 0, 1, SVGCompliance },
622     { "orange", 255, 165, 0, 1, SVGCompliance | X11Compliance | XPMCompliance },
623     { "orange1", 255, 165, 0, 1, X11Compliance },
624     { "orange2", 238, 154, 0, 1, X11Compliance },
625     { "orange3", 205, 133, 0, 1, X11Compliance },
626     { "orange4", 139, 90, 0, 1, X11Compliance },
627     { "OrangeRed", 255, 69, 0, 1, SVGCompliance | X11Compliance | XPMCompliance },
628     { "OrangeRed1", 255, 69, 0, 1, X11Compliance },
629     { "OrangeRed2", 238, 64, 0, 1, X11Compliance },
630     { "OrangeRed3", 205, 55, 0, 1, X11Compliance },
631     { "OrangeRed4", 139, 37, 0, 1, X11Compliance },
632     { "orchid", 218, 112, 214, 1, SVGCompliance | X11Compliance | XPMCompliance },
633     { "orchid1", 255, 131, 250, 1, X11Compliance },
634     { "orchid2", 238, 122, 233, 1, X11Compliance },
635     { "orchid3", 205, 105, 201, 1, X11Compliance },
636     { "orchid4", 139, 71, 137, 1, X11Compliance },
637     { "PaleGoldenrod", 238, 232, 170, 1, SVGCompliance | X11Compliance | XPMCompliance },
638     { "PaleGreen", 152, 251, 152, 1, SVGCompliance | X11Compliance | XPMCompliance },
639     { "PaleGreen1", 154, 255, 154, 1, X11Compliance },
640     { "PaleGreen2", 144, 238, 144, 1, X11Compliance },
641     { "PaleGreen3", 124, 205, 124, 1, X11Compliance },
642     { "PaleGreen4", 84, 139, 84, 1, X11Compliance },
643     { "PaleTurquoise", 175, 238, 238, 1, SVGCompliance | X11Compliance | XPMCompliance },
644     { "PaleTurquoise1", 187, 255, 255, 1, X11Compliance },
645     { "PaleTurquoise2", 174, 238, 238, 1, X11Compliance },
646     { "PaleTurquoise3", 150, 205, 205, 1, X11Compliance },
647     { "PaleTurquoise4", 102, 139, 139, 1, X11Compliance },
648     { "PaleVioletRed", 219, 112, 147, 1, SVGCompliance | X11Compliance | XPMCompliance },
649     { "PaleVioletRed1", 255, 130, 171, 1, X11Compliance },
650     { "PaleVioletRed2", 238, 121, 159, 1, X11Compliance },
651     { "PaleVioletRed3", 205, 104, 137, 1, X11Compliance },
652     { "PaleVioletRed4", 139, 71, 93, 1, X11Compliance },
653     { "PapayaWhip", 255, 239, 213, 1, SVGCompliance | X11Compliance | XPMCompliance },
654     { "PeachPuff", 255, 218, 185, 1, SVGCompliance | X11Compliance | XPMCompliance },
655     { "PeachPuff1", 255, 218, 185, 1, X11Compliance },
656     { "PeachPuff2", 238, 203, 173, 1, X11Compliance },
657     { "PeachPuff3", 205, 175, 149, 1, X11Compliance },
658     { "PeachPuff4", 139, 119, 101, 1, X11Compliance },
659     { "peru", 205, 133, 63, 1, SVGCompliance | X11Compliance | XPMCompliance },
660     { "pink", 255, 192, 203, 1, SVGCompliance | X11Compliance | XPMCompliance },
661     { "pink1", 255, 181, 197, 1, X11Compliance },
662     { "pink2", 238, 169, 184, 1, X11Compliance },
663     { "pink3", 205, 145, 158, 1, X11Compliance },
664     { "pink4", 139, 99, 108, 1, X11Compliance },
665     { "plum", 221, 160, 221, 1, SVGCompliance | X11Compliance | XPMCompliance },
666     { "plum1", 255, 187, 255, 1, X11Compliance },
667     { "plum2", 238, 174, 238, 1, X11Compliance },
668     { "plum3", 205, 150, 205, 1, X11Compliance },
669     { "plum4", 139, 102, 139, 1, X11Compliance },
670     { "PowderBlue", 176, 224, 230, 1, SVGCompliance | X11Compliance | XPMCompliance },
671     { "purple", 128, 0, 128, 1, SVGCompliance },
672     { "purple", 160, 32, 240, 1, X11Compliance | XPMCompliance },
673     { "purple1", 155, 48, 255, 1, X11Compliance },
674     { "purple2", 145, 44, 238, 1, X11Compliance },
675     { "purple3", 125, 38, 205, 1, X11Compliance },
676     { "purple4", 85, 26, 139, 1, X11Compliance },
677     { "red1", 255, 0, 0, 1, X11Compliance },
678     { "red2", 238, 0, 0, 1, X11Compliance },
679     { "red3", 205, 0, 0, 1, X11Compliance },
680     { "red4", 139, 0, 0, 1, X11Compliance },
681     { "RosyBrown", 188, 143, 143, 1, SVGCompliance | X11Compliance | XPMCompliance },
682     { "RosyBrown1", 255, 193, 193, 1, X11Compliance },
683     { "RosyBrown2", 238, 180, 180, 1, X11Compliance },
684     { "RosyBrown3", 205, 155, 155, 1, X11Compliance },
685     { "RosyBrown4", 139, 105, 105, 1, X11Compliance },
686     { "RoyalBlue", 65, 105, 225, 1, SVGCompliance | X11Compliance | XPMCompliance },
687     { "RoyalBlue1", 72, 118, 255, 1, X11Compliance },
688     { "RoyalBlue2", 67, 110, 238, 1, X11Compliance },
689     { "RoyalBlue3", 58, 95, 205, 1, X11Compliance },
690     { "RoyalBlue4", 39, 64, 139, 1, X11Compliance },
691     { "SaddleBrown", 139, 69, 19, 1, SVGCompliance | X11Compliance | XPMCompliance },
692     { "salmon", 250, 128, 114, 1, SVGCompliance | X11Compliance | XPMCompliance },
693     { "salmon1", 255, 140, 105, 1, X11Compliance },
694     { "salmon2", 238, 130, 98, 1, X11Compliance },
695     { "salmon3", 205, 112, 84, 1, X11Compliance },
696     { "salmon4", 139, 76, 57, 1, X11Compliance },
697     { "SandyBrown", 244, 164, 96, 1, SVGCompliance | X11Compliance | XPMCompliance },
698     { "SeaGreen", 46, 139, 87, 1, SVGCompliance | X11Compliance | XPMCompliance },
699     { "SeaGreen1", 84, 255, 159, 1, X11Compliance },
700     { "SeaGreen2", 78, 238, 148, 1, X11Compliance },
701     { "SeaGreen3", 67, 205, 128, 1, X11Compliance },
702     { "SeaGreen4", 46, 139, 87, 1, X11Compliance },
703     { "seashell", 255, 245, 238, 1, SVGCompliance | X11Compliance | XPMCompliance },
704     { "seashell1", 255, 245, 238, 1, X11Compliance },
705     { "seashell2", 238, 229, 222, 1, X11Compliance },
706     { "seashell3", 205, 197, 191, 1, X11Compliance },
707     { "seashell4", 139, 134, 130, 1, X11Compliance },
708     { "sienna", 160, 82, 45, 1, SVGCompliance | X11Compliance | XPMCompliance },
709     { "sienna1", 255, 130, 71, 1, X11Compliance },
710     { "sienna2", 238, 121, 66, 1, X11Compliance },
711     { "sienna3", 205, 104, 57, 1, X11Compliance },
712     { "sienna4", 139, 71, 38, 1, X11Compliance },
713     { "silver", 192, 192, 192, 1, SVGCompliance },
714     { "SkyBlue", 135, 206, 235, 1, SVGCompliance | X11Compliance | XPMCompliance },
715     { "SkyBlue1", 135, 206, 255, 1, X11Compliance },
716     { "SkyBlue2", 126, 192, 238, 1, X11Compliance },
717     { "SkyBlue3", 108, 166, 205, 1, X11Compliance },
718     { "SkyBlue4", 74, 112, 139, 1, X11Compliance },
719     { "SlateBlue", 106, 90, 205, 1, SVGCompliance | X11Compliance | XPMCompliance },
720     { "SlateBlue1", 131, 111, 255, 1, X11Compliance },
721     { "SlateBlue2", 122, 103, 238, 1, X11Compliance },
722     { "SlateBlue3", 105, 89, 205, 1, X11Compliance },
723     { "SlateBlue4", 71, 60, 139, 1, X11Compliance },
724     { "SlateGray", 112, 128, 144, 1, SVGCompliance | X11Compliance | XPMCompliance },
725     { "SlateGray1", 198, 226, 255, 1, X11Compliance },
726     { "SlateGray2", 185, 211, 238, 1, X11Compliance },
727     { "SlateGray3", 159, 182, 205, 1, X11Compliance },
728     { "SlateGray4", 108, 123, 139, 1, X11Compliance },
729     { "SlateGrey", 112, 128, 144, 1, SVGCompliance | X11Compliance },
730     { "snow", 255, 250, 250, 1, SVGCompliance | X11Compliance | XPMCompliance },
731     { "snow1", 255, 250, 250, 1, X11Compliance },
732     { "snow2", 238, 233, 233, 1, X11Compliance },
733     { "snow3", 205, 201, 201, 1, X11Compliance },
734     { "snow4", 139, 137, 137, 1, X11Compliance },
735     { "SpringGreen", 0, 255, 127, 1, SVGCompliance | X11Compliance | XPMCompliance },
736     { "SpringGreen1", 0, 255, 127, 1, X11Compliance },
737     { "SpringGreen2", 0, 238, 118, 1, X11Compliance },
738     { "SpringGreen3", 0, 205, 102, 1, X11Compliance },
739     { "SpringGreen4", 0, 139, 69, 1, X11Compliance },
740     { "SteelBlue", 70, 130, 180, 1, SVGCompliance | X11Compliance | XPMCompliance },
741     { "SteelBlue1", 99, 184, 255, 1, X11Compliance },
742     { "SteelBlue2", 92, 172, 238, 1, X11Compliance },
743     { "SteelBlue3", 79, 148, 205, 1, X11Compliance },
744     { "SteelBlue4", 54, 100, 139, 1, X11Compliance },
745     { "tan", 210, 180, 140, 1, SVGCompliance | X11Compliance | XPMCompliance },
746     { "tan1", 255, 165, 79, 1, X11Compliance },
747     { "tan2", 238, 154, 73, 1, X11Compliance },
748     { "tan3", 205, 133, 63, 1, X11Compliance },
749     { "tan4", 139, 90, 43, 1, X11Compliance },
750     { "teal", 0, 128, 128, 1, SVGCompliance },
751     { "thistle", 216, 191, 216, 1, SVGCompliance | X11Compliance | XPMCompliance },
752     { "thistle1", 255, 225, 255, 1, X11Compliance },
753     { "thistle2", 238, 210, 238, 1, X11Compliance },
754     { "thistle3", 205, 181, 205, 1, X11Compliance },
755     { "thistle4", 139, 123, 139, 1, X11Compliance },
756     { "tomato", 255, 99, 71, 1, SVGCompliance | X11Compliance | XPMCompliance },
757     { "tomato1", 255, 99, 71, 1, X11Compliance },
758     { "tomato2", 238, 92, 66, 1, X11Compliance },
759     { "tomato3", 205, 79, 57, 1, X11Compliance },
760     { "tomato4", 139, 54, 38, 1, X11Compliance },
761     { "transparent", 0, 0, 0, 0, SVGCompliance },
762     { "turquoise", 64, 224, 208, 1, SVGCompliance | X11Compliance | XPMCompliance },
763     { "turquoise1", 0, 245, 255, 1, X11Compliance },
764     { "turquoise2", 0, 229, 238, 1, X11Compliance },
765     { "turquoise3", 0, 197, 205, 1, X11Compliance },
766     { "turquoise4", 0, 134, 139, 1, X11Compliance },
767     { "violet", 238, 130, 238, 1, SVGCompliance | X11Compliance | XPMCompliance },
768     { "VioletRed", 208, 32, 144, 1, X11Compliance | XPMCompliance },
769     { "VioletRed1", 255, 62, 150, 1, X11Compliance },
770     { "VioletRed2", 238, 58, 140, 1, X11Compliance },
771     { "VioletRed3", 205, 50, 120, 1, X11Compliance },
772     { "VioletRed4", 139, 34, 82, 1, X11Compliance },
773     { "wheat", 245, 222, 179, 1, SVGCompliance | X11Compliance | XPMCompliance },
774     { "wheat1", 255, 231, 186, 1, X11Compliance },
775     { "wheat2", 238, 216, 174, 1, X11Compliance },
776     { "wheat3", 205, 186, 150, 1, X11Compliance },
777     { "wheat4", 139, 126, 102, 1, X11Compliance },
778     { "WhiteSmoke", 245, 245, 245, 1, SVGCompliance | X11Compliance | XPMCompliance },
779     { "yellow1", 255, 255, 0, 1, X11Compliance },
780     { "yellow2", 238, 238, 0, 1, X11Compliance },
781     { "yellow3", 205, 205, 0, 1, X11Compliance },
782     { "yellow4", 139, 139, 0, 1, X11Compliance },
783     { "YellowGreen", 154, 205, 50, 1, SVGCompliance | X11Compliance | XPMCompliance }
784   };
785 
786 /*
787   Static declarations.
788 */
789 static LinkedListInfo
790   *color_cache = (LinkedListInfo *) NULL;
791 
792 static SemaphoreInfo
793   *color_semaphore = (SemaphoreInfo *) NULL;
794 
795 /*
796   Forward declarations.
797 */
798 static MagickBooleanType
799   IsColorCacheInstantiated(ExceptionInfo *);
800 
801 #if !MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
802 static MagickBooleanType
803   LoadColorCache(LinkedListInfo *,const char *,const char *,const size_t,
804     ExceptionInfo *);
805 #endif
806 
807 /*
808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
809 %                                                                             %
810 %                                                                             %
811 %                                                                             %
812 %  A c q u i r e C o l o r C a c h e                                          %
813 %                                                                             %
814 %                                                                             %
815 %                                                                             %
816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
817 %
818 %  AcquireColorCache() caches one or more color configurations which provides a
819 %  mapping between color attributes and a color name.
820 %
821 %  The format of the AcquireColorCache method is:
822 %
823 %      LinkedListInfo *AcquireColorCache(const char *filename,
824 %        ExceptionInfo *exception)
825 %
826 %  A description of each parameter follows:
827 %
828 %    o filename: the font file name.
829 %
830 %    o exception: return any errors or warnings in this structure.
831 %
832 */
AcquireColorCache(const char * filename,ExceptionInfo * exception)833 static LinkedListInfo *AcquireColorCache(const char *filename,
834   ExceptionInfo *exception)
835 {
836   LinkedListInfo
837     *cache;
838 
839   MagickStatusType
840     status;
841 
842   ssize_t
843     i;
844 
845   /*
846     Load external color map.
847   */
848   cache=NewLinkedList(0);
849   status=MagickTrue;
850 #if !MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
851   {
852     const StringInfo
853       *option;
854 
855     LinkedListInfo
856       *options;
857 
858     options=GetConfigureOptions(filename,exception);
859     option=(const StringInfo *) GetNextValueInLinkedList(options);
860     while (option != (const StringInfo *) NULL)
861     {
862       status&=LoadColorCache(cache,(const char *) GetStringInfoDatum(option),
863         GetStringInfoPath(option),0,exception);
864       option=(const StringInfo *) GetNextValueInLinkedList(options);
865     }
866     options=DestroyConfigureOptions(options);
867   }
868 #endif
869   /*
870     Load built-in color map.
871   */
872   for (i=0; i < (ssize_t) (sizeof(Colormap)/sizeof(*Colormap)); i++)
873   {
874     ColorInfo
875       *color_info;
876 
877     const ColormapInfo
878       *p;
879 
880     p=Colormap+i;
881     color_info=(ColorInfo *) AcquireMagickMemory(sizeof(*color_info));
882     if (color_info == (ColorInfo *) NULL)
883       {
884         (void) ThrowMagickException(exception,GetMagickModule(),
885           ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
886         continue;
887       }
888     (void) memset(color_info,0,sizeof(*color_info));
889     color_info->path=(char *) "[built-in]";
890     color_info->name=(char *) p->name;
891     GetPixelInfo((Image *) NULL,&color_info->color);
892     color_info->color.red=(double) ScaleCharToQuantum(p->red);
893     color_info->color.green=(double) ScaleCharToQuantum(p->green);
894     color_info->color.blue=(double) ScaleCharToQuantum(p->blue);
895     color_info->color.alpha=((double) QuantumRange*p->alpha);
896     color_info->compliance=(ComplianceType) p->compliance;
897     color_info->exempt=MagickTrue;
898     color_info->signature=MagickCoreSignature;
899     status&=AppendValueToLinkedList(cache,color_info);
900     if (status == MagickFalse)
901       (void) ThrowMagickException(exception,GetMagickModule(),
902         ResourceLimitError,"MemoryAllocationFailed","`%s'",color_info->name);
903   }
904   return(cache);
905 }
906 
907 /*
908 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
909 %                                                                             %
910 %                                                                             %
911 %                                                                             %
912 +   C o l o r C o m p o n e n t G e n e s i s                                 %
913 %                                                                             %
914 %                                                                             %
915 %                                                                             %
916 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
917 %
918 %  ColorComponentGenesis() instantiates the color component.
919 %
920 %  The format of the ColorComponentGenesis method is:
921 %
922 %      MagickBooleanType ColorComponentGenesis(void)
923 %
924 */
ColorComponentGenesis(void)925 MagickPrivate MagickBooleanType ColorComponentGenesis(void)
926 {
927   if (color_semaphore == (SemaphoreInfo *) NULL)
928     color_semaphore=AcquireSemaphoreInfo();
929   return(MagickTrue);
930 }
931 
932 /*
933 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
934 %                                                                             %
935 %                                                                             %
936 %                                                                             %
937 +   C o l o r C o m p o n e n t T e r m i n u s                               %
938 %                                                                             %
939 %                                                                             %
940 %                                                                             %
941 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
942 %
943 %  ColorComponentTerminus() destroys the color component.
944 %
945 %  The format of the ColorComponentTerminus method is:
946 %
947 %      ColorComponentTerminus(void)
948 %
949 */
950 
DestroyColorElement(void * color_info)951 static void *DestroyColorElement(void *color_info)
952 {
953   ColorInfo
954     *p;
955 
956   p=(ColorInfo *) color_info;
957   if (p->exempt == MagickFalse)
958     {
959       if (p->path != (char *) NULL)
960         p->path=DestroyString(p->path);
961       if (p->name != (char *) NULL)
962         p->name=DestroyString(p->name);
963     }
964   p=(ColorInfo *) RelinquishMagickMemory(p);
965   return((void *) NULL);
966 }
967 
ColorComponentTerminus(void)968 MagickPrivate void ColorComponentTerminus(void)
969 {
970   if (color_semaphore == (SemaphoreInfo *) NULL)
971     ActivateSemaphoreInfo(&color_semaphore);
972   LockSemaphoreInfo(color_semaphore);
973   if (color_cache != (LinkedListInfo *) NULL)
974     color_cache=DestroyLinkedList(color_cache,DestroyColorElement);
975   UnlockSemaphoreInfo(color_semaphore);
976   RelinquishSemaphoreInfo(&color_semaphore);
977 }
978 
979 /*
980 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
981 %                                                                             %
982 %                                                                             %
983 %                                                                             %
984 +   G e t C o l o r C o m p l i a n c e                                       %
985 %                                                                             %
986 %                                                                             %
987 %                                                                             %
988 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
989 %
990 %  GetColorInfo() searches the color list for the specified name and standards
991 %  compliance and if found returns attributes for that color.
992 %
993 %  The format of the GetColorInfo method is:
994 %
995 %      const PixelInfo *GetColorInfo(const char *name,
996 %        const ComplianceType compliance,ExceptionInfo *exception)
997 %
998 %  A description of each parameter follows:
999 %
1000 %    o name: the color name.
1001 %
1002 %    o compliance: Adhere to this color standard: SVG, X11, or XPM.
1003 %
1004 %    o exception: return any errors or warnings in this structure.
1005 %
1006 */
GetColorCompliance(const char * name,const ComplianceType compliance,ExceptionInfo * exception)1007 MagickExport const ColorInfo *GetColorCompliance(const char *name,
1008   const ComplianceType compliance,ExceptionInfo *exception)
1009 {
1010   char
1011     colorname[MagickPathExtent];
1012 
1013   const ColorInfo
1014     *p;
1015 
1016   char
1017     *q;
1018 
1019   assert(exception != (ExceptionInfo *) NULL);
1020   if (IsColorCacheInstantiated(exception) == MagickFalse)
1021     return((const ColorInfo *) NULL);
1022   /*
1023     Strip names of whitespace.
1024   */
1025   *colorname='\0';
1026   if (name != (const char *) NULL)
1027     (void) CopyMagickString(colorname,name,MagickPathExtent);
1028   for (q=colorname; *q != '\0'; q++)
1029   {
1030     if (isspace((int) ((unsigned char) *q)) == 0)
1031       continue;
1032     (void) CopyMagickString(q,q+1,MagickPathExtent);
1033     q--;
1034   }
1035   /*
1036     Search for color tag.
1037   */
1038   LockSemaphoreInfo(color_semaphore);
1039   ResetLinkedListIterator(color_cache);
1040   p=(const ColorInfo *) GetNextValueInLinkedList(color_cache);
1041   if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
1042     {
1043       UnlockSemaphoreInfo(color_semaphore);
1044       return(p);
1045     }
1046   while (p != (const ColorInfo *) NULL)
1047   {
1048     if (((p->compliance & compliance) != 0) &&
1049         (LocaleCompare(colorname,p->name) == 0))
1050       break;
1051     p=(const ColorInfo *) GetNextValueInLinkedList(color_cache);
1052   }
1053   if (p == (ColorInfo *) NULL)
1054     (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
1055       "UnrecognizedColor","`%s'",name);
1056   else
1057     (void) InsertValueInLinkedList(color_cache,0,
1058       RemoveElementByValueFromLinkedList(color_cache,p));
1059   UnlockSemaphoreInfo(color_semaphore);
1060   return(p);
1061 }
1062 
1063 /*
1064 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1065 %                                                                             %
1066 %                                                                             %
1067 %                                                                             %
1068 +   G e t C o l o r I n f o                                                   %
1069 %                                                                             %
1070 %                                                                             %
1071 %                                                                             %
1072 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1073 %
1074 %  GetColorInfo() searches the color list for the specified name and if found
1075 %  returns attributes for that color.
1076 %
1077 %  The format of the GetColorInfo method is:
1078 %
1079 %      const PixelInfo *GetColorInfo(const char *name,
1080 %        ExceptionInfo *exception)
1081 %
1082 %  A description of each parameter follows:
1083 %
1084 %    o color_info: search the color list for the specified name and if found
1085 %      return attributes for that color.
1086 %
1087 %    o name: the color name.
1088 %
1089 %    o exception: return any errors or warnings in this structure.
1090 %
1091 */
GetColorInfo(const char * name,ExceptionInfo * exception)1092 MagickExport const ColorInfo *GetColorInfo(const char *name,
1093   ExceptionInfo *exception)
1094 {
1095   return(GetColorCompliance(name,AllCompliance,exception));
1096 }
1097 
1098 /*
1099 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1100 %                                                                             %
1101 %                                                                             %
1102 %                                                                             %
1103 +   C o n c a t e n a t e C o l o r C o m p o n e n t                         %
1104 %                                                                             %
1105 %                                                                             %
1106 %                                                                             %
1107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1108 %
1109 %  ConcatenateColorComponent() returns the pixel as a canonical string.
1110 %
1111 %  The format of the ConcatenateColorComponent() method is:
1112 %
1113 %      void ConcatenateColorComponent(const PixelInfo *pixel,
1114 %        const PixelChannel channel,const ComplianceType compliance,
1115 %        char *tuple)
1116 %
1117 %  A description of each parameter follows.
1118 %
1119 %    o pixel:  The pixel.
1120 %
1121 %    o channel:  The pixel channel.
1122 %
1123 %    o compliance: Adhere to this color standard: SVG, X11, or XPM.
1124 %
1125 %    o tuple:  The color tuple.
1126 %
1127 */
1128 
IsSVGCompliant(const PixelInfo * pixel)1129 static inline MagickBooleanType IsSVGCompliant(const PixelInfo *pixel)
1130 {
1131 #define SVGCompliant(component) ((double) \
1132    ScaleCharToQuantum(ScaleQuantumToChar(ClampToQuantum(component))))
1133 #define SVGEpsilon  1.0e-6
1134 
1135   /*
1136     SVG requires color depths > 8 expressed as percentages.
1137   */
1138   if (fabs((double) (SVGCompliant(pixel->red)-pixel->red)) >= SVGEpsilon)
1139     return(MagickFalse);
1140   if (fabs((double) (SVGCompliant(pixel->green)-pixel->green)) >= SVGEpsilon)
1141     return(MagickFalse);
1142   if (fabs((double) (SVGCompliant(pixel->blue)-pixel->blue)) >= SVGEpsilon)
1143     return(MagickFalse);
1144   if ((pixel->colorspace == CMYKColorspace) &&
1145       (fabs((double) (SVGCompliant(pixel->black)-pixel->black)) >= SVGEpsilon))
1146     return(MagickFalse);
1147   return(MagickTrue);
1148 }
1149 
ConcatenateColorComponent(const PixelInfo * pixel,const PixelChannel channel,const ComplianceType compliance,char * tuple)1150 MagickExport void ConcatenateColorComponent(const PixelInfo *pixel,
1151   const PixelChannel channel,const ComplianceType compliance,char *tuple)
1152 {
1153 #define IsColorComponentFactional(color) \
1154   ((color)-ScaleCharToQuantum(ScaleQuantumToChar(color)))
1155 
1156   char
1157     component[MagickPathExtent];
1158 
1159   float
1160     color,
1161     scale;
1162 
1163   color=0.0f;
1164   scale=QuantumRange;
1165   if ((compliance != NoCompliance) || (pixel->depth <= 8))
1166     scale=255.0f;
1167   if ((compliance != NoCompliance) &&
1168       (IssRGBCompatibleColorspace(pixel->colorspace) != MagickFalse) &&
1169       (IsSVGCompliant(pixel) == MagickFalse))
1170     scale=100.0f;
1171   switch (channel)
1172   {
1173     case RedPixelChannel:
1174     {
1175       color=pixel->red;
1176       if (IsHueCompatibleColorspace(pixel->colorspace) != MagickFalse)
1177         scale=360.0f;
1178       if ((compliance != NoCompliance) && (pixel->colorspace == LabColorspace))
1179         scale=100.0f;
1180       break;
1181     }
1182     case GreenPixelChannel:
1183     {
1184       color=pixel->green;
1185       if (IsHueCompatibleColorspace(pixel->colorspace) != MagickFalse)
1186         scale=100.0f;
1187       if ((compliance != NoCompliance) && (pixel->colorspace == LabColorspace))
1188         color-=QuantumRange/2.0f;
1189       break;
1190     }
1191     case BluePixelChannel:
1192     {
1193       color=pixel->blue;
1194       if (IsHueCompatibleColorspace(pixel->colorspace) != MagickFalse)
1195         scale=100.0f;
1196       if (pixel->colorspace == LabColorspace)
1197         color-=QuantumRange/2.0f;
1198       break;
1199     }
1200     case AlphaPixelChannel:
1201     {
1202       color=pixel->alpha;
1203       if (compliance != NoCompliance)
1204         scale=1.0f;
1205       break;
1206     }
1207     case BlackPixelChannel:
1208     {
1209       color=pixel->black;
1210       break;
1211     }
1212     case IndexPixelChannel:
1213     {
1214       color=pixel->index;
1215       break;
1216     }
1217     default:
1218       break;
1219   }
1220   if ((scale != 100.0f) || (pixel->colorspace == LabColorspace))
1221     (void) FormatLocaleString(component,MagickPathExtent,"%.*g",
1222       GetMagickPrecision(),(double) (scale*QuantumScale*color));
1223   else
1224     (void) FormatLocaleString(component,MagickPathExtent,"%.*g%%",
1225       GetMagickPrecision(),(double) (scale*QuantumScale*color));
1226   (void) ConcatenateMagickString(tuple,component,MagickPathExtent);
1227 }
1228 
1229 /*
1230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1231 %                                                                             %
1232 %                                                                             %
1233 %                                                                             %
1234 %   G e t C o l o r I n f o L i s t                                           %
1235 %                                                                             %
1236 %                                                                             %
1237 %                                                                             %
1238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1239 %
1240 %  GetColorInfoList() returns any colors that match the specified pattern.
1241 %
1242 %  The format of the GetColorInfoList function is:
1243 %
1244 %      const ColorInfo **GetColorInfoList(const char *pattern,
1245 %        size_t *number_colors,ExceptionInfo *exception)
1246 %
1247 %  A description of each parameter follows:
1248 %
1249 %    o pattern: Specifies a pointer to a text string containing a pattern.
1250 %
1251 %    o number_colors:  This integer returns the number of colors in the list.
1252 %
1253 %    o exception: return any errors or warnings in this structure.
1254 %
1255 */
1256 
1257 #if defined(__cplusplus) || defined(c_plusplus)
1258 extern "C" {
1259 #endif
1260 
ColorInfoCompare(const void * x,const void * y)1261 static int ColorInfoCompare(const void *x,const void *y)
1262 {
1263   const ColorInfo
1264     **p,
1265     **q;
1266 
1267   int
1268     cmp;
1269 
1270   p=(const ColorInfo **) x,
1271   q=(const ColorInfo **) y;
1272   cmp=LocaleCompare((*p)->path,(*q)->path);
1273   if (cmp == 0)
1274     return(LocaleCompare((*p)->name,(*q)->name));
1275   return(cmp);
1276 }
1277 
1278 #if defined(__cplusplus) || defined(c_plusplus)
1279 }
1280 #endif
1281 
GetColorInfoList(const char * pattern,size_t * number_colors,ExceptionInfo * exception)1282 MagickExport const ColorInfo **GetColorInfoList(const char *pattern,
1283   size_t *number_colors,ExceptionInfo *exception)
1284 {
1285   const ColorInfo
1286     **colors;
1287 
1288   const ColorInfo
1289     *p;
1290 
1291   ssize_t
1292     i;
1293 
1294   /*
1295     Allocate color list.
1296   */
1297   assert(pattern != (char *) NULL);
1298   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
1299   assert(number_colors != (size_t *) NULL);
1300   *number_colors=0;
1301   p=GetColorInfo("*",exception);
1302   if (p == (const ColorInfo *) NULL)
1303     return((const ColorInfo **) NULL);
1304   colors=(const ColorInfo **) AcquireQuantumMemory((size_t)
1305     GetNumberOfElementsInLinkedList(color_cache)+1UL,sizeof(*colors));
1306   if (colors == (const ColorInfo **) NULL)
1307     return((const ColorInfo **) NULL);
1308   /*
1309     Generate color list.
1310   */
1311   LockSemaphoreInfo(color_semaphore);
1312   ResetLinkedListIterator(color_cache);
1313   p=(const ColorInfo *) GetNextValueInLinkedList(color_cache);
1314   for (i=0; p != (const ColorInfo *) NULL; )
1315   {
1316     if ((p->stealth == MagickFalse) &&
1317         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
1318       colors[i++]=p;
1319     p=(const ColorInfo *) GetNextValueInLinkedList(color_cache);
1320   }
1321   UnlockSemaphoreInfo(color_semaphore);
1322   qsort((void *) colors,(size_t) i,sizeof(*colors),ColorInfoCompare);
1323   colors[i]=(ColorInfo *) NULL;
1324   *number_colors=(size_t) i;
1325   return(colors);
1326 }
1327 
1328 /*
1329 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1330 %                                                                             %
1331 %                                                                             %
1332 %                                                                             %
1333 %   G e t C o l o r L i s t                                                   %
1334 %                                                                             %
1335 %                                                                             %
1336 %                                                                             %
1337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1338 %
1339 %  GetColorList() returns any colors that match the specified pattern.
1340 %
1341 %  The format of the GetColorList function is:
1342 %
1343 %      char **GetColorList(const char *pattern,size_t *number_colors,
1344 %        ExceptionInfo *exception)
1345 %
1346 %  A description of each parameter follows:
1347 %
1348 %    o pattern: Specifies a pointer to a text string containing a pattern.
1349 %
1350 %    o number_colors:  This integer returns the number of colors in the list.
1351 %
1352 %    o exception: return any errors or warnings in this structure.
1353 %
1354 */
1355 
1356 #if defined(__cplusplus) || defined(c_plusplus)
1357 extern "C" {
1358 #endif
1359 
ColorCompare(const void * x,const void * y)1360 static int ColorCompare(const void *x,const void *y)
1361 {
1362   const char
1363     **p,
1364     **q;
1365 
1366   p=(const char **) x;
1367   q=(const char **) y;
1368   return(LocaleCompare(*p,*q));
1369 }
1370 
1371 #if defined(__cplusplus) || defined(c_plusplus)
1372 }
1373 #endif
1374 
GetColorList(const char * pattern,size_t * number_colors,ExceptionInfo * exception)1375 MagickExport char **GetColorList(const char *pattern,
1376   size_t *number_colors,ExceptionInfo *exception)
1377 {
1378   char
1379     **colors;
1380 
1381   const ColorInfo
1382     *p;
1383 
1384   ssize_t
1385     i;
1386 
1387   /*
1388     Allocate color list.
1389   */
1390   assert(pattern != (char *) NULL);
1391   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
1392   assert(number_colors != (size_t *) NULL);
1393   *number_colors=0;
1394   p=GetColorInfo("*",exception);
1395   if (p == (const ColorInfo *) NULL)
1396     return((char **) NULL);
1397   colors=(char **) AcquireQuantumMemory((size_t)
1398     GetNumberOfElementsInLinkedList(color_cache)+1UL,sizeof(*colors));
1399   if (colors == (char **) NULL)
1400     return((char **) NULL);
1401   /*
1402     Generate color list.
1403   */
1404   LockSemaphoreInfo(color_semaphore);
1405   ResetLinkedListIterator(color_cache);
1406   p=(const ColorInfo *) GetNextValueInLinkedList(color_cache);
1407   for (i=0; p != (const ColorInfo *) NULL; )
1408   {
1409     if ((p->stealth == MagickFalse) &&
1410         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
1411       colors[i++]=ConstantString(p->name);
1412     p=(const ColorInfo *) GetNextValueInLinkedList(color_cache);
1413   }
1414   UnlockSemaphoreInfo(color_semaphore);
1415   qsort((void *) colors,(size_t) i,sizeof(*colors),ColorCompare);
1416   colors[i]=(char *) NULL;
1417   *number_colors=(size_t) i;
1418   return(colors);
1419 }
1420 
1421 /*
1422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1423 %                                                                             %
1424 %                                                                             %
1425 %                                                                             %
1426 +   G e t C o l o r T u p l e                                                 %
1427 %                                                                             %
1428 %                                                                             %
1429 %                                                                             %
1430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1431 %
1432 %  GetColorTuple() returns a color as a color tuple string (e.g. rgba(255,0,0))
1433 %  or hex string (e.g. #FF0000).
1434 %
1435 %  The format of the GetColorTuple method is:
1436 %
1437 %      GetColorTuple(const PixelInfo *pixel,const MagickBooleanType hex,
1438 %        char *tuple)
1439 %
1440 %  A description of each parameter follows.
1441 %
1442 %    o pixel: the pixel.
1443 %
1444 %    o hex: A value other than zero returns the tuple in a hexidecimal format.
1445 %
1446 %    o tuple: Return the color tuple as this string.
1447 %
1448 */
1449 
ConcatentateHexColorComponent(const PixelInfo * pixel,const PixelChannel channel,char * tuple)1450 static void ConcatentateHexColorComponent(const PixelInfo *pixel,
1451   const PixelChannel channel,char *tuple)
1452 {
1453   char
1454     component[MagickPathExtent];
1455 
1456   double
1457     color;
1458 
1459   color=0.0;
1460   switch (channel)
1461   {
1462     case RedPixelChannel:
1463     {
1464       color=pixel->red;
1465       break;
1466     }
1467     case GreenPixelChannel:
1468     {
1469       color=pixel->green;
1470       break;
1471     }
1472     case BluePixelChannel:
1473     {
1474       color=pixel->blue;
1475       break;
1476     }
1477     case AlphaPixelChannel:
1478     {
1479       color=pixel->alpha;
1480       break;
1481     }
1482     case BlackPixelChannel:
1483     {
1484       color=pixel->black;
1485       break;
1486     }
1487     default:
1488       break;
1489   }
1490   if (pixel->depth > 32)
1491     {
1492       (void) FormatLocaleString(component,MagickPathExtent,"%08lX%08lX",
1493         (unsigned long) ScaleQuantumToLong(ClampToQuantum(color)),
1494         (unsigned long) ScaleQuantumToLong(ClampToQuantum(color)));
1495       (void) ConcatenateMagickString(tuple,component,MagickPathExtent);
1496       return;
1497     }
1498   if (pixel->depth > 16)
1499     {
1500       (void) FormatLocaleString(component,MagickPathExtent,"%08X",
1501         (unsigned int) ScaleQuantumToLong(ClampToQuantum(color)));
1502       (void) ConcatenateMagickString(tuple,component,MagickPathExtent);
1503       return;
1504     }
1505   if (pixel->depth > 8)
1506     {
1507       (void) FormatLocaleString(component,MagickPathExtent,"%04X",
1508         ScaleQuantumToShort(ClampToQuantum(color)));
1509       (void) ConcatenateMagickString(tuple,component,MagickPathExtent);
1510       return;
1511     }
1512   (void) FormatLocaleString(component,MagickPathExtent,"%02X",
1513     ScaleQuantumToChar(ClampToQuantum(color)));
1514   (void) ConcatenateMagickString(tuple,component,MagickPathExtent);
1515   return;
1516 }
1517 
GetColorTuple(const PixelInfo * pixel,const MagickBooleanType hex,char * tuple)1518 MagickExport void GetColorTuple(const PixelInfo *pixel,
1519   const MagickBooleanType hex,char *tuple)
1520 {
1521   PixelInfo
1522     color;
1523 
1524   assert(pixel != (const PixelInfo *) NULL);
1525   assert(tuple != (char *) NULL);
1526   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",tuple);
1527   *tuple='\0';
1528   if (hex != MagickFalse)
1529     {
1530       /*
1531         Convert pixel to hex color.
1532       */
1533       (void) ConcatenateMagickString(tuple,"#",MagickPathExtent);
1534       ConcatentateHexColorComponent(pixel,RedPixelChannel,tuple);
1535       ConcatentateHexColorComponent(pixel,GreenPixelChannel,tuple);
1536       ConcatentateHexColorComponent(pixel,BluePixelChannel,tuple);
1537       if (pixel->colorspace == CMYKColorspace)
1538         ConcatentateHexColorComponent(pixel,BlackPixelChannel,tuple);
1539       if (pixel->alpha_trait != UndefinedPixelTrait)
1540         ConcatentateHexColorComponent(pixel,AlphaPixelChannel,tuple);
1541       return;
1542     }
1543   /*
1544     Convert pixel to rgb() or cmyk() color.
1545   */
1546   color=(*pixel);
1547   if ((color.depth > 8) && (IsSVGCompliant(pixel) != MagickFalse))
1548     color.depth=8;
1549   (void) ConcatenateMagickString(tuple,CommandOptionToMnemonic(
1550     MagickColorspaceOptions,(ssize_t) color.colorspace),MagickPathExtent);
1551   if (color.alpha_trait != UndefinedPixelTrait)
1552     (void) ConcatenateMagickString(tuple,"a",MagickPathExtent);
1553   (void) ConcatenateMagickString(tuple,"(",MagickPathExtent);
1554   if ((color.colorspace == LinearGRAYColorspace) ||
1555       (color.colorspace == GRAYColorspace))
1556     ConcatenateColorComponent(&color,GrayPixelChannel,SVGCompliance,tuple);
1557   else
1558     {
1559       ConcatenateColorComponent(&color,RedPixelChannel,SVGCompliance,tuple);
1560       (void) ConcatenateMagickString(tuple,",",MagickPathExtent);
1561       ConcatenateColorComponent(&color,GreenPixelChannel,SVGCompliance,tuple);
1562       (void) ConcatenateMagickString(tuple,",",MagickPathExtent);
1563       ConcatenateColorComponent(&color,BluePixelChannel,SVGCompliance,tuple);
1564     }
1565   if (color.colorspace == CMYKColorspace)
1566     {
1567       (void) ConcatenateMagickString(tuple,",",MagickPathExtent);
1568       ConcatenateColorComponent(&color,BlackPixelChannel,SVGCompliance,tuple);
1569     }
1570   if (color.alpha_trait != UndefinedPixelTrait)
1571     {
1572       (void) ConcatenateMagickString(tuple,",",MagickPathExtent);
1573       ConcatenateColorComponent(&color,AlphaPixelChannel,SVGCompliance,tuple);
1574     }
1575   (void) ConcatenateMagickString(tuple,")",MagickPathExtent);
1576   LocaleLower(tuple);
1577   return;
1578 }
1579 
1580 /*
1581 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1582 %                                                                             %
1583 %                                                                             %
1584 %                                                                             %
1585 +   I s C o l o r C a c h e I n s t a n t i a t e d                           %
1586 %                                                                             %
1587 %                                                                             %
1588 %                                                                             %
1589 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1590 %
1591 %  IsColorCacheInstantiated() determines if the color list is instantiated.  If
1592 %  not, it instantiates the list and returns it.
1593 %
1594 %  The format of the IsColorInstantiated method is:
1595 %
1596 %      MagickBooleanType IsColorCacheInstantiated(ExceptionInfo *exception)
1597 %
1598 %  A description of each parameter follows.
1599 %
1600 %    o exception: return any errors or warnings in this structure.
1601 %
1602 */
IsColorCacheInstantiated(ExceptionInfo * exception)1603 static MagickBooleanType IsColorCacheInstantiated(ExceptionInfo *exception)
1604 {
1605   if (color_cache == (LinkedListInfo *) NULL)
1606     {
1607       if (color_semaphore == (SemaphoreInfo *) NULL)
1608         ActivateSemaphoreInfo(&color_semaphore);
1609       LockSemaphoreInfo(color_semaphore);
1610       if (color_cache == (LinkedListInfo *) NULL)
1611         color_cache=AcquireColorCache(ColorFilename,exception);
1612       UnlockSemaphoreInfo(color_semaphore);
1613     }
1614   return(color_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
1615 }
1616 
1617 /*
1618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1619 %                                                                             %
1620 %                                                                             %
1621 %                                                                             %
1622 +   I s E q u i v a l e n t A l p h a                                         %
1623 %                                                                             %
1624 %                                                                             %
1625 %                                                                             %
1626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1627 %
1628 %  IsEquivalentAlpha() returns true if the distance between two alpha values is
1629 %  less than the specified distance in a linear color space.  This method is
1630 %  used by MatteFloodFill() and other algorithms which compare two alpha values.
1631 %
1632 %  The format of the IsEquivalentAlpha method is:
1633 %
1634 %      void IsEquivalentAlpha(const Image *image,const PixelInfo *p,
1635 %        const PixelInfo *q)
1636 %
1637 %  A description of each parameter follows:
1638 %
1639 %    o image: the image.
1640 %
1641 %    o p: Pixel p.
1642 %
1643 %    o q: Pixel q.
1644 %
1645 */
IsEquivalentAlpha(const Image * image,const PixelInfo * p,const PixelInfo * q)1646 MagickPrivate MagickBooleanType IsEquivalentAlpha(const Image *image,
1647   const PixelInfo *p,const PixelInfo *q)
1648 {
1649   double
1650     fuzz,
1651     pixel;
1652 
1653   double
1654     distance;
1655 
1656   if (image->alpha_trait == UndefinedPixelTrait)
1657     return(MagickTrue);
1658   if (p->alpha == q->alpha)
1659     return(MagickTrue);
1660   fuzz=MagickMax(image->fuzz,MagickSQ1_2);
1661   fuzz*=fuzz;
1662   pixel=(double) p->alpha-(double) q->alpha;
1663   distance=pixel*pixel;
1664   if (distance > fuzz)
1665     return(MagickFalse);
1666   return(MagickTrue);
1667 }
1668 
1669 /*
1670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1671 %                                                                             %
1672 %                                                                             %
1673 %                                                                             %
1674 +   I s E q u i v a l e n t I m a g e                                         %
1675 %                                                                             %
1676 %                                                                             %
1677 %                                                                             %
1678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1679 %
1680 %  IsEquivalentImage() returns true if the target is similar to a region of the
1681 %  image.
1682 %
1683 %  The format of the IsEquivalentImage method is:
1684 %
1685 %      MagickBooleanType IsEquivalentImage(const Image *image,
1686 %        const Image *target_image,ssize_t *x_offset,ssize_t *y_offset,
1687 %        ExceptionInfo *exception)
1688 %
1689 %  A description of each parameter follows:
1690 %
1691 %    o image: the image.
1692 %
1693 %    o target_image: the target image.
1694 %
1695 %    o x_offset: On input the starting x position to search for a match;
1696 %      on output the x position of the first match found.
1697 %
1698 %    o y_offset: On input the starting y position to search for a match;
1699 %      on output the y position of the first match found.
1700 %
1701 %    o exception: return any errors or warnings in this structure.
1702 %
1703 */
IsEquivalentImage(const Image * image,const Image * target_image,ssize_t * x_offset,ssize_t * y_offset,ExceptionInfo * exception)1704 MagickExport MagickBooleanType IsEquivalentImage(const Image *image,
1705   const Image *target_image,ssize_t *x_offset,ssize_t *y_offset,
1706   ExceptionInfo *exception)
1707 {
1708 #define SearchImageText  "  Searching image...  "
1709 
1710   CacheView
1711     *image_view,
1712     *target_view;
1713 
1714   MagickBooleanType
1715     status;
1716 
1717   PixelInfo
1718     target,
1719     pixel;
1720 
1721   const Quantum
1722     *p,
1723     *q;
1724 
1725   ssize_t
1726     i,
1727     x;
1728 
1729   ssize_t
1730     j,
1731     y;
1732 
1733   assert(image != (Image *) NULL);
1734   assert(image->signature == MagickCoreSignature);
1735   if (image->debug != MagickFalse)
1736     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1737   assert(target_image != (Image *) NULL);
1738   assert(target_image->signature == MagickCoreSignature);
1739   assert(x_offset != (ssize_t *) NULL);
1740   assert(y_offset != (ssize_t *) NULL);
1741   assert(exception != (ExceptionInfo *) NULL);
1742   x=0;
1743   status=MagickTrue;
1744   GetPixelInfo(image,&pixel);
1745   GetPixelInfo(image,&target);
1746   image_view=AcquireVirtualCacheView(image,exception);
1747   target_view=AcquireVirtualCacheView(target_image,exception);
1748   for (y=(*y_offset); y < (ssize_t) image->rows; y++)
1749   {
1750     for (x=y == 0 ? *x_offset : 0; x < (ssize_t) image->columns; x++)
1751     {
1752       for (j=0; j < (ssize_t) target_image->rows; j++)
1753       {
1754         for (i=0; i < (ssize_t) target_image->columns; i++)
1755         {
1756           p=GetCacheViewVirtualPixels(image_view,x+i,y+j,1,1,exception);
1757           if (p == (const Quantum *) NULL)
1758             break;
1759           GetPixelInfoPixel(image,p,&pixel);
1760           q=GetCacheViewVirtualPixels(target_view,i,j,1,1,exception);
1761           if (q == (const Quantum *) NULL)
1762             break;
1763           GetPixelInfoPixel(image,q,&target);
1764           if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
1765             break;
1766         }
1767         if (i < (ssize_t) target_image->columns)
1768           break;
1769       }
1770       if (j == (ssize_t) target_image->rows)
1771         break;
1772     }
1773     if (x < (ssize_t) image->columns)
1774       break;
1775     if (image->progress_monitor != (MagickProgressMonitor) NULL)
1776       {
1777         MagickBooleanType
1778           proceed;
1779 
1780         proceed=SetImageProgress(image,SearchImageText,(MagickOffsetType) y,
1781           image->rows);
1782         if (proceed == MagickFalse)
1783           status=MagickFalse;
1784       }
1785   }
1786   target_view=DestroyCacheView(target_view);
1787   image_view=DestroyCacheView(image_view);
1788   *x_offset=x;
1789   *y_offset=y;
1790   if (status == MagickFalse)
1791     return(status);
1792   return(y < (ssize_t) image->rows ? MagickTrue : MagickFalse);
1793 }
1794 
1795 /*
1796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1797 %                                                                             %
1798 %                                                                             %
1799 %                                                                             %
1800 +   I s E q u i v a l e n t I n t e n s i t y                                 %
1801 %                                                                             %
1802 %                                                                             %
1803 %                                                                             %
1804 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1805 %
1806 %  IsEquivalentIntensity() returns true if the distance between two intensity
1807 %  values is less than the specified distance in a linear color space.
1808 %
1809 %  The format of the IsEquivalentIntensity method is:
1810 %
1811 %      void IsEquivalentIntensity(const Image *image,const PixelInfo *p,
1812 %        const PixelInfo *q)
1813 %
1814 %  A description of each parameter follows:
1815 %
1816 %    o image: the image.
1817 %
1818 %    o p: Pixel p.
1819 %
1820 %    o q: Pixel q.
1821 %
1822 */
IsEquivalentIntensity(const Image * image,const PixelInfo * p,const PixelInfo * q)1823 MagickPrivate MagickBooleanType IsEquivalentIntensity(const Image *image,
1824   const PixelInfo *p,const PixelInfo *q)
1825 {
1826   double
1827     fuzz,
1828     pixel;
1829 
1830   double
1831     distance;
1832 
1833   if (GetPixelInfoIntensity(image,p) == GetPixelInfoIntensity(image,q))
1834     return(MagickTrue);
1835   fuzz=MagickMax(image->fuzz,MagickSQ1_2);
1836   fuzz*=fuzz;
1837   pixel=GetPixelInfoIntensity(image,p)-GetPixelInfoIntensity(image,q);
1838   distance=pixel*pixel;
1839   if (distance > fuzz)
1840     return(MagickFalse);
1841   return(MagickTrue);
1842 }
1843 
1844 /*
1845 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1846 %                                                                             %
1847 %                                                                             %
1848 %                                                                             %
1849 %  L i s t C o l o r I n f o                                                  %
1850 %                                                                             %
1851 %                                                                             %
1852 %                                                                             %
1853 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1854 %
1855 %  ListColorInfo() lists color names to the specified file.  Color names
1856 %  are a convenience.  Rather than defining a color by its red, green, and
1857 %  blue intensities just use a color name such as white, blue, or yellow.
1858 %
1859 %  The format of the ListColorInfo method is:
1860 %
1861 %      MagickBooleanType ListColorInfo(FILE *file,ExceptionInfo *exception)
1862 %
1863 %  A description of each parameter follows.
1864 %
1865 %    o file:  List color names to this file handle.
1866 %
1867 %    o exception: return any errors or warnings in this structure.
1868 %
1869 */
ListColorInfo(FILE * file,ExceptionInfo * exception)1870 MagickExport MagickBooleanType ListColorInfo(FILE *file,
1871   ExceptionInfo *exception)
1872 {
1873   char
1874     tuple[MagickPathExtent];
1875 
1876   const char
1877     *path;
1878 
1879   const ColorInfo
1880     **color_info;
1881 
1882   ssize_t
1883     i;
1884 
1885   size_t
1886     number_colors;
1887 
1888   /*
1889     List name and attributes of each color in the list.
1890   */
1891   if (file == (const FILE *) NULL)
1892     file=stdout;
1893   color_info=GetColorInfoList("*",&number_colors,exception);
1894   if (color_info == (const ColorInfo **) NULL)
1895     return(MagickFalse);
1896   path=(const char *) NULL;
1897   for (i=0; i < (ssize_t) number_colors; i++)
1898   {
1899     if (color_info[i]->stealth != MagickFalse)
1900       continue;
1901     if ((path == (const char *) NULL) ||
1902         (LocaleCompare(path,color_info[i]->path) != 0))
1903       {
1904         if (color_info[i]->path != (char *) NULL)
1905           (void) FormatLocaleFile(file,"\nPath: %s\n\n",color_info[i]->path);
1906         (void) FormatLocaleFile(file,
1907           "Name                  Color                  "
1908           "                       Compliance\n");
1909         (void) FormatLocaleFile(file,
1910           "-------------------------------------------------"
1911           "------------------------------\n");
1912       }
1913     path=color_info[i]->path;
1914     (void) FormatLocaleFile(file,"%-21.21s ",color_info[i]->name);
1915     GetColorTuple(&color_info[i]->color,MagickFalse,tuple);
1916     (void) FormatLocaleFile(file,"%-45.45s ",tuple);
1917     if ((color_info[i]->compliance & SVGCompliance) != 0)
1918       (void) FormatLocaleFile(file,"SVG ");
1919     if ((color_info[i]->compliance & X11Compliance) != 0)
1920       (void) FormatLocaleFile(file,"X11 ");
1921     if ((color_info[i]->compliance & XPMCompliance) != 0)
1922       (void) FormatLocaleFile(file,"XPM ");
1923     (void) FormatLocaleFile(file,"\n");
1924   }
1925   color_info=(const ColorInfo **) RelinquishMagickMemory((void *) color_info);
1926   (void) fflush(file);
1927   return(MagickTrue);
1928 }
1929 
1930 #if !MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
1931 /*
1932 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1933 %                                                                             %
1934 %                                                                             %
1935 %                                                                             %
1936 +   L o a d C o l o r C a c h e                                               %
1937 %                                                                             %
1938 %                                                                             %
1939 %                                                                             %
1940 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1941 %
1942 %  LoadColorCache() loads the color configurations which provides a mapping
1943 %  between color attributes and a color name.
1944 %
1945 %  The format of the LoadColorCache method is:
1946 %
1947 %      MagickBooleanType LoadColorCache(LinkedListInfo *cache,const char *xml,
1948 %        const char *filename,const size_t depth,ExceptionInfo *exception)
1949 %
1950 %  A description of each parameter follows:
1951 %
1952 %    o xml:  The color list in XML format.
1953 %
1954 %    o filename:  The color list filename.
1955 %
1956 %    o depth: depth of <include /> statements.
1957 %
1958 %    o exception: return any errors or warnings in this structure.
1959 %
1960 */
LoadColorCache(LinkedListInfo * cache,const char * xml,const char * filename,const size_t depth,ExceptionInfo * exception)1961 static MagickBooleanType LoadColorCache(LinkedListInfo *cache,const char *xml,
1962   const char *filename,const size_t depth,ExceptionInfo *exception)
1963 {
1964   char
1965     keyword[MagickPathExtent],
1966     *token;
1967 
1968   ColorInfo
1969     *color_info;
1970 
1971   const char
1972     *q;
1973 
1974   MagickStatusType
1975     status;
1976 
1977   size_t
1978     extent;
1979 
1980   /*
1981     Load the color map file.
1982   */
1983   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1984     "Loading color file \"%s\" ...",filename);
1985   if (xml == (char *) NULL)
1986     return(MagickFalse);
1987   status=MagickTrue;
1988   color_info=(ColorInfo *) NULL;
1989   token=AcquireString(xml);
1990   extent=strlen(token)+MagickPathExtent;
1991   for (q=(char *) xml; *q != '\0'; )
1992   {
1993     /*
1994       Interpret XML.
1995     */
1996     (void) GetNextToken(q,&q,extent,token);
1997     if (*token == '\0')
1998       break;
1999     (void) CopyMagickString(keyword,token,MagickPathExtent);
2000     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
2001       {
2002         /*
2003           Doctype element.
2004         */
2005         while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
2006           (void) GetNextToken(q,&q,extent,token);
2007         continue;
2008       }
2009     if (LocaleNCompare(keyword,"<!--",4) == 0)
2010       {
2011         /*
2012           Comment element.
2013         */
2014         while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
2015           (void) GetNextToken(q,&q,extent,token);
2016         continue;
2017       }
2018     if (LocaleCompare(keyword,"<include") == 0)
2019       {
2020         /*
2021           Include element.
2022         */
2023         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
2024         {
2025           (void) CopyMagickString(keyword,token,MagickPathExtent);
2026           (void) GetNextToken(q,&q,extent,token);
2027           if (*token != '=')
2028             continue;
2029           (void) GetNextToken(q,&q,extent,token);
2030           if (LocaleCompare(keyword,"file") == 0)
2031             {
2032               if (depth > MagickMaxRecursionDepth)
2033                 (void) ThrowMagickException(exception,GetMagickModule(),
2034                   ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
2035               else
2036                 {
2037                   char
2038                     path[MagickPathExtent],
2039                     *file_xml;
2040 
2041                   GetPathComponent(filename,HeadPath,path);
2042                   if (*path != '\0')
2043                     (void) ConcatenateMagickString(path,DirectorySeparator,
2044                       MagickPathExtent);
2045                   if (*token == *DirectorySeparator)
2046                     (void) CopyMagickString(path,token,MagickPathExtent);
2047                   else
2048                     (void) ConcatenateMagickString(path,token,MagickPathExtent);
2049                   file_xml=FileToXML(path,~0UL);
2050                   if (file_xml != (char *) NULL)
2051                     {
2052                       status&=LoadColorCache(cache,file_xml,path,depth+1,
2053                         exception);
2054                       file_xml=DestroyString(file_xml);
2055                     }
2056                 }
2057             }
2058         }
2059         continue;
2060       }
2061     if (LocaleCompare(keyword,"<color") == 0)
2062       {
2063         /*
2064           Color element.
2065         */
2066         color_info=(ColorInfo *) AcquireCriticalMemory(sizeof(*color_info));
2067         (void) memset(color_info,0,sizeof(*color_info));
2068         color_info->path=ConstantString(filename);
2069         color_info->exempt=MagickFalse;
2070         color_info->signature=MagickCoreSignature;
2071         continue;
2072       }
2073     if (color_info == (ColorInfo *) NULL)
2074       continue;
2075     if ((LocaleCompare(keyword,"/>") == 0) ||
2076         (LocaleCompare(keyword,"</policy>") == 0))
2077       {
2078         status=AppendValueToLinkedList(cache,color_info);
2079         if (status == MagickFalse)
2080           (void) ThrowMagickException(exception,GetMagickModule(),
2081             ResourceLimitError,"MemoryAllocationFailed","`%s'",
2082             color_info->name);
2083         color_info=(ColorInfo *) NULL;
2084         continue;
2085       }
2086     (void) GetNextToken(q,(const char **) NULL,extent,token);
2087     if (*token != '=')
2088       continue;
2089     (void) GetNextToken(q,&q,extent,token);
2090     (void) GetNextToken(q,&q,extent,token);
2091     switch (*keyword)
2092     {
2093       case 'C':
2094       case 'c':
2095       {
2096         if (LocaleCompare((char *) keyword,"color") == 0)
2097           {
2098             (void) QueryColorCompliance(token,AllCompliance,&color_info->color,
2099               exception);
2100             break;
2101           }
2102         if (LocaleCompare((char *) keyword,"compliance") == 0)
2103           {
2104             ssize_t
2105               compliance;
2106 
2107             compliance=color_info->compliance;
2108             if (StringLocateSubstring(token,"SVG") != (char *) NULL)
2109               compliance|=SVGCompliance;
2110             if (StringLocateSubstring(token,"X11") != (char *) NULL)
2111               compliance|=X11Compliance;
2112             if (StringLocateSubstring(token,"XPM") != (char *) NULL)
2113               compliance|=XPMCompliance;
2114             color_info->compliance=(ComplianceType) compliance;
2115             break;
2116           }
2117         break;
2118       }
2119       case 'N':
2120       case 'n':
2121       {
2122         if (LocaleCompare((char *) keyword,"name") == 0)
2123           {
2124             color_info->name=ConstantString(token);
2125             break;
2126           }
2127         break;
2128       }
2129       case 'S':
2130       case 's':
2131       {
2132         if (LocaleCompare((char *) keyword,"stealth") == 0)
2133           {
2134             color_info->stealth=IsStringTrue(token);
2135             break;
2136           }
2137         break;
2138       }
2139       default:
2140         break;
2141     }
2142   }
2143   token=(char *) RelinquishMagickMemory(token);
2144   return(status != 0 ? MagickTrue : MagickFalse);
2145 }
2146 #endif
2147 
2148 /*
2149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2150 %                                                                             %
2151 %                                                                             %
2152 %                                                                             %
2153 +   Q u e r y C o l o r C o m p l i a n c e                                   %
2154 %                                                                             %
2155 %                                                                             %
2156 %                                                                             %
2157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2158 %
2159 %  QueryColorCompliance() returns the red, green, blue, and alpha intensities
2160 %  for a given color name and standards compliance.
2161 %
2162 %  The format of the QueryColorCompliance method is:
2163 %
2164 %      MagickBooleanType QueryColorCompliance(const char *name,
2165 %        const ComplianceType compliance,PixelInfo *color,
2166 %        ExceptionInfo *exception)
2167 %
2168 %  A description of each parameter follows:
2169 %
2170 %    o name: the color name (e.g. white, blue, yellow).
2171 %
2172 %    o compliance: Adhere to this color standard: SVG, X11, or XPM.
2173 %
2174 %    o color: the red, green, blue, and opacity intensities values of the
2175 %      named color in this structure.
2176 %
2177 %    o exception: return any errors or warnings in this structure.
2178 %
2179 */
2180 
ParseCSSColor(const char * magick_restrict color,GeometryInfo * geometry_info)2181 static MagickStatusType ParseCSSColor(const char *magick_restrict color,
2182   GeometryInfo *geometry_info)
2183 {
2184   char
2185     *q;
2186 
2187   ssize_t
2188     i;
2189 
2190   MagickStatusType
2191     flags;
2192 
2193   SetGeometryInfo(geometry_info);
2194   flags=NoValue;
2195   if ((color == (char *) NULL) || (*color == '\0'))
2196     return(flags);
2197   q=(char *) color;
2198   if (*q == '(')
2199     q++;
2200   for (i=0; (i < 5) && (*q != ')') && (*q != '\0'); i++)
2201   {
2202     char
2203       *p;
2204 
2205     double
2206       intensity;
2207 
2208     p=q;
2209     intensity=(float) StringToDouble(p,&q);
2210     if (p == q)
2211       break;
2212     if (*q == '%')
2213       {
2214         intensity*=0.01*255.0;
2215         q++;
2216       }
2217     switch (i)
2218     {
2219       case 0:
2220       {
2221         geometry_info->rho=intensity;
2222         flags|=RhoValue;
2223         if (LocaleNCompare(q,"deg",3) == 0)
2224           q+=3;
2225         break;
2226       }
2227       case 1:
2228       {
2229         geometry_info->sigma=intensity;
2230         flags|=SigmaValue;
2231         break;
2232       }
2233       case 2:
2234       {
2235         geometry_info->xi=intensity;
2236         flags|=XiValue;
2237         break;
2238       }
2239       case 3:
2240       {
2241         geometry_info->psi=intensity;
2242         flags|=PsiValue;
2243         break;
2244       }
2245       case 4:
2246       {
2247         geometry_info->chi=intensity;
2248         flags|=ChiValue;
2249         break;
2250       }
2251     }
2252     while (isspace((int) ((unsigned char) *q)) != 0)
2253       q++;
2254     if (*q == ',')
2255       q++;
2256     if (*q == '/')
2257       {
2258         flags|=AlphaValue;
2259         q++;
2260       }
2261   }
2262   return(flags);
2263 }
2264 
QueryColorCompliance(const char * name,const ComplianceType compliance,PixelInfo * color,ExceptionInfo * exception)2265 MagickExport MagickBooleanType QueryColorCompliance(const char *name,
2266   const ComplianceType compliance,PixelInfo *color,ExceptionInfo *exception)
2267 {
2268   GeometryInfo
2269     geometry_info;
2270 
2271   double
2272     scale;
2273 
2274   MagickStatusType
2275     flags;
2276 
2277   const ColorInfo
2278     *p;
2279 
2280   ssize_t
2281     i;
2282 
2283   ssize_t
2284     type;
2285 
2286   /*
2287     Initialize color return value.
2288   */
2289   assert(name != (const char *) NULL);
2290   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",name);
2291   assert(color != (PixelInfo *) NULL);
2292   if ((name == (char *) NULL) || (*name == '\0'))
2293     name=BackgroundColor;
2294   while (isspace((int) ((unsigned char) *name)) != 0)
2295     name++;
2296   GetPixelInfo((Image *) NULL,color);
2297   if (*name == '#')
2298     {
2299       char
2300         c;
2301 
2302       PixelPacket
2303         pixel;
2304 
2305       QuantumAny
2306         range;
2307 
2308       size_t
2309         depth,
2310         n;
2311 
2312       /*
2313         Parse hex color.
2314       */
2315       (void) memset(&pixel,0,sizeof(pixel));
2316       name++;
2317       for (n=0; isxdigit((int) ((unsigned char) name[n])) != 0; n++) ;
2318       if ((n == 3) || (n == 6) || (n == 9) || (n == 12) || (n == 24) ||
2319           (n == 48))
2320         {
2321           do
2322           {
2323             pixel.red=pixel.green;
2324             pixel.green=pixel.blue;
2325             pixel.blue=0;
2326             for (i=(ssize_t) (n/3-1); i >= 0; i--)
2327             {
2328               c=(*name++);
2329               pixel.blue<<=4;
2330               if ((c >= '0') && (c <= '9'))
2331                 pixel.blue|=(int) (c-'0');
2332               else
2333                 if ((c >= 'A') && (c <= 'F'))
2334                   pixel.blue|=(int) c-((int) 'A'-10);
2335                 else
2336                   if ((c >= 'a') && (c <= 'f'))
2337                     pixel.blue|=(int) c-((int) 'a'-10);
2338                   else
2339                     return(MagickFalse);
2340             }
2341           } while (isxdigit((int) ((unsigned char) *name)) != 0);
2342           depth=4*(n/3);
2343         }
2344       else
2345         {
2346           if ((n != 4) && (n != 8) && (n != 16) && (n != 32) && (n != 64))
2347             {
2348               (void) ThrowMagickException(exception,GetMagickModule(),
2349                 OptionWarning,"UnrecognizedColor","`%s'",name);
2350               return(MagickFalse);
2351             }
2352           do
2353           {
2354             pixel.red=pixel.green;
2355             pixel.green=pixel.blue;
2356             pixel.blue=pixel.alpha;
2357             pixel.alpha=0;
2358             for (i=(ssize_t) (n/4-1); i >= 0; i--)
2359             {
2360               c=(*name++);
2361               pixel.alpha<<=4;
2362               if ((c >= '0') && (c <= '9'))
2363                 pixel.alpha|=(int) (c-'0');
2364               else
2365                 if ((c >= 'A') && (c <= 'F'))
2366                   pixel.alpha|=(int) c-((int) 'A'-10);
2367                 else
2368                   if ((c >= 'a') && (c <= 'f'))
2369                     pixel.alpha|=(int) c-((int) 'a'-10);
2370                   else
2371                     return(MagickFalse);
2372             }
2373           } while (isxdigit((int) ((unsigned char) *name)) != 0);
2374           depth=4*(n/4);
2375         }
2376       color->colorspace=sRGBColorspace;
2377       color->depth=depth;
2378       color->alpha_trait=UndefinedPixelTrait;
2379       range=GetQuantumRange(depth);
2380       color->red=(double) ScaleAnyToQuantum(pixel.red,range);
2381       color->green=(double) ScaleAnyToQuantum(pixel.green,range);
2382       color->blue=(double) ScaleAnyToQuantum(pixel.blue,range);
2383       color->alpha=(double) OpaqueAlpha;
2384       if ((n % 3) != 0)
2385         {
2386           color->alpha_trait=BlendPixelTrait;
2387           color->alpha=(double) ScaleAnyToQuantum(pixel.alpha,range);
2388         }
2389       color->black=0.0;
2390       return(MagickTrue);
2391     }
2392   if (strchr(name,'(') != (char *) NULL)
2393     {
2394       char
2395         colorspace[2*MagickPathExtent];
2396 
2397       MagickBooleanType
2398         icc_color;
2399 
2400       /*
2401         Parse color of the form rgb(100,255,0).
2402       */
2403       (void) memset(colorspace,0,sizeof(colorspace));
2404       (void) CopyMagickString(colorspace,name,MagickPathExtent);
2405       for (i=0; colorspace[i] != '\0'; i++)
2406         if (colorspace[i] == '(')
2407           break;
2408       colorspace[i--]='\0';
2409       scale=(double) ScaleCharToQuantum(1);
2410       icc_color=MagickFalse;
2411       if (LocaleNCompare(colorspace,"device-",7) == 0)
2412         {
2413           (void) CopyMagickString(colorspace,colorspace+7,MagickPathExtent);
2414           if (strchr(name,'%') == (char *) NULL)
2415             scale=(double) QuantumRange;
2416           icc_color=MagickTrue;
2417         }
2418       if ((LocaleCompare(colorspace,"color") == 0) ||
2419           (LocaleCompare(colorspace,"icc-color") == 0))
2420         {
2421           ssize_t
2422             j;
2423 
2424           (void) CopyMagickString(colorspace,name+i+2,MagickPathExtent);
2425           for (j=0; colorspace[j] != '\0'; j++)
2426             if ((colorspace[j] == ' ') || (colorspace[j] == ','))
2427               break;
2428           colorspace[j--]='\0';
2429           i+=j+3;
2430           scale=(double) QuantumRange;
2431           icc_color=MagickTrue;
2432         }
2433       LocaleLower(colorspace);
2434       color->alpha_trait=UndefinedPixelTrait;
2435       if ((i > 0) && (colorspace[i] == 'a'))
2436         {
2437           colorspace[i]='\0';
2438           color->alpha_trait=BlendPixelTrait;
2439         }
2440       type=ParseCommandOption(MagickColorspaceOptions,MagickFalse,colorspace);
2441       if (type < 0)
2442         {
2443           (void) ThrowMagickException(exception,GetMagickModule(),
2444             OptionWarning,"UnrecognizedColor","`%s'",name);
2445           return(MagickFalse);
2446         }
2447       color->colorspace=(ColorspaceType) type;
2448       if ((icc_color == MagickFalse) && (color->colorspace == RGBColorspace))
2449         {
2450           color->colorspace=sRGBColorspace;  /* as required by SVG standard */
2451           color->depth=8;
2452         }
2453       if (i >= (ssize_t) strlen(name))
2454         flags=ParseCSSColor(name,&geometry_info);
2455       else
2456         flags=ParseCSSColor(name+i+1,&geometry_info);
2457       if (flags == 0)
2458         {
2459           char
2460             *colorname;
2461 
2462           ColorspaceType
2463             colorspaceType;
2464 
2465           MagickBooleanType
2466             status;
2467 
2468           colorspaceType=color->colorspace;
2469           if (i >= (ssize_t) strlen(name))
2470             colorname=AcquireString(name);
2471           else
2472             colorname=AcquireString(name+i+1);
2473           (void) SubstituteString(&colorname,"(","");
2474           (void) SubstituteString(&colorname,")","");
2475           status=MagickFalse;
2476           if (LocaleCompare(name,colorname) != 0)
2477             status=QueryColorCompliance(colorname,AllCompliance,color,
2478               exception);
2479           color->colorspace=colorspaceType;
2480           if (*colorname == '\0')
2481             {
2482               (void) ThrowMagickException(exception,GetMagickModule(),
2483                 OptionWarning,"UnrecognizedColor","`%s'",name);
2484               status=MagickFalse;
2485             }
2486           colorname=DestroyString(colorname);
2487           return(status);
2488         }
2489       if ((flags & AlphaValue) != 0)
2490         color->alpha_trait=BlendPixelTrait;
2491       if ((flags & RhoValue) != 0)
2492         color->red=(double) ClampToQuantum((MagickRealType) (scale*
2493           geometry_info.rho));
2494       if ((flags & SigmaValue) != 0)
2495         color->green=(double) ClampToQuantum((MagickRealType) (scale*
2496           geometry_info.sigma));
2497       if ((flags & XiValue) != 0)
2498         color->blue=(double) ClampToQuantum((MagickRealType) (scale*
2499           geometry_info.xi));
2500       color->alpha=(double) OpaqueAlpha;
2501       if ((flags & PsiValue) != 0)
2502         {
2503           if (color->colorspace == CMYKColorspace)
2504             color->black=(double) ClampToQuantum((MagickRealType) (scale*
2505               geometry_info.psi));
2506           else
2507             if (color->alpha_trait != UndefinedPixelTrait)
2508               {
2509                 if ((flags & AlphaValue) != 0)
2510                   color->alpha=(double) ClampToQuantum((MagickRealType) (scale*
2511                     geometry_info.psi));
2512                 else
2513                   color->alpha=(double) ClampToQuantum((MagickRealType) (
2514                     QuantumRange*geometry_info.psi));
2515               }
2516         }
2517       if (((flags & ChiValue) != 0) &&
2518           (color->alpha_trait != UndefinedPixelTrait))
2519         color->alpha=(double) ClampToQuantum(QuantumRange*geometry_info.chi);
2520       if (color->colorspace == LabColorspace)
2521         {
2522           color->red=(MagickRealType) ClampToQuantum((MagickRealType)
2523             (QuantumRange*geometry_info.rho/100.0));
2524           if ((flags & SigmaValue) != 0)
2525             color->green=(MagickRealType) ClampToQuantum((MagickRealType)
2526               (scale*geometry_info.sigma+(QuantumRange+1)/2.0));
2527           if ((flags & XiValue) != 0)
2528             color->blue=(MagickRealType) ClampToQuantum((MagickRealType)
2529               (scale*geometry_info.xi+(QuantumRange+1)/2.0));
2530         }
2531       if ((LocaleCompare(colorspace,"gray") == 0) ||
2532           (LocaleCompare(colorspace,"lineargray") == 0))
2533         {
2534           color->green=color->red;
2535           color->blue=color->red;
2536           if (((flags & SigmaValue) != 0) &&
2537               (color->alpha_trait != UndefinedPixelTrait))
2538             color->alpha=(double) ClampToQuantum(QuantumRange*
2539               geometry_info.sigma);
2540         }
2541       if ((LocaleCompare(colorspace,"HCL") == 0) ||
2542           (LocaleCompare(colorspace,"HSB") == 0) ||
2543           (LocaleCompare(colorspace,"HSL") == 0) ||
2544           (LocaleCompare(colorspace,"HSV") == 0) ||
2545           (LocaleCompare(colorspace,"HWB") == 0))
2546         {
2547           double
2548             blue,
2549             green,
2550             red;
2551 
2552           scale=1.0/255.0;
2553           geometry_info.sigma*=scale;
2554           geometry_info.xi*=scale;
2555           red=0.0;
2556           green=0.0;
2557           blue=0.0;
2558           switch (color->colorspace)
2559           {
2560             case HCLColorspace:
2561             {
2562               ConvertHCLToRGB(fmod(fmod(geometry_info.rho,360.0)+360.0,360.0)/
2563                 360.0,geometry_info.sigma,geometry_info.xi,&red,&green,&blue);
2564               break;
2565             }
2566             case HSBColorspace:
2567             {
2568               ConvertHSBToRGB(fmod(fmod(geometry_info.rho,360.0)+360.0,360.0)/
2569                 360.0,geometry_info.sigma,geometry_info.xi,&red,&green,&blue);
2570               break;
2571             }
2572             case HSLColorspace:
2573             {
2574               ConvertHSLToRGB(fmod(fmod(geometry_info.rho,360.0)+360.0,360.0)/
2575                 360.0,geometry_info.sigma,geometry_info.xi,&red,&green,&blue);
2576               break;
2577             }
2578             case HSVColorspace:
2579             {
2580               ConvertHSVToRGB(fmod(fmod(geometry_info.rho,360.0)+360.0,360.0)/
2581                 360.0,geometry_info.sigma,geometry_info.xi,&red,&green,&blue);
2582               break;
2583             }
2584             case HWBColorspace:
2585             {
2586               ConvertHWBToRGB(fmod(fmod(geometry_info.rho,360.0)+360.0,360.0)/
2587                 360.0,geometry_info.sigma,geometry_info.xi,&red,&green,&blue);
2588               break;
2589             }
2590             default:
2591               break;
2592           }
2593           color->colorspace=sRGBColorspace;
2594           color->red=(MagickRealType) red;
2595           color->green=(MagickRealType) green;
2596           color->blue=(MagickRealType) blue;
2597         }
2598       return(MagickTrue);
2599     }
2600   /*
2601     Parse named color.
2602   */
2603   p=GetColorCompliance(name,compliance,exception);
2604   if (p == (const ColorInfo *) NULL)
2605     return(MagickFalse);
2606   color->colorspace=sRGBColorspace;
2607   if ((LocaleNCompare(name,"gray",4) == 0) ||
2608       (LocaleNCompare(name,"grey",4) == 0))
2609     color->colorspace=GRAYColorspace;
2610   color->depth=8;
2611   color->alpha_trait=p->color.alpha != OpaqueAlpha ? BlendPixelTrait :
2612     UndefinedPixelTrait;
2613   color->red=(double) p->color.red;
2614   color->green=(double) p->color.green;
2615   color->blue=(double) p->color.blue;
2616   color->alpha=(double) p->color.alpha;
2617   color->black=0.0;
2618   return(MagickTrue);
2619 }
2620 
2621 /*
2622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2623 %                                                                             %
2624 %                                                                             %
2625 %                                                                             %
2626 %  Q u e r y C o l o r n a m e                                                %
2627 %                                                                             %
2628 %                                                                             %
2629 %                                                                             %
2630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2631 %
2632 %  QueryColorname() returns a named color for the given color intensity.
2633 %  If an exact match is not found, a hex value is returned instead.  For
2634 %  example an intensity of rgb:(0,0,0) returns black whereas rgb:(223,223,223)
2635 %  returns #dfdfdf.
2636 %
2637 %  UPDATE: the 'image' argument is no longer needed as all information should
2638 %  have been preset using GetPixelInfo().
2639 %
2640 %  The format of the QueryColorname method is:
2641 %
2642 %      MagickBooleanType QueryColorname(const Image *image,
2643 %        const PixelInfo *color,const ComplianceType compliance,char *name,
2644 %        ExceptionInfo *exception)
2645 %
2646 %  A description of each parameter follows.
2647 %
2648 %    o image: the image. (not used! - color gets settings from GetPixelInfo()
2649 %
2650 %    o color: the color intensities.
2651 %
2652 %    o Compliance: Adhere to this color standard: SVG, X11, or XPM.
2653 %
2654 %    o name: Return the color name or hex value.
2655 %
2656 %    o exception: return any errors or warnings in this structure.
2657 %
2658 */
2659 
QueryColorname(const Image * magick_unused (image),const PixelInfo * color,const ComplianceType compliance,char * name,ExceptionInfo * exception)2660 MagickExport MagickBooleanType QueryColorname(
2661   const Image *magick_unused(image),const PixelInfo *color,
2662   const ComplianceType compliance,char *name,ExceptionInfo *exception)
2663 {
2664   PixelInfo
2665     pixel;
2666 
2667   double
2668     alpha;
2669 
2670   const ColorInfo
2671     *p;
2672 
2673   magick_unreferenced(image);
2674   *name='\0';
2675   pixel=(*color);
2676   if (compliance == XPMCompliance)
2677     {
2678       pixel.alpha_trait=UndefinedPixelTrait;
2679       if (pixel.depth > 16)
2680         pixel.depth=16;
2681     }
2682   GetColorTuple(&pixel,compliance != SVGCompliance ? MagickTrue : MagickFalse,
2683     name);
2684   if (IssRGBColorspace(pixel.colorspace) == MagickFalse)
2685     return(MagickFalse);
2686   alpha=color->alpha_trait != UndefinedPixelTrait ? color->alpha : OpaqueAlpha;
2687   (void) GetColorInfo("*",exception);
2688   ResetLinkedListIterator(color_cache);
2689   p=(const ColorInfo *) GetNextValueInLinkedList(color_cache);
2690   while (p != (const ColorInfo *) NULL)
2691   {
2692     if (((p->compliance & compliance) != 0) &&
2693         ((fabs((double) (p->color.red-color->red)) < MagickEpsilon)) &&
2694          (fabs((double) (p->color.green-color->green)) < MagickEpsilon) &&
2695          (fabs((double) (p->color.blue-color->blue)) < MagickEpsilon) &&
2696          (fabs((double) (p->color.alpha-alpha)) < MagickEpsilon))
2697       {
2698         (void) CopyMagickString(name,p->name,MagickPathExtent);
2699         break;
2700       }
2701     p=(const ColorInfo *) GetNextValueInLinkedList(color_cache);
2702   }
2703   return(MagickTrue);
2704 }
2705