1 /*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/utils/SkParse.h"
9 #include "tests/Test.h"
10
11 static constexpr struct {
12 const char* fName;
13 SkColor fColor;
14 } gNamedColors[] = {
15 { "aliceblue", 0xfff0f8ff },
16 { "antiquewhite", 0xfffaebd7 },
17 { "aqua", 0xff00ffff },
18 { "aquamarine", 0xff7fffd4 },
19 { "azure", 0xfff0ffff },
20 { "beige", 0xfff5f5dc },
21 { "bisque", 0xffffe4c4 },
22 { "black", 0xff000000 },
23 { "blanchedalmond", 0xffffebcd },
24 { "blue", 0xff0000ff },
25 { "blueviolet", 0xff8a2be2 },
26 { "brown", 0xffa52a2a },
27 { "burlywood", 0xffdeb887 },
28 { "cadetblue", 0xff5f9ea0 },
29 { "chartreuse", 0xff7fff00 },
30 { "chocolate", 0xffd2691e },
31 { "coral", 0xffff7f50 },
32 { "cornflowerblue", 0xff6495ed },
33 { "cornsilk", 0xfffff8dc },
34 { "crimson", 0xffdc143c },
35 { "cyan", 0xff00ffff },
36 { "darkblue", 0xff00008b },
37 { "darkcyan", 0xff008b8b },
38 { "darkgoldenrod", 0xffb8860b },
39 { "darkgray", 0xffa9a9a9 },
40 { "darkgreen", 0xff006400 },
41 { "darkkhaki", 0xffbdb76b },
42 { "darkmagenta", 0xff8b008b },
43 { "darkolivegreen", 0xff556b2f },
44 { "darkorange", 0xffff8c00 },
45 { "darkorchid", 0xff9932cc },
46 { "darkred", 0xff8b0000 },
47 { "darksalmon", 0xffe9967a },
48 { "darkseagreen", 0xff8fbc8f },
49 { "darkslateblue", 0xff483d8b },
50 { "darkslategray", 0xff2f4f4f },
51 { "darkturquoise", 0xff00ced1 },
52 { "darkviolet", 0xff9400d3 },
53 { "deeppink", 0xffff1493 },
54 { "deepskyblue", 0xff00bfff },
55 { "dimgray", 0xff696969 },
56 { "dodgerblue", 0xff1e90ff },
57 { "firebrick", 0xffb22222 },
58 { "floralwhite", 0xfffffaf0 },
59 { "forestgreen", 0xff228b22 },
60 { "fuchsia", 0xffff00ff },
61 { "gainsboro", 0xffdcdcdc },
62 { "ghostwhite", 0xfff8f8ff },
63 { "gold", 0xffffd700 },
64 { "goldenrod", 0xffdaa520 },
65 { "gray", 0xff808080 },
66 { "green", 0xff008000 },
67 { "greenyellow", 0xffadff2f },
68 { "honeydew", 0xfff0fff0 },
69 { "hotpink", 0xffff69b4 },
70 { "indianred", 0xffcd5c5c },
71 { "indigo", 0xff4b0082 },
72 { "ivory", 0xfffffff0 },
73 { "khaki", 0xfff0e68c },
74 { "lavender", 0xffe6e6fa },
75 { "lavenderblush", 0xfffff0f5 },
76 { "lawngreen", 0xff7cfc00 },
77 { "lemonchiffon", 0xfffffacd },
78 { "lightblue", 0xffadd8e6 },
79 { "lightcoral", 0xfff08080 },
80 { "lightcyan", 0xffe0ffff },
81 { "lightgoldenrodyellow", 0xfffafad2 },
82 { "lightgreen", 0xff90ee90 },
83 { "lightgrey", 0xffd3d3d3 },
84 { "lightpink", 0xffffb6c1 },
85 { "lightsalmon", 0xffffa07a },
86 { "lightseagreen", 0xff20b2aa },
87 { "lightskyblue", 0xff87cefa },
88 { "lightslategray", 0xff778899 },
89 { "lightsteelblue", 0xffb0c4de },
90 { "lightyellow", 0xffffffe0 },
91 { "lime", 0xff00ff00 },
92 { "limegreen", 0xff32cd32 },
93 { "linen", 0xfffaf0e6 },
94 { "magenta", 0xffff00ff },
95 { "maroon", 0xff800000 },
96 { "mediumaquamarine", 0xff66cdaa },
97 { "mediumblue", 0xff0000cd },
98 { "mediumorchid", 0xffba55d3 },
99 { "mediumpurple", 0xff9370db },
100 { "mediumseagreen", 0xff3cb371 },
101 { "mediumslateblue", 0xff7b68ee },
102 { "mediumspringgreen", 0xff00fa9a },
103 { "mediumturquoise", 0xff48d1cc },
104 { "mediumvioletred", 0xffc71585 },
105 { "midnightblue", 0xff191970 },
106 { "mintcream", 0xfff5fffa },
107 { "mistyrose", 0xffffe4e1 },
108 { "moccasin", 0xffffe4b5 },
109 { "navajowhite", 0xffffdead },
110 { "navy", 0xff000080 },
111 { "oldlace", 0xfffdf5e6 },
112 { "olive", 0xff808000 },
113 { "olivedrab", 0xff6b8e23 },
114 { "orange", 0xffffa500 },
115 { "orangered", 0xffff4500 },
116 { "orchid", 0xffda70d6 },
117 { "palegoldenrod", 0xffeee8aa },
118 { "palegreen", 0xff98fb98 },
119 { "paleturquoise", 0xffafeeee },
120 { "palevioletred", 0xffdb7093 },
121 { "papayawhip", 0xffffefd5 },
122 { "peachpuff", 0xffffdab9 },
123 { "peru", 0xffcd853f },
124 { "pink", 0xffffc0cb },
125 { "plum", 0xffdda0dd },
126 { "powderblue", 0xffb0e0e6 },
127 { "purple", 0xff800080 },
128 { "red", 0xffff0000 },
129 { "rosybrown", 0xffbc8f8f },
130 { "royalblue", 0xff4169e1 },
131 { "saddlebrown", 0xff8b4513 },
132 { "salmon", 0xfffa8072 },
133 { "sandybrown", 0xfff4a460 },
134 { "seagreen", 0xff2e8b57 },
135 { "seashell", 0xfffff5ee },
136 { "sienna", 0xffa0522d },
137 { "silver", 0xffc0c0c0 },
138 { "skyblue", 0xff87ceeb },
139 { "slateblue", 0xff6a5acd },
140 { "slategray", 0xff708090 },
141 { "snow", 0xfffffafa },
142 { "springgreen", 0xff00ff7f },
143 { "steelblue", 0xff4682b4 },
144 { "tan", 0xffd2b48c },
145 { "teal", 0xff008080 },
146 { "thistle", 0xffd8bfd8 },
147 { "tomato", 0xffff6347 },
148 { "turquoise", 0xff40e0d0 },
149 { "violet", 0xffee82ee },
150 { "wheat", 0xfff5deb3 },
151 { "white", 0xffffffff },
152 { "whitesmoke", 0xfff5f5f5 },
153 { "yellow", 0xffffff00 },
154 { "yellowgreen", 0xff9acd32 },
155 };
156
is_valid_name(const SkString & name)157 static bool is_valid_name(const SkString& name) {
158 for (const auto& c : gNamedColors) {
159 if (name.equals(c.fName)) return true;
160 }
161 return false;
162 }
163
DEF_TEST(ParseNamedColor,reporter)164 DEF_TEST(ParseNamedColor, reporter) {
165 SkColor color;
166
167 for (const auto& c : gNamedColors) {
168 // check the real name
169 REPORTER_ASSERT(reporter, SkParse::FindNamedColor(c.fName , strlen(c.fName), &color)
170 == c.fName + strlen(c.fName));
171 REPORTER_ASSERT(reporter, color == c.fColor);
172
173 // check partial prefixes
174 for (int l = strlen(c.fName) - 1; l >= 0; --l) {
175 SkString s(c.fName, l);
176 // some substrings are valid color names
177 if (is_valid_name(s)) continue;
178
179 REPORTER_ASSERT(reporter, SkParse::FindNamedColor(s.c_str(), l, &color) == nullptr);
180 }
181
182 // check suffixes
183 SkString s(c.fName, strlen(c.fName));
184 s.append("A");
185 REPORTER_ASSERT(reporter, SkParse::FindNamedColor(s.c_str(), s.size(), &color) == nullptr);
186 }
187
188 // some oddballs
189 REPORTER_ASSERT(reporter, SkParse::FindNamedColor("" , 0, &color) == nullptr);
190 REPORTER_ASSERT(reporter, SkParse::FindNamedColor("aaa", 3, &color) == nullptr);
191 REPORTER_ASSERT(reporter, SkParse::FindNamedColor("zzz", 3, &color) == nullptr);
192 REPORTER_ASSERT(reporter, SkParse::FindNamedColor("aaaaaaaaaaaa", 12, &color) == nullptr);
193 REPORTER_ASSERT(reporter, SkParse::FindNamedColor("zzzzzzzzzzzz", 12, &color) == nullptr);
194 }
195