Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / video / colors.c
blob76359eaf0f01f9bd9b9a01e1bc46b5aee87b9e22
1 /* named_colors.c - Named color values. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/types.h>
21 #include <grub/gui.h>
22 #include <grub/gui_string_util.h>
23 #include <grub/misc.h>
24 #include <grub/dl.h>
25 #include <grub/i18n.h>
27 GRUB_MOD_LICENSE ("GPLv3+");
29 struct named_color
31 const char *name;
32 grub_video_rgba_color_t color;
36 Named color list generated from the list of SVG color keywords from
37 <http://www.w3.org/TR/css3-color/#svg-color>,
38 processed through the following Perl command:
39 perl -ne 'chomp;split;print "{ \"$_[0]\", RGB_COLOR($_[2]) },\n"'
42 #define RGB_COLOR(r,g,b) {.red = r, .green = g, .blue = b, .alpha = 255}
44 static struct named_color named_colors[] =
46 { "aliceblue", RGB_COLOR(240,248,255) },
47 { "antiquewhite", RGB_COLOR(250,235,215) },
48 { "aqua", RGB_COLOR(0,255,255) },
49 { "aquamarine", RGB_COLOR(127,255,212) },
50 { "azure", RGB_COLOR(240,255,255) },
51 { "beige", RGB_COLOR(245,245,220) },
52 { "bisque", RGB_COLOR(255,228,196) },
53 { "black", RGB_COLOR(0,0,0) },
54 { "blanchedalmond", RGB_COLOR(255,235,205) },
55 { "blue", RGB_COLOR(0,0,255) },
56 { "blueviolet", RGB_COLOR(138,43,226) },
57 { "brown", RGB_COLOR(165,42,42) },
58 { "burlywood", RGB_COLOR(222,184,135) },
59 { "cadetblue", RGB_COLOR(95,158,160) },
60 { "chartreuse", RGB_COLOR(127,255,0) },
61 { "chocolate", RGB_COLOR(210,105,30) },
62 { "coral", RGB_COLOR(255,127,80) },
63 { "cornflowerblue", RGB_COLOR(100,149,237) },
64 { "cornsilk", RGB_COLOR(255,248,220) },
65 { "crimson", RGB_COLOR(220,20,60) },
66 { "cyan", RGB_COLOR(0,255,255) },
67 { "darkblue", RGB_COLOR(0,0,139) },
68 { "darkcyan", RGB_COLOR(0,139,139) },
69 { "darkgoldenrod", RGB_COLOR(184,134,11) },
70 { "darkgray", RGB_COLOR(169,169,169) },
71 { "darkgreen", RGB_COLOR(0,100,0) },
72 { "darkgrey", RGB_COLOR(169,169,169) },
73 { "darkkhaki", RGB_COLOR(189,183,107) },
74 { "darkmagenta", RGB_COLOR(139,0,139) },
75 { "darkolivegreen", RGB_COLOR(85,107,47) },
76 { "darkorange", RGB_COLOR(255,140,0) },
77 { "darkorchid", RGB_COLOR(153,50,204) },
78 { "darkred", RGB_COLOR(139,0,0) },
79 { "darksalmon", RGB_COLOR(233,150,122) },
80 { "darkseagreen", RGB_COLOR(143,188,143) },
81 { "darkslateblue", RGB_COLOR(72,61,139) },
82 { "darkslategray", RGB_COLOR(47,79,79) },
83 { "darkslategrey", RGB_COLOR(47,79,79) },
84 { "darkturquoise", RGB_COLOR(0,206,209) },
85 { "darkviolet", RGB_COLOR(148,0,211) },
86 { "deeppink", RGB_COLOR(255,20,147) },
87 { "deepskyblue", RGB_COLOR(0,191,255) },
88 { "dimgray", RGB_COLOR(105,105,105) },
89 { "dimgrey", RGB_COLOR(105,105,105) },
90 { "dodgerblue", RGB_COLOR(30,144,255) },
91 { "firebrick", RGB_COLOR(178,34,34) },
92 { "floralwhite", RGB_COLOR(255,250,240) },
93 { "forestgreen", RGB_COLOR(34,139,34) },
94 { "fuchsia", RGB_COLOR(255,0,255) },
95 { "gainsboro", RGB_COLOR(220,220,220) },
96 { "ghostwhite", RGB_COLOR(248,248,255) },
97 { "gold", RGB_COLOR(255,215,0) },
98 { "goldenrod", RGB_COLOR(218,165,32) },
99 { "gray", RGB_COLOR(128,128,128) },
100 { "green", RGB_COLOR(0,128,0) },
101 { "greenyellow", RGB_COLOR(173,255,47) },
102 { "grey", RGB_COLOR(128,128,128) },
103 { "honeydew", RGB_COLOR(240,255,240) },
104 { "hotpink", RGB_COLOR(255,105,180) },
105 { "indianred", RGB_COLOR(205,92,92) },
106 { "indigo", RGB_COLOR(75,0,130) },
107 { "ivory", RGB_COLOR(255,255,240) },
108 { "khaki", RGB_COLOR(240,230,140) },
109 { "lavender", RGB_COLOR(230,230,250) },
110 { "lavenderblush", RGB_COLOR(255,240,245) },
111 { "lawngreen", RGB_COLOR(124,252,0) },
112 { "lemonchiffon", RGB_COLOR(255,250,205) },
113 { "lightblue", RGB_COLOR(173,216,230) },
114 { "lightcoral", RGB_COLOR(240,128,128) },
115 { "lightcyan", RGB_COLOR(224,255,255) },
116 { "lightgoldenrodyellow", RGB_COLOR(250,250,210) },
117 { "lightgray", RGB_COLOR(211,211,211) },
118 { "lightgreen", RGB_COLOR(144,238,144) },
119 { "lightgrey", RGB_COLOR(211,211,211) },
120 { "lightpink", RGB_COLOR(255,182,193) },
121 { "lightsalmon", RGB_COLOR(255,160,122) },
122 { "lightseagreen", RGB_COLOR(32,178,170) },
123 { "lightskyblue", RGB_COLOR(135,206,250) },
124 { "lightslategray", RGB_COLOR(119,136,153) },
125 { "lightslategrey", RGB_COLOR(119,136,153) },
126 { "lightsteelblue", RGB_COLOR(176,196,222) },
127 { "lightyellow", RGB_COLOR(255,255,224) },
128 { "lime", RGB_COLOR(0,255,0) },
129 { "limegreen", RGB_COLOR(50,205,50) },
130 { "linen", RGB_COLOR(250,240,230) },
131 { "magenta", RGB_COLOR(255,0,255) },
132 { "maroon", RGB_COLOR(128,0,0) },
133 { "mediumaquamarine", RGB_COLOR(102,205,170) },
134 { "mediumblue", RGB_COLOR(0,0,205) },
135 { "mediumorchid", RGB_COLOR(186,85,211) },
136 { "mediumpurple", RGB_COLOR(147,112,219) },
137 { "mediumseagreen", RGB_COLOR(60,179,113) },
138 { "mediumslateblue", RGB_COLOR(123,104,238) },
139 { "mediumspringgreen", RGB_COLOR(0,250,154) },
140 { "mediumturquoise", RGB_COLOR(72,209,204) },
141 { "mediumvioletred", RGB_COLOR(199,21,133) },
142 { "midnightblue", RGB_COLOR(25,25,112) },
143 { "mintcream", RGB_COLOR(245,255,250) },
144 { "mistyrose", RGB_COLOR(255,228,225) },
145 { "moccasin", RGB_COLOR(255,228,181) },
146 { "navajowhite", RGB_COLOR(255,222,173) },
147 { "navy", RGB_COLOR(0,0,128) },
148 { "oldlace", RGB_COLOR(253,245,230) },
149 { "olive", RGB_COLOR(128,128,0) },
150 { "olivedrab", RGB_COLOR(107,142,35) },
151 { "orange", RGB_COLOR(255,165,0) },
152 { "orangered", RGB_COLOR(255,69,0) },
153 { "orchid", RGB_COLOR(218,112,214) },
154 { "palegoldenrod", RGB_COLOR(238,232,170) },
155 { "palegreen", RGB_COLOR(152,251,152) },
156 { "paleturquoise", RGB_COLOR(175,238,238) },
157 { "palevioletred", RGB_COLOR(219,112,147) },
158 { "papayawhip", RGB_COLOR(255,239,213) },
159 { "peachpuff", RGB_COLOR(255,218,185) },
160 { "peru", RGB_COLOR(205,133,63) },
161 { "pink", RGB_COLOR(255,192,203) },
162 { "plum", RGB_COLOR(221,160,221) },
163 { "powderblue", RGB_COLOR(176,224,230) },
164 { "purple", RGB_COLOR(128,0,128) },
165 { "red", RGB_COLOR(255,0,0) },
166 { "rosybrown", RGB_COLOR(188,143,143) },
167 { "royalblue", RGB_COLOR(65,105,225) },
168 { "saddlebrown", RGB_COLOR(139,69,19) },
169 { "salmon", RGB_COLOR(250,128,114) },
170 { "sandybrown", RGB_COLOR(244,164,96) },
171 { "seagreen", RGB_COLOR(46,139,87) },
172 { "seashell", RGB_COLOR(255,245,238) },
173 { "sienna", RGB_COLOR(160,82,45) },
174 { "silver", RGB_COLOR(192,192,192) },
175 { "skyblue", RGB_COLOR(135,206,235) },
176 { "slateblue", RGB_COLOR(106,90,205) },
177 { "slategray", RGB_COLOR(112,128,144) },
178 { "slategrey", RGB_COLOR(112,128,144) },
179 { "snow", RGB_COLOR(255,250,250) },
180 { "springgreen", RGB_COLOR(0,255,127) },
181 { "steelblue", RGB_COLOR(70,130,180) },
182 { "tan", RGB_COLOR(210,180,140) },
183 { "teal", RGB_COLOR(0,128,128) },
184 { "thistle", RGB_COLOR(216,191,216) },
185 { "tomato", RGB_COLOR(255,99,71) },
186 { "turquoise", RGB_COLOR(64,224,208) },
187 { "violet", RGB_COLOR(238,130,238) },
188 { "wheat", RGB_COLOR(245,222,179) },
189 { "white", RGB_COLOR(255,255,255) },
190 { "whitesmoke", RGB_COLOR(245,245,245) },
191 { "yellow", RGB_COLOR(255,255,0) },
192 { "yellowgreen", RGB_COLOR(154,205,50) },
193 { 0, { 0, 0, 0, 0 } } /* Terminator. */
196 /* Get the color named NAME. If the color was found, returns 1 and
197 stores the color into *COLOR. If the color was not found, returns 0 and
198 does not modify *COLOR. */
200 grub_video_get_named_color (const char *name,
201 grub_video_rgba_color_t *color)
203 int i;
204 for (i = 0; named_colors[i].name; i++)
206 if (grub_strcmp (named_colors[i].name, name) == 0)
208 *color = named_colors[i].color;
209 return 1;
212 return 0;
215 static int
216 parse_hex_color_component (const char *s, unsigned start, unsigned end)
218 unsigned len;
219 char buf[3];
221 len = end - start;
222 /* Check the limits so we don't overrun the buffer. */
223 if (len < 1 || len > 2)
224 return 0;
226 if (len == 1)
228 buf[0] = s[start]; /* Get the first and only hex digit. */
229 buf[1] = buf[0]; /* Duplicate the hex digit. */
231 else if (len == 2)
233 buf[0] = s[start];
234 buf[1] = s[start + 1];
237 buf[2] = '\0';
239 return grub_strtoul (buf, 0, 16);
242 /* Parse a color string of the form "r, g, b", "#RGB", "#RGBA",
243 "#RRGGBB", or "#RRGGBBAA". */
244 grub_err_t
245 grub_video_parse_color (const char *s, grub_video_rgba_color_t *color)
247 grub_video_rgba_color_t c;
248 const char *s0;
250 /* Skip whitespace. */
251 while (*s && grub_isspace (*s))
252 s++;
254 s0 = s;
256 if (*s == '#')
258 /* HTML-style. Number if hex digits:
259 [6] #RRGGBB [3] #RGB
260 [8] #RRGGBBAA [4] #RGBA */
262 s++; /* Skip the '#'. */
263 /* Count the hexits to determine the format. */
264 int hexits = 0;
265 const char *end = s;
266 while (grub_isxdigit (*end))
268 end++;
269 hexits++;
272 /* Parse the color components based on the format. */
273 if (hexits == 3 || hexits == 4)
275 c.red = parse_hex_color_component (s, 0, 1);
276 c.green = parse_hex_color_component (s, 1, 2);
277 c.blue = parse_hex_color_component (s, 2, 3);
278 if (hexits == 4)
279 c.alpha = parse_hex_color_component (s, 3, 4);
280 else
281 c.alpha = 255;
283 else if (hexits == 6 || hexits == 8)
285 c.red = parse_hex_color_component (s, 0, 2);
286 c.green = parse_hex_color_component (s, 2, 4);
287 c.blue = parse_hex_color_component (s, 4, 6);
288 if (hexits == 8)
289 c.alpha = parse_hex_color_component (s, 6, 8);
290 else
291 c.alpha = 255;
293 else
294 return grub_error (GRUB_ERR_BAD_ARGUMENT,
295 N_("invalid color specification `%s'"), s0);
297 else if (grub_isdigit (*s))
299 /* Comma separated decimal values. */
300 c.red = grub_strtoul (s, 0, 0);
301 s = grub_strchr (s, ',');
302 if (!s)
303 return grub_error (GRUB_ERR_BAD_ARGUMENT,
304 N_("invalid color specification `%s'"), s0);
305 s++;
306 c.green = grub_strtoul (s, 0, 0);
307 s = grub_strchr (s, ',');
308 if (!s)
309 return grub_error (GRUB_ERR_BAD_ARGUMENT,
310 N_("invalid color specification `%s'"), s0);
311 s++;
312 c.blue = grub_strtoul (s, 0, 0);
313 s = grub_strchr (s, ',');
314 if (!s)
315 c.alpha = 255;
316 else
318 s++;
319 c.alpha = grub_strtoul (s, 0, 0);
322 else
324 if (! grub_video_get_named_color (s, &c))
325 return grub_error (GRUB_ERR_BAD_ARGUMENT,
326 N_("invalid color specification `%s'"), s0);
329 if (grub_errno == GRUB_ERR_NONE)
330 *color = c;
331 return grub_errno;