Skip to main content

ferroui_base/media/
known_colors.rs

1//! The set of named colors and the lookups between names and ARGB values.
2
3use crate::media::Color;
4
5/// A named color, identified by its ARGB value.
6///
7/// Several names share a value (`AQUA`/`CYAN`, `FUCHSIA`/`MAGENTA`); those constants
8/// compare equal.
9#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
10pub struct KnownColor(u32);
11
12impl KnownColor {
13    pub const NONE: KnownColor = KnownColor(0);
14    pub const ALICE_BLUE: KnownColor = KnownColor(0xfff0f8ff);
15    pub const ANTIQUE_WHITE: KnownColor = KnownColor(0xfffaebd7);
16    pub const AQUA: KnownColor = KnownColor(0xff00ffff);
17    pub const AQUAMARINE: KnownColor = KnownColor(0xff7fffd4);
18    pub const AZURE: KnownColor = KnownColor(0xfff0ffff);
19    pub const BEIGE: KnownColor = KnownColor(0xfff5f5dc);
20    pub const BISQUE: KnownColor = KnownColor(0xffffe4c4);
21    pub const BLACK: KnownColor = KnownColor(0xff000000);
22    pub const BLANCHED_ALMOND: KnownColor = KnownColor(0xffffebcd);
23    pub const BLUE: KnownColor = KnownColor(0xff0000ff);
24    pub const BLUE_VIOLET: KnownColor = KnownColor(0xff8a2be2);
25    pub const BROWN: KnownColor = KnownColor(0xffa52a2a);
26    pub const BURLY_WOOD: KnownColor = KnownColor(0xffdeb887);
27    pub const CADET_BLUE: KnownColor = KnownColor(0xff5f9ea0);
28    pub const CHARTREUSE: KnownColor = KnownColor(0xff7fff00);
29    pub const CHOCOLATE: KnownColor = KnownColor(0xffd2691e);
30    pub const CORAL: KnownColor = KnownColor(0xffff7f50);
31    pub const CORNFLOWER_BLUE: KnownColor = KnownColor(0xff6495ed);
32    pub const CORNSILK: KnownColor = KnownColor(0xfffff8dc);
33    pub const CRIMSON: KnownColor = KnownColor(0xffdc143c);
34    pub const CYAN: KnownColor = KnownColor(0xff00ffff);
35    pub const DARK_BLUE: KnownColor = KnownColor(0xff00008b);
36    pub const DARK_CYAN: KnownColor = KnownColor(0xff008b8b);
37    pub const DARK_GOLDENROD: KnownColor = KnownColor(0xffb8860b);
38    pub const DARK_GRAY: KnownColor = KnownColor(0xffa9a9a9);
39    pub const DARK_GREEN: KnownColor = KnownColor(0xff006400);
40    pub const DARK_KHAKI: KnownColor = KnownColor(0xffbdb76b);
41    pub const DARK_MAGENTA: KnownColor = KnownColor(0xff8b008b);
42    pub const DARK_OLIVE_GREEN: KnownColor = KnownColor(0xff556b2f);
43    pub const DARK_ORANGE: KnownColor = KnownColor(0xffff8c00);
44    pub const DARK_ORCHID: KnownColor = KnownColor(0xff9932cc);
45    pub const DARK_RED: KnownColor = KnownColor(0xff8b0000);
46    pub const DARK_SALMON: KnownColor = KnownColor(0xffe9967a);
47    pub const DARK_SEA_GREEN: KnownColor = KnownColor(0xff8fbc8f);
48    pub const DARK_SLATE_BLUE: KnownColor = KnownColor(0xff483d8b);
49    pub const DARK_SLATE_GRAY: KnownColor = KnownColor(0xff2f4f4f);
50    pub const DARK_TURQUOISE: KnownColor = KnownColor(0xff00ced1);
51    pub const DARK_VIOLET: KnownColor = KnownColor(0xff9400d3);
52    pub const DEEP_PINK: KnownColor = KnownColor(0xffff1493);
53    pub const DEEP_SKY_BLUE: KnownColor = KnownColor(0xff00bfff);
54    pub const DIM_GRAY: KnownColor = KnownColor(0xff696969);
55    pub const DODGER_BLUE: KnownColor = KnownColor(0xff1e90ff);
56    pub const FIREBRICK: KnownColor = KnownColor(0xffb22222);
57    pub const FLORAL_WHITE: KnownColor = KnownColor(0xfffffaf0);
58    pub const FOREST_GREEN: KnownColor = KnownColor(0xff228b22);
59    pub const FUCHSIA: KnownColor = KnownColor(0xffff00ff);
60    pub const GAINSBORO: KnownColor = KnownColor(0xffdcdcdc);
61    pub const GHOST_WHITE: KnownColor = KnownColor(0xfff8f8ff);
62    pub const GOLD: KnownColor = KnownColor(0xffffd700);
63    pub const GOLDENROD: KnownColor = KnownColor(0xffdaa520);
64    pub const GRAY: KnownColor = KnownColor(0xff808080);
65    pub const GREEN: KnownColor = KnownColor(0xff008000);
66    pub const GREEN_YELLOW: KnownColor = KnownColor(0xffadff2f);
67    pub const HONEYDEW: KnownColor = KnownColor(0xfff0fff0);
68    pub const HOT_PINK: KnownColor = KnownColor(0xffff69b4);
69    pub const INDIAN_RED: KnownColor = KnownColor(0xffcd5c5c);
70    pub const INDIGO: KnownColor = KnownColor(0xff4b0082);
71    pub const IVORY: KnownColor = KnownColor(0xfffffff0);
72    pub const KHAKI: KnownColor = KnownColor(0xfff0e68c);
73    pub const LAVENDER: KnownColor = KnownColor(0xffe6e6fa);
74    pub const LAVENDER_BLUSH: KnownColor = KnownColor(0xfffff0f5);
75    pub const LAWN_GREEN: KnownColor = KnownColor(0xff7cfc00);
76    pub const LEMON_CHIFFON: KnownColor = KnownColor(0xfffffacd);
77    pub const LIGHT_BLUE: KnownColor = KnownColor(0xffadd8e6);
78    pub const LIGHT_CORAL: KnownColor = KnownColor(0xfff08080);
79    pub const LIGHT_CYAN: KnownColor = KnownColor(0xffe0ffff);
80    pub const LIGHT_GOLDENROD_YELLOW: KnownColor = KnownColor(0xfffafad2);
81    pub const LIGHT_GREEN: KnownColor = KnownColor(0xff90ee90);
82    pub const LIGHT_GRAY: KnownColor = KnownColor(0xffd3d3d3);
83    pub const LIGHT_PINK: KnownColor = KnownColor(0xffffb6c1);
84    pub const LIGHT_SALMON: KnownColor = KnownColor(0xffffa07a);
85    pub const LIGHT_SEA_GREEN: KnownColor = KnownColor(0xff20b2aa);
86    pub const LIGHT_SKY_BLUE: KnownColor = KnownColor(0xff87cefa);
87    pub const LIGHT_SLATE_GRAY: KnownColor = KnownColor(0xff778899);
88    pub const LIGHT_STEEL_BLUE: KnownColor = KnownColor(0xffb0c4de);
89    pub const LIGHT_YELLOW: KnownColor = KnownColor(0xffffffe0);
90    pub const LIME: KnownColor = KnownColor(0xff00ff00);
91    pub const LIME_GREEN: KnownColor = KnownColor(0xff32cd32);
92    pub const LINEN: KnownColor = KnownColor(0xfffaf0e6);
93    pub const MAGENTA: KnownColor = KnownColor(0xffff00ff);
94    pub const MAROON: KnownColor = KnownColor(0xff800000);
95    pub const MEDIUM_AQUAMARINE: KnownColor = KnownColor(0xff66cdaa);
96    pub const MEDIUM_BLUE: KnownColor = KnownColor(0xff0000cd);
97    pub const MEDIUM_ORCHID: KnownColor = KnownColor(0xffba55d3);
98    pub const MEDIUM_PURPLE: KnownColor = KnownColor(0xff9370db);
99    pub const MEDIUM_SEA_GREEN: KnownColor = KnownColor(0xff3cb371);
100    pub const MEDIUM_SLATE_BLUE: KnownColor = KnownColor(0xff7b68ee);
101    pub const MEDIUM_SPRING_GREEN: KnownColor = KnownColor(0xff00fa9a);
102    pub const MEDIUM_TURQUOISE: KnownColor = KnownColor(0xff48d1cc);
103    pub const MEDIUM_VIOLET_RED: KnownColor = KnownColor(0xffc71585);
104    pub const MIDNIGHT_BLUE: KnownColor = KnownColor(0xff191970);
105    pub const MINT_CREAM: KnownColor = KnownColor(0xfff5fffa);
106    pub const MISTY_ROSE: KnownColor = KnownColor(0xffffe4e1);
107    pub const MOCCASIN: KnownColor = KnownColor(0xffffe4b5);
108    pub const NAVAJO_WHITE: KnownColor = KnownColor(0xffffdead);
109    pub const NAVY: KnownColor = KnownColor(0xff000080);
110    pub const OLD_LACE: KnownColor = KnownColor(0xfffdf5e6);
111    pub const OLIVE: KnownColor = KnownColor(0xff808000);
112    pub const OLIVE_DRAB: KnownColor = KnownColor(0xff6b8e23);
113    pub const ORANGE: KnownColor = KnownColor(0xffffa500);
114    pub const ORANGE_RED: KnownColor = KnownColor(0xffff4500);
115    pub const ORCHID: KnownColor = KnownColor(0xffda70d6);
116    pub const PALE_GOLDENROD: KnownColor = KnownColor(0xffeee8aa);
117    pub const PALE_GREEN: KnownColor = KnownColor(0xff98fb98);
118    pub const PALE_TURQUOISE: KnownColor = KnownColor(0xffafeeee);
119    pub const PALE_VIOLET_RED: KnownColor = KnownColor(0xffdb7093);
120    pub const PAPAYA_WHIP: KnownColor = KnownColor(0xffffefd5);
121    pub const PEACH_PUFF: KnownColor = KnownColor(0xffffdab9);
122    pub const PERU: KnownColor = KnownColor(0xffcd853f);
123    pub const PINK: KnownColor = KnownColor(0xffffc0cb);
124    pub const PLUM: KnownColor = KnownColor(0xffdda0dd);
125    pub const POWDER_BLUE: KnownColor = KnownColor(0xffb0e0e6);
126    pub const PURPLE: KnownColor = KnownColor(0xff800080);
127    pub const RED: KnownColor = KnownColor(0xffff0000);
128    pub const ROSY_BROWN: KnownColor = KnownColor(0xffbc8f8f);
129    pub const ROYAL_BLUE: KnownColor = KnownColor(0xff4169e1);
130    pub const SADDLE_BROWN: KnownColor = KnownColor(0xff8b4513);
131    pub const SALMON: KnownColor = KnownColor(0xfffa8072);
132    pub const SANDY_BROWN: KnownColor = KnownColor(0xfff4a460);
133    pub const SEA_GREEN: KnownColor = KnownColor(0xff2e8b57);
134    pub const SEA_SHELL: KnownColor = KnownColor(0xfffff5ee);
135    pub const SIENNA: KnownColor = KnownColor(0xffa0522d);
136    pub const SILVER: KnownColor = KnownColor(0xffc0c0c0);
137    pub const SKY_BLUE: KnownColor = KnownColor(0xff87ceeb);
138    pub const SLATE_BLUE: KnownColor = KnownColor(0xff6a5acd);
139    pub const SLATE_GRAY: KnownColor = KnownColor(0xff708090);
140    pub const SNOW: KnownColor = KnownColor(0xfffffafa);
141    pub const SPRING_GREEN: KnownColor = KnownColor(0xff00ff7f);
142    pub const STEEL_BLUE: KnownColor = KnownColor(0xff4682b4);
143    pub const TAN: KnownColor = KnownColor(0xffd2b48c);
144    pub const TEAL: KnownColor = KnownColor(0xff008080);
145    pub const THISTLE: KnownColor = KnownColor(0xffd8bfd8);
146    pub const TOMATO: KnownColor = KnownColor(0xffff6347);
147    pub const TRANSPARENT: KnownColor = KnownColor(0x00ffffff);
148    pub const TURQUOISE: KnownColor = KnownColor(0xff40e0d0);
149    pub const VIOLET: KnownColor = KnownColor(0xffee82ee);
150    pub const WHEAT: KnownColor = KnownColor(0xfff5deb3);
151    pub const WHITE: KnownColor = KnownColor(0xffffffff);
152    pub const WHITE_SMOKE: KnownColor = KnownColor(0xfff5f5f5);
153    pub const YELLOW: KnownColor = KnownColor(0xffffff00);
154    pub const YELLOW_GREEN: KnownColor = KnownColor(0xff9acd32);
155
156    /// Gets the ARGB value of the known color.
157    #[inline]
158    pub const fn value(self) -> u32 {
159        self.0
160    }
161
162    /// Converts the known color to a [`Color`].
163    #[inline]
164    pub const fn to_color(self) -> Color {
165        Color::from_uint32(self.0)
166    }
167}
168
169/// Lookups between color names and known colors.
170pub struct KnownColors;
171
172impl KnownColors {
173    /// Gets the known color with the given name (ASCII case-insensitive), or
174    /// [`KnownColor::NONE`] when there is no such color.
175    pub fn get_known_color(s: &str) -> KnownColor {
176        match BY_NAME.binary_search_by(|(name, _)| compare_ignore_ascii_case(name, s)) {
177            Ok(index) => BY_NAME[index].1,
178            Err(_) => KnownColor::NONE,
179        }
180    }
181
182    /// Gets the name of the known color with the given ARGB value. When several
183    /// names share the value, the first one in declaration order is returned.
184    pub fn get_known_color_name(rgb: u32) -> Option<&'static str> {
185        match BY_VALUE.binary_search_by_key(&rgb, |(value, _)| *value) {
186            Ok(index) => Some(BY_VALUE[index].1),
187            Err(_) => None,
188        }
189    }
190
191    /// Same as [`get_known_color_name`](Self::get_known_color_name).
192    #[inline]
193    pub fn try_get_known_color_name(rgb: u32) -> Option<&'static str> {
194        Self::get_known_color_name(rgb)
195    }
196
197    /// The known colors in ascending order of their ARGB values, one per
198    /// value: the counterpart of `Enum.GetValues<KnownColor>()` without
199    /// `None` (which the original lists first) and without the second name of
200    /// a shared value, which has the same value and the same name
201    /// ([`get_known_color_name`](Self::get_known_color_name)).
202    pub fn values() -> impl Iterator<Item = KnownColor> {
203        BY_VALUE.iter().map(|(value, _)| KnownColor(*value))
204    }
205
206    /// Converts the known color to a [`Color`].
207    #[inline]
208    pub const fn to_color(color: KnownColor) -> Color {
209        color.to_color()
210    }
211}
212
213/// Orders `name` (already lower-case) relative to `s`, ignoring the ASCII case of `s`.
214fn compare_ignore_ascii_case(name: &str, s: &str) -> std::cmp::Ordering {
215    name.bytes().cmp(s.bytes().map(|b| b.to_ascii_lowercase()))
216}
217
218/// Lower-cased names, sorted, for the case-insensitive name lookup.
219static BY_NAME: [(&str, KnownColor); 141] = [
220    ("aliceblue", KnownColor::ALICE_BLUE),
221    ("antiquewhite", KnownColor::ANTIQUE_WHITE),
222    ("aqua", KnownColor::AQUA),
223    ("aquamarine", KnownColor::AQUAMARINE),
224    ("azure", KnownColor::AZURE),
225    ("beige", KnownColor::BEIGE),
226    ("bisque", KnownColor::BISQUE),
227    ("black", KnownColor::BLACK),
228    ("blanchedalmond", KnownColor::BLANCHED_ALMOND),
229    ("blue", KnownColor::BLUE),
230    ("blueviolet", KnownColor::BLUE_VIOLET),
231    ("brown", KnownColor::BROWN),
232    ("burlywood", KnownColor::BURLY_WOOD),
233    ("cadetblue", KnownColor::CADET_BLUE),
234    ("chartreuse", KnownColor::CHARTREUSE),
235    ("chocolate", KnownColor::CHOCOLATE),
236    ("coral", KnownColor::CORAL),
237    ("cornflowerblue", KnownColor::CORNFLOWER_BLUE),
238    ("cornsilk", KnownColor::CORNSILK),
239    ("crimson", KnownColor::CRIMSON),
240    ("cyan", KnownColor::CYAN),
241    ("darkblue", KnownColor::DARK_BLUE),
242    ("darkcyan", KnownColor::DARK_CYAN),
243    ("darkgoldenrod", KnownColor::DARK_GOLDENROD),
244    ("darkgray", KnownColor::DARK_GRAY),
245    ("darkgreen", KnownColor::DARK_GREEN),
246    ("darkkhaki", KnownColor::DARK_KHAKI),
247    ("darkmagenta", KnownColor::DARK_MAGENTA),
248    ("darkolivegreen", KnownColor::DARK_OLIVE_GREEN),
249    ("darkorange", KnownColor::DARK_ORANGE),
250    ("darkorchid", KnownColor::DARK_ORCHID),
251    ("darkred", KnownColor::DARK_RED),
252    ("darksalmon", KnownColor::DARK_SALMON),
253    ("darkseagreen", KnownColor::DARK_SEA_GREEN),
254    ("darkslateblue", KnownColor::DARK_SLATE_BLUE),
255    ("darkslategray", KnownColor::DARK_SLATE_GRAY),
256    ("darkturquoise", KnownColor::DARK_TURQUOISE),
257    ("darkviolet", KnownColor::DARK_VIOLET),
258    ("deeppink", KnownColor::DEEP_PINK),
259    ("deepskyblue", KnownColor::DEEP_SKY_BLUE),
260    ("dimgray", KnownColor::DIM_GRAY),
261    ("dodgerblue", KnownColor::DODGER_BLUE),
262    ("firebrick", KnownColor::FIREBRICK),
263    ("floralwhite", KnownColor::FLORAL_WHITE),
264    ("forestgreen", KnownColor::FOREST_GREEN),
265    ("fuchsia", KnownColor::FUCHSIA),
266    ("gainsboro", KnownColor::GAINSBORO),
267    ("ghostwhite", KnownColor::GHOST_WHITE),
268    ("gold", KnownColor::GOLD),
269    ("goldenrod", KnownColor::GOLDENROD),
270    ("gray", KnownColor::GRAY),
271    ("green", KnownColor::GREEN),
272    ("greenyellow", KnownColor::GREEN_YELLOW),
273    ("honeydew", KnownColor::HONEYDEW),
274    ("hotpink", KnownColor::HOT_PINK),
275    ("indianred", KnownColor::INDIAN_RED),
276    ("indigo", KnownColor::INDIGO),
277    ("ivory", KnownColor::IVORY),
278    ("khaki", KnownColor::KHAKI),
279    ("lavender", KnownColor::LAVENDER),
280    ("lavenderblush", KnownColor::LAVENDER_BLUSH),
281    ("lawngreen", KnownColor::LAWN_GREEN),
282    ("lemonchiffon", KnownColor::LEMON_CHIFFON),
283    ("lightblue", KnownColor::LIGHT_BLUE),
284    ("lightcoral", KnownColor::LIGHT_CORAL),
285    ("lightcyan", KnownColor::LIGHT_CYAN),
286    ("lightgoldenrodyellow", KnownColor::LIGHT_GOLDENROD_YELLOW),
287    ("lightgray", KnownColor::LIGHT_GRAY),
288    ("lightgreen", KnownColor::LIGHT_GREEN),
289    ("lightpink", KnownColor::LIGHT_PINK),
290    ("lightsalmon", KnownColor::LIGHT_SALMON),
291    ("lightseagreen", KnownColor::LIGHT_SEA_GREEN),
292    ("lightskyblue", KnownColor::LIGHT_SKY_BLUE),
293    ("lightslategray", KnownColor::LIGHT_SLATE_GRAY),
294    ("lightsteelblue", KnownColor::LIGHT_STEEL_BLUE),
295    ("lightyellow", KnownColor::LIGHT_YELLOW),
296    ("lime", KnownColor::LIME),
297    ("limegreen", KnownColor::LIME_GREEN),
298    ("linen", KnownColor::LINEN),
299    ("magenta", KnownColor::MAGENTA),
300    ("maroon", KnownColor::MAROON),
301    ("mediumaquamarine", KnownColor::MEDIUM_AQUAMARINE),
302    ("mediumblue", KnownColor::MEDIUM_BLUE),
303    ("mediumorchid", KnownColor::MEDIUM_ORCHID),
304    ("mediumpurple", KnownColor::MEDIUM_PURPLE),
305    ("mediumseagreen", KnownColor::MEDIUM_SEA_GREEN),
306    ("mediumslateblue", KnownColor::MEDIUM_SLATE_BLUE),
307    ("mediumspringgreen", KnownColor::MEDIUM_SPRING_GREEN),
308    ("mediumturquoise", KnownColor::MEDIUM_TURQUOISE),
309    ("mediumvioletred", KnownColor::MEDIUM_VIOLET_RED),
310    ("midnightblue", KnownColor::MIDNIGHT_BLUE),
311    ("mintcream", KnownColor::MINT_CREAM),
312    ("mistyrose", KnownColor::MISTY_ROSE),
313    ("moccasin", KnownColor::MOCCASIN),
314    ("navajowhite", KnownColor::NAVAJO_WHITE),
315    ("navy", KnownColor::NAVY),
316    ("oldlace", KnownColor::OLD_LACE),
317    ("olive", KnownColor::OLIVE),
318    ("olivedrab", KnownColor::OLIVE_DRAB),
319    ("orange", KnownColor::ORANGE),
320    ("orangered", KnownColor::ORANGE_RED),
321    ("orchid", KnownColor::ORCHID),
322    ("palegoldenrod", KnownColor::PALE_GOLDENROD),
323    ("palegreen", KnownColor::PALE_GREEN),
324    ("paleturquoise", KnownColor::PALE_TURQUOISE),
325    ("palevioletred", KnownColor::PALE_VIOLET_RED),
326    ("papayawhip", KnownColor::PAPAYA_WHIP),
327    ("peachpuff", KnownColor::PEACH_PUFF),
328    ("peru", KnownColor::PERU),
329    ("pink", KnownColor::PINK),
330    ("plum", KnownColor::PLUM),
331    ("powderblue", KnownColor::POWDER_BLUE),
332    ("purple", KnownColor::PURPLE),
333    ("red", KnownColor::RED),
334    ("rosybrown", KnownColor::ROSY_BROWN),
335    ("royalblue", KnownColor::ROYAL_BLUE),
336    ("saddlebrown", KnownColor::SADDLE_BROWN),
337    ("salmon", KnownColor::SALMON),
338    ("sandybrown", KnownColor::SANDY_BROWN),
339    ("seagreen", KnownColor::SEA_GREEN),
340    ("seashell", KnownColor::SEA_SHELL),
341    ("sienna", KnownColor::SIENNA),
342    ("silver", KnownColor::SILVER),
343    ("skyblue", KnownColor::SKY_BLUE),
344    ("slateblue", KnownColor::SLATE_BLUE),
345    ("slategray", KnownColor::SLATE_GRAY),
346    ("snow", KnownColor::SNOW),
347    ("springgreen", KnownColor::SPRING_GREEN),
348    ("steelblue", KnownColor::STEEL_BLUE),
349    ("tan", KnownColor::TAN),
350    ("teal", KnownColor::TEAL),
351    ("thistle", KnownColor::THISTLE),
352    ("tomato", KnownColor::TOMATO),
353    ("transparent", KnownColor::TRANSPARENT),
354    ("turquoise", KnownColor::TURQUOISE),
355    ("violet", KnownColor::VIOLET),
356    ("wheat", KnownColor::WHEAT),
357    ("white", KnownColor::WHITE),
358    ("whitesmoke", KnownColor::WHITE_SMOKE),
359    ("yellow", KnownColor::YELLOW),
360    ("yellowgreen", KnownColor::YELLOW_GREEN),
361];
362
363/// Display names sorted by ARGB value; the first declared name wins for shared values.
364static BY_VALUE: [(u32, &str); 139] = [
365    (0x00ffffff, "Transparent"),
366    (0xff000000, "Black"),
367    (0xff000080, "Navy"),
368    (0xff00008b, "DarkBlue"),
369    (0xff0000cd, "MediumBlue"),
370    (0xff0000ff, "Blue"),
371    (0xff006400, "DarkGreen"),
372    (0xff008000, "Green"),
373    (0xff008080, "Teal"),
374    (0xff008b8b, "DarkCyan"),
375    (0xff00bfff, "DeepSkyBlue"),
376    (0xff00ced1, "DarkTurquoise"),
377    (0xff00fa9a, "MediumSpringGreen"),
378    (0xff00ff00, "Lime"),
379    (0xff00ff7f, "SpringGreen"),
380    (0xff00ffff, "Aqua"),
381    (0xff191970, "MidnightBlue"),
382    (0xff1e90ff, "DodgerBlue"),
383    (0xff20b2aa, "LightSeaGreen"),
384    (0xff228b22, "ForestGreen"),
385    (0xff2e8b57, "SeaGreen"),
386    (0xff2f4f4f, "DarkSlateGray"),
387    (0xff32cd32, "LimeGreen"),
388    (0xff3cb371, "MediumSeaGreen"),
389    (0xff40e0d0, "Turquoise"),
390    (0xff4169e1, "RoyalBlue"),
391    (0xff4682b4, "SteelBlue"),
392    (0xff483d8b, "DarkSlateBlue"),
393    (0xff48d1cc, "MediumTurquoise"),
394    (0xff4b0082, "Indigo"),
395    (0xff556b2f, "DarkOliveGreen"),
396    (0xff5f9ea0, "CadetBlue"),
397    (0xff6495ed, "CornflowerBlue"),
398    (0xff66cdaa, "MediumAquamarine"),
399    (0xff696969, "DimGray"),
400    (0xff6a5acd, "SlateBlue"),
401    (0xff6b8e23, "OliveDrab"),
402    (0xff708090, "SlateGray"),
403    (0xff778899, "LightSlateGray"),
404    (0xff7b68ee, "MediumSlateBlue"),
405    (0xff7cfc00, "LawnGreen"),
406    (0xff7fff00, "Chartreuse"),
407    (0xff7fffd4, "Aquamarine"),
408    (0xff800000, "Maroon"),
409    (0xff800080, "Purple"),
410    (0xff808000, "Olive"),
411    (0xff808080, "Gray"),
412    (0xff87ceeb, "SkyBlue"),
413    (0xff87cefa, "LightSkyBlue"),
414    (0xff8a2be2, "BlueViolet"),
415    (0xff8b0000, "DarkRed"),
416    (0xff8b008b, "DarkMagenta"),
417    (0xff8b4513, "SaddleBrown"),
418    (0xff8fbc8f, "DarkSeaGreen"),
419    (0xff90ee90, "LightGreen"),
420    (0xff9370db, "MediumPurple"),
421    (0xff9400d3, "DarkViolet"),
422    (0xff98fb98, "PaleGreen"),
423    (0xff9932cc, "DarkOrchid"),
424    (0xff9acd32, "YellowGreen"),
425    (0xffa0522d, "Sienna"),
426    (0xffa52a2a, "Brown"),
427    (0xffa9a9a9, "DarkGray"),
428    (0xffadd8e6, "LightBlue"),
429    (0xffadff2f, "GreenYellow"),
430    (0xffafeeee, "PaleTurquoise"),
431    (0xffb0c4de, "LightSteelBlue"),
432    (0xffb0e0e6, "PowderBlue"),
433    (0xffb22222, "Firebrick"),
434    (0xffb8860b, "DarkGoldenrod"),
435    (0xffba55d3, "MediumOrchid"),
436    (0xffbc8f8f, "RosyBrown"),
437    (0xffbdb76b, "DarkKhaki"),
438    (0xffc0c0c0, "Silver"),
439    (0xffc71585, "MediumVioletRed"),
440    (0xffcd5c5c, "IndianRed"),
441    (0xffcd853f, "Peru"),
442    (0xffd2691e, "Chocolate"),
443    (0xffd2b48c, "Tan"),
444    (0xffd3d3d3, "LightGray"),
445    (0xffd8bfd8, "Thistle"),
446    (0xffda70d6, "Orchid"),
447    (0xffdaa520, "Goldenrod"),
448    (0xffdb7093, "PaleVioletRed"),
449    (0xffdc143c, "Crimson"),
450    (0xffdcdcdc, "Gainsboro"),
451    (0xffdda0dd, "Plum"),
452    (0xffdeb887, "BurlyWood"),
453    (0xffe0ffff, "LightCyan"),
454    (0xffe6e6fa, "Lavender"),
455    (0xffe9967a, "DarkSalmon"),
456    (0xffee82ee, "Violet"),
457    (0xffeee8aa, "PaleGoldenrod"),
458    (0xfff08080, "LightCoral"),
459    (0xfff0e68c, "Khaki"),
460    (0xfff0f8ff, "AliceBlue"),
461    (0xfff0fff0, "Honeydew"),
462    (0xfff0ffff, "Azure"),
463    (0xfff4a460, "SandyBrown"),
464    (0xfff5deb3, "Wheat"),
465    (0xfff5f5dc, "Beige"),
466    (0xfff5f5f5, "WhiteSmoke"),
467    (0xfff5fffa, "MintCream"),
468    (0xfff8f8ff, "GhostWhite"),
469    (0xfffa8072, "Salmon"),
470    (0xfffaebd7, "AntiqueWhite"),
471    (0xfffaf0e6, "Linen"),
472    (0xfffafad2, "LightGoldenrodYellow"),
473    (0xfffdf5e6, "OldLace"),
474    (0xffff0000, "Red"),
475    (0xffff00ff, "Fuchsia"),
476    (0xffff1493, "DeepPink"),
477    (0xffff4500, "OrangeRed"),
478    (0xffff6347, "Tomato"),
479    (0xffff69b4, "HotPink"),
480    (0xffff7f50, "Coral"),
481    (0xffff8c00, "DarkOrange"),
482    (0xffffa07a, "LightSalmon"),
483    (0xffffa500, "Orange"),
484    (0xffffb6c1, "LightPink"),
485    (0xffffc0cb, "Pink"),
486    (0xffffd700, "Gold"),
487    (0xffffdab9, "PeachPuff"),
488    (0xffffdead, "NavajoWhite"),
489    (0xffffe4b5, "Moccasin"),
490    (0xffffe4c4, "Bisque"),
491    (0xffffe4e1, "MistyRose"),
492    (0xffffebcd, "BlanchedAlmond"),
493    (0xffffefd5, "PapayaWhip"),
494    (0xfffff0f5, "LavenderBlush"),
495    (0xfffff5ee, "SeaShell"),
496    (0xfffff8dc, "Cornsilk"),
497    (0xfffffacd, "LemonChiffon"),
498    (0xfffffaf0, "FloralWhite"),
499    (0xfffffafa, "Snow"),
500    (0xffffff00, "Yellow"),
501    (0xffffffe0, "LightYellow"),
502    (0xfffffff0, "Ivory"),
503    (0xffffffff, "White"),
504];
505
506#[cfg(test)]
507mod tests {
508    // The reference test-suite has no dedicated tests for this type.
509    use super::*;
510
511    #[test]
512    fn tables_are_sorted_for_binary_search() {
513        assert!(BY_NAME.windows(2).all(|w| w[0].0 < w[1].0));
514        assert!(BY_VALUE.windows(2).all(|w| w[0].0 < w[1].0));
515        assert!(BY_NAME
516            .iter()
517            .all(|(name, _)| name.bytes().all(|b| b.is_ascii_lowercase())));
518    }
519
520    #[test]
521    fn name_lookup_is_case_insensitive() {
522        assert_eq!(KnownColors::get_known_color("red"), KnownColor::RED);
523        assert_eq!(KnownColors::get_known_color("RED"), KnownColor::RED);
524        assert_eq!(
525            KnownColors::get_known_color("AliceBlue"),
526            KnownColor::ALICE_BLUE
527        );
528        assert_eq!(
529            KnownColors::get_known_color("yellowgreen"),
530            KnownColor::YELLOW_GREEN
531        );
532        assert_eq!(
533            KnownColors::get_known_color("Transparent").value(),
534            0x00ffffff
535        );
536        assert_eq!(KnownColors::get_known_color("None"), KnownColor::NONE);
537        assert_eq!(KnownColors::get_known_color(""), KnownColor::NONE);
538        assert_eq!(KnownColors::get_known_color("reds"), KnownColor::NONE);
539        assert_eq!(KnownColors::get_known_color("re"), KnownColor::NONE);
540    }
541
542    #[test]
543    fn every_name_round_trips() {
544        for (name, color) in BY_NAME.iter() {
545            assert_eq!(KnownColors::get_known_color(name), *color);
546            assert_eq!(
547                KnownColors::get_known_color(&name.to_ascii_uppercase()),
548                *color
549            );
550            let display = KnownColors::get_known_color_name(color.value()).unwrap();
551            assert_eq!(KnownColors::get_known_color(display), *color);
552        }
553    }
554
555    #[test]
556    fn shared_values_use_first_declared_name() {
557        assert_eq!(KnownColor::AQUA, KnownColor::CYAN);
558        assert_eq!(KnownColor::FUCHSIA, KnownColor::MAGENTA);
559        assert_eq!(KnownColors::get_known_color_name(0xff00ffff), Some("Aqua"));
560        assert_eq!(
561            KnownColors::get_known_color_name(0xffff00ff),
562            Some("Fuchsia")
563        );
564        assert_eq!(KnownColors::get_known_color_name(0xffff0000), Some("Red"));
565        assert_eq!(
566            KnownColors::get_known_color_name(0x00ffffff),
567            Some("Transparent")
568        );
569        assert_eq!(KnownColors::get_known_color_name(0), None);
570        assert_eq!(KnownColors::get_known_color_name(0xff123456), None);
571    }
572}