Debian Codegraph @

tests/named_colors.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 use csscolorparser::{parse, Color, NAMED_COLORS};
2
3 #[test]
4 fn named_colors() {
5 let skip_list = ["aqua", "cyan", "fuchsia", "magenta"];
6
7 for (&name, &rgb) in NAMED_COLORS.entries() {
•8 let c = parse(name).unwrap();
9 assert_eq!(c.to_rgba8()[0..3], rgb);
10
•11 if skip_list.contains(&name)
12 || name.contains("gray")
13 || name.contains("grey")
14 {
15 continue;
16 }
•17 assert_eq!(c.name(), Some(name));
18
19 let [r, g, b] = rgb;
20 let c = Color::from_rgba8(r, g, b, 255);
•21 assert_eq!(c.name(), Some(name));
22 }
23
24 // Case-insensitive tests
25
26 macro_rules! cmp {
27 ($a:expr, $b:expr) => {
28 assert_eq!(parse($a).unwrap().to_rgba8(), parse($b).unwrap().to_rgba8());
29 };
30 }
31
32 cmp!("red", "RED");
33 cmp!("red", "Red");
34 cmp!("skyblue", "SKYBLUE");
35 cmp!("skyblue", "SkyBlue");
36
37 // Hex
38
39 #[rustfmt::skip]
40 let test_data = [
41 ("aliceblue", "#f0f8ff"),
42 ("bisque", "#ffe4c4"),
43 ("black", "#000000"),
44 ("chartreuse", "#7fff00"),
45 ("coral", "#ff7f50"),
46 ("crimson", "#dc143c"),
47 ("dodgerblue", "#1e90ff"),
48 ("firebrick", "#b22222"),
49 ("gold", "#ffd700"),
50 ("hotpink", "#ff69b4"),
51 ("indigo", "#4b0082"),
52 ("lavender", "#e6e6fa"),
53 ("lime", "#00ff00"),
54 ("plum", "#dda0dd"),
55 ("red", "#ff0000"),
56 ("salmon", "#fa8072"),
57 ("skyblue", "#87ceeb"),
58 ("tomato", "#ff6347"),
59 ("violet", "#ee82ee"),
60 ("yellowgreen", "#9acd32"),
61 ];
62
63 for (name, hex) in test_data {
64 let c = csscolorparser::parse(name).unwrap();
65 assert_eq!(c.to_css_hex(), hex);
66
67 let c = csscolorparser::parse(hex).unwrap();
68 assert_eq!(c.name(), Some(name));
69 }
70
71 // Colors without names
72
73 let test_data = [
74 Color::new(0.7, 0.8, 0.9, 1.0),
75 Color::new(1.0, 0.5, 0.0, 1.0),
76 Color::from_rgba8(0, 50, 100, 255),
77 ];
78 for c in test_data {
79 assert!(c.name().is_none());
80 }
81 }