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