Flutter Engine
 
Loading...
Searching...
No Matches
dl_color_unittests.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
8
9namespace flutter {
10namespace testing {
11
12static void arraysEqual(const uint32_t* ints,
13 const DlColor* colors,
14 int count) {
15 for (int i = 0; i < count; i++) {
16 EXPECT_EQ(ints[i], colors[i].argb()) << " index:" << i;
17 }
18}
19
20TEST(DisplayListColor, DefaultValue) {
21 DlColor color;
22 EXPECT_EQ(color.getAlphaF(), 1.f);
23 EXPECT_EQ(color.getRedF(), 0.f);
24 EXPECT_EQ(color.getGreenF(), 0.f);
25 EXPECT_EQ(color.getBlueF(), 0.f);
26 EXPECT_EQ(color.getColorSpace(), DlColorSpace::kSRGB);
27}
28
29TEST(DisplayListColor, ArrayInterchangeableWithUint32) {
30 uint32_t ints[5] = {
31 0xFF000000, //
32 0xFFFF0000, //
33 0xFF00FF00, //
34 0xFF0000FF, //
35 0xF1F2F3F4,
36 };
37 DlColor colors[5] = {
38 DlColor::kBlack(), //
39 DlColor::kRed(), //
40 DlColor::kGreen(), //
41 DlColor::kBlue(), //
42 DlColor(0xF1F2F3F4),
43 };
44 arraysEqual(ints, colors, 5);
45}
46
47TEST(DisplayListColor, DlColorFloatConstructor) {
48 EXPECT_EQ(DlColor::ARGB(1.0f, 1.0f, 1.0f, 1.0f), DlColor(0xFFFFFFFF));
49 EXPECT_EQ(DlColor::ARGB(0.0f, 0.0f, 0.0f, 0.0f), DlColor(0x00000000));
50 EXPECT_TRUE(
51 DlColor::ARGB(0.5f, 0.5f, 0.5f, 0.5f).isClose(DlColor(0x80808080)));
52 EXPECT_TRUE(
53 DlColor::ARGB(1.0f, 0.0f, 0.5f, 1.0f).isClose(DlColor(0xFF0080FF)));
54
55 EXPECT_EQ(DlColor::RGBA(1.0f, 1.0f, 1.0f, 1.0f), DlColor(0xFFFFFFFF));
56 EXPECT_EQ(DlColor::RGBA(0.0f, 0.0f, 0.0f, 0.0f), DlColor(0x00000000));
57 EXPECT_TRUE(
58 DlColor::RGBA(0.5f, 0.5f, 0.5f, 0.5f).isClose(DlColor(0x80808080)));
59 EXPECT_TRUE(
60 DlColor::RGBA(1.0f, 0.0f, 0.5f, 1.0f).isClose(DlColor(0xFFFF0080)));
61}
62
63TEST(DisplayListColor, DlColorComponentGetters) {
64 {
65 DlColor test(0xFFFFFFFF);
66
67 EXPECT_EQ(test.getAlpha(), 0xFF);
68 EXPECT_EQ(test.getRed(), 0xFF);
69 EXPECT_EQ(test.getGreen(), 0xFF);
70 EXPECT_EQ(test.getBlue(), 0xFF);
71
72 EXPECT_EQ(test.getAlphaF(), 1.0f);
73 EXPECT_EQ(test.getRedF(), 1.0f);
74 EXPECT_EQ(test.getGreenF(), 1.0f);
75 EXPECT_EQ(test.getBlueF(), 1.0f);
76 }
77
78 {
79 DlColor test = DlColor::ARGB(1.0f, 1.0f, 1.0f, 1.0f);
80
81 EXPECT_EQ(test.getAlpha(), 0xFF);
82 EXPECT_EQ(test.getRed(), 0xFF);
83 EXPECT_EQ(test.getGreen(), 0xFF);
84 EXPECT_EQ(test.getBlue(), 0xFF);
85
86 EXPECT_EQ(test.getAlphaF(), 1.0f);
87 EXPECT_EQ(test.getRedF(), 1.0f);
88 EXPECT_EQ(test.getGreenF(), 1.0f);
89 EXPECT_EQ(test.getBlueF(), 1.0f);
90 }
91
92 {
93 DlColor test(0x00000000);
94
95 EXPECT_EQ(test.getAlpha(), 0x00);
96 EXPECT_EQ(test.getRed(), 0x00);
97 EXPECT_EQ(test.getGreen(), 0x00);
98 EXPECT_EQ(test.getBlue(), 0x00);
99
100 EXPECT_EQ(test.getAlphaF(), 0.0f);
101 EXPECT_EQ(test.getRedF(), 0.0f);
102 EXPECT_EQ(test.getGreenF(), 0.0f);
103 EXPECT_EQ(test.getBlueF(), 0.0f);
104 }
105
106 {
107 DlColor test = DlColor::ARGB(0.0f, 0.0f, 0.0f, 0.0f);
108
109 EXPECT_EQ(test.getAlpha(), 0x00);
110 EXPECT_EQ(test.getRed(), 0x00);
111 EXPECT_EQ(test.getGreen(), 0x00);
112 EXPECT_EQ(test.getBlue(), 0x00);
113
114 EXPECT_EQ(test.getAlphaF(), 0.0f);
115 EXPECT_EQ(test.getRedF(), 0.0f);
116 EXPECT_EQ(test.getGreenF(), 0.0f);
117 EXPECT_EQ(test.getBlueF(), 0.0f);
118 }
119
120 {
121 DlColor test(0x7F7F7F7F);
122
123 EXPECT_EQ(test.getAlpha(), 0x7F);
124 EXPECT_EQ(test.getRed(), 0x7F);
125 EXPECT_EQ(test.getGreen(), 0x7F);
126 EXPECT_EQ(test.getBlue(), 0x7F);
127
128 const DlScalar half = 127.0f * (1.0f / 255.0f);
129
130 EXPECT_NEAR(test.getAlphaF(), half, 0.00001);
131 EXPECT_NEAR(test.getRedF(), half, 0.00001);
132 EXPECT_NEAR(test.getGreenF(), half, 0.00001);
133 EXPECT_NEAR(test.getBlueF(), half, 0.00001);
134 }
135
136 {
137 DlColor test = DlColor::ARGB(0.5f, 0.5f, 0.5f, 0.5f);
138
139 EXPECT_EQ(test.getAlpha(), 0x80);
140 EXPECT_EQ(test.getRed(), 0x80);
141 EXPECT_EQ(test.getGreen(), 0x80);
142 EXPECT_EQ(test.getBlue(), 0x80);
143
144 EXPECT_EQ(test.getAlphaF(), 0.5);
145 EXPECT_EQ(test.getRedF(), 0.5);
146 EXPECT_EQ(test.getGreenF(), 0.5);
147 EXPECT_EQ(test.getBlueF(), 0.5);
148 }
149
150 {
151 DlColor test(0x1F2F3F4F);
152
153 EXPECT_EQ(test.getAlpha(), 0x1F);
154 EXPECT_EQ(test.getRed(), 0x2F);
155 EXPECT_EQ(test.getGreen(), 0x3F);
156 EXPECT_EQ(test.getBlue(), 0x4F);
157
158 EXPECT_NEAR(test.getAlphaF(), 0x1f * (1.0f / 255.0f), 0.00001);
159 EXPECT_NEAR(test.getRedF(), 0x2f * (1.0f / 255.0f), 0.00001);
160 EXPECT_NEAR(test.getGreenF(), 0x3f * (1.0f / 255.0f), 0.00001);
161 EXPECT_NEAR(test.getBlueF(), 0x4f * (1.0f / 255.0f), 0.00001);
162 }
163
164 {
165 DlColor test = DlColor::ARGB(0.1f, 0.2f, 0.3f, 0.4f);
166
167 EXPECT_EQ(test.getAlpha(), round(0.1f * 255));
168 EXPECT_EQ(test.getRed(), round(0.2f * 255));
169 EXPECT_EQ(test.getGreen(), round(0.3f * 255));
170 EXPECT_EQ(test.getBlue(), round(0.4f * 255));
171
172 EXPECT_EQ(test.getAlphaF(), 0.1f);
173 EXPECT_EQ(test.getRedF(), 0.2f);
174 EXPECT_EQ(test.getGreenF(), 0.3f);
175 EXPECT_EQ(test.getBlueF(), 0.4f);
176 }
177
178 {
179 DlColor test = DlColor::RGBA(0.2f, 0.3f, 0.4f, 0.1f);
180
181 EXPECT_EQ(test.getAlpha(), round(0.1f * 255));
182 EXPECT_EQ(test.getRed(), round(0.2f * 255));
183 EXPECT_EQ(test.getGreen(), round(0.3f * 255));
184 EXPECT_EQ(test.getBlue(), round(0.4f * 255));
185
186 EXPECT_EQ(test.getAlphaF(), 0.1f);
187 EXPECT_EQ(test.getRedF(), 0.2f);
188 EXPECT_EQ(test.getGreenF(), 0.3f);
189 EXPECT_EQ(test.getBlueF(), 0.4f);
190 }
191}
192
193TEST(DisplayListColor, DlColorOpaqueTransparent) {
194 auto test_argb = [](int a, int r, int g, int b) {
195 ASSERT_TRUE(a >= 0 && a <= 255);
196 ASSERT_TRUE(r >= 0 && r <= 255);
197 ASSERT_TRUE(g >= 0 && g <= 255);
198 ASSERT_TRUE(b >= 0 && b <= 255);
199
200 int argb = ((a << 24) | (r << 16) | (g << 8) | b);
201 bool is_opaque = (a == 255);
202 bool is_transprent = (a == 0);
203
204 EXPECT_EQ(DlColor(argb).isOpaque(), is_opaque);
205 EXPECT_EQ(DlColor(argb).isTransparent(), is_transprent);
206
207 DlScalar aF = a * (1.0f / 255.0f);
208 DlScalar rF = r * (1.0f / 255.0f);
209 DlScalar gF = g * (1.0f / 255.0f);
210 DlScalar bF = b * (1.0f / 255.0f);
211
212 EXPECT_EQ(DlColor::ARGB(aF, rF, gF, bF).isOpaque(), is_opaque);
213 EXPECT_EQ(DlColor::ARGB(aF, rF, gF, bF).isTransparent(), is_transprent);
214
215 EXPECT_EQ(DlColor::RGBA(rF, gF, bF, aF).isOpaque(), is_opaque);
216 EXPECT_EQ(DlColor::RGBA(rF, gF, bF, aF).isTransparent(), is_transprent);
217 };
218
219 for (int r = 0; r <= 255; r += 15) {
220 for (int g = 0; g <= 255; g += 15) {
221 for (int b = 0; b <= 255; b += 15) {
222 test_argb(0, r, g, b);
223 for (int a = 15; a < 255; a += 15) {
224 test_argb(a, r, g, b);
225 }
226 test_argb(255, r, g, b);
227 }
228 }
229 }
230}
231
232TEST(DisplayListColor, EqualityWithColorspace) {
233 EXPECT_TRUE(DlColor(0.9, 0.8, 0.7, 0.6, DlColorSpace::kSRGB) ==
234 DlColor(0.9, 0.8, 0.7, 0.6, DlColorSpace::kSRGB));
235 EXPECT_FALSE(DlColor(0.9, 0.8, 0.7, 0.6, DlColorSpace::kSRGB) ==
236 DlColor(0.9, 0.8, 0.7, 0.6, DlColorSpace::kExtendedSRGB));
237 EXPECT_FALSE(DlColor(0.9, 0.8, 0.7, 0.6, DlColorSpace::kSRGB) !=
238 DlColor(0.9, 0.8, 0.7, 0.6, DlColorSpace::kSRGB));
239 EXPECT_TRUE(DlColor(0.9, 0.8, 0.7, 0.6, DlColorSpace::kSRGB) !=
240 DlColor(0.9, 0.8, 0.7, 0.6, DlColorSpace::kExtendedSRGB));
241}
242
243TEST(DisplayListColor, EqualityWithExtendedSRGB) {
244 EXPECT_TRUE(DlColor(1.0, 1.1, -0.2, 0.1, DlColorSpace::kExtendedSRGB) ==
245 DlColor(1.0, 1.1, -0.2, 0.1, DlColorSpace::kExtendedSRGB));
246 EXPECT_FALSE(DlColor(1.0, 1.1, -0.2, 0.1, DlColorSpace::kExtendedSRGB) ==
247 DlColor(1.0, 1.0, 0.0, 0.0, DlColorSpace::kExtendedSRGB));
248}
249
250TEST(DisplayListColor, ColorSpaceSRGBtoSRGB) {
251 DlColor srgb(0.9, 0.8, 0.7, 0.6, DlColorSpace::kSRGB);
252 EXPECT_EQ(srgb, srgb.withColorSpace(DlColorSpace::kSRGB));
253}
254
255TEST(DisplayListColor, ColorSpaceSRGBtoExtendedSRGB) {
256 DlColor srgb(0.9, 0.8, 0.7, 0.6, DlColorSpace::kSRGB);
257 EXPECT_EQ(DlColor(0.9, 0.8, 0.7, 0.6, DlColorSpace::kExtendedSRGB),
259}
260
261TEST(DisplayListColor, ColorSpaceExtendedSRGBtoExtendedSRGB) {
262 DlColor xsrgb(0.9, 0.8, 0.7, 0.6, DlColorSpace::kExtendedSRGB);
263 EXPECT_EQ(DlColor(0.9, 0.8, 0.7, 0.6, DlColorSpace::kExtendedSRGB),
265}
266
267TEST(DisplayListColor, ColorSpaceExtendedSRGBtoSRGB) {
268 DlColor xsrgb1(0.9, 0.8, 0.7, 0.6, DlColorSpace::kExtendedSRGB);
269 EXPECT_EQ(DlColor(0.9, 0.8, 0.7, 0.6, DlColorSpace::kSRGB),
271
272 DlColor xsrgb2(0.9, 1.1, -0.1, 0.6, DlColorSpace::kExtendedSRGB);
273 EXPECT_EQ(DlColor(0.9, 1.0, 0.0, 0.6, DlColorSpace::kSRGB),
275}
276
277TEST(DisplayListColor, ColorSpaceP3ToP3) {
278 DlColor p3(0.9, 0.8, 0.7, 0.6, DlColorSpace::kDisplayP3);
279 EXPECT_EQ(DlColor(0.9, 0.8, 0.7, 0.6, DlColorSpace::kDisplayP3),
281}
282
283TEST(DisplayListColor, ColorSpaceP3ToExtendedSRGB) {
284 DlColor red(0.9, 1.0, 0.0, 0.0, DlColorSpace::kDisplayP3);
285 EXPECT_TRUE(
286 DlColor(0.9, 1.0931, -0.2268, -0.1501, DlColorSpace::kExtendedSRGB)
289
290 DlColor green(0.9, 0.0, 1.0, 0.0, DlColorSpace::kDisplayP3);
291 EXPECT_TRUE(
292 DlColor(0.9, -0.5116, 1.0183, -0.3106, DlColorSpace::kExtendedSRGB)
295
296 DlColor blue(0.9, 0.0, 0.0, 1.0, DlColorSpace::kDisplayP3);
297 EXPECT_TRUE(DlColor(0.9, -0.0004, 0.0003, 1.0420, DlColorSpace::kExtendedSRGB)
300}
301
302TEST(DisplayListColor, ColorSpaceP3ToSRGB) {
303 DlColor red(0.9, 1.0, 0.0, 0.0, DlColorSpace::kDisplayP3);
304 EXPECT_TRUE(DlColor(0.9, 1.0, 0.0, 0.0, DlColorSpace::kSRGB)
305 .isClose(red.withColorSpace(DlColorSpace::kSRGB)))
307
308 DlColor green(0.9, 0.0, 1.0, 0.0, DlColorSpace::kDisplayP3);
309 EXPECT_TRUE(DlColor(0.9, 0.0, 1.0, 0.0, DlColorSpace::kSRGB)
310 .isClose(green.withColorSpace(DlColorSpace::kSRGB)))
312
313 DlColor blue(0.9, 0.0, 0.0, 1.0, DlColorSpace::kDisplayP3);
314 EXPECT_TRUE(DlColor(0.9, 0.0, 0.0003, 1.0, DlColorSpace::kSRGB)
315 .isClose(blue.withColorSpace(DlColorSpace::kSRGB)))
317}
318
319TEST(DisplayListColor, isClose) {
320 EXPECT_TRUE(DlColor(0xffaabbcc).isClose(DlColor(0xffaabbcc)));
321}
322
323TEST(DisplayListColor, isNotClose) {
324 EXPECT_FALSE(DlColor(0xffaabbcc).isClose(DlColor(0xffaabbcd)));
325}
326
327TEST(DisplayListColor, ClampAlpha) {
328 EXPECT_EQ(DlColor::ARGB(2.0, 0.0, 0.0, 0.0),
329 DlColor::ARGB(1.0, 0.0, 0.0, 0.0));
330
331 EXPECT_EQ(DlColor::ARGB(-1.0, 0.0, 0.0, 0.0),
332 DlColor::ARGB(0.0, 0.0, 0.0, 0.0));
333}
334
335} // namespace testing
336} // namespace flutter
static void arraysEqual(const uint32_t *ints, const DlColor *colors, int count)
TEST(NativeAssetsManagerTest, NoAvailableAssets)
impeller::Scalar DlScalar
static constexpr DlColor kBlue()
Definition dl_color.h:73
static constexpr DlColor RGBA(DlScalar r, DlScalar g, DlScalar b, DlScalar a)
Construct a 32 bit color from floating point R, G, B, and A color channels.
Definition dl_color.h:48
static constexpr DlColor kBlack()
Definition dl_color.h:69
static constexpr DlColor ARGB(DlScalar a, DlScalar r, DlScalar g, DlScalar b)
Construct a 32 bit color from floating point A, R, G, and B color channels.
Definition dl_color.h:57
int getAlpha() const
Definition dl_color.h:102
constexpr DlColorSpace getColorSpace() const
Definition dl_color.h:118
int getRed() const
Definition dl_color.h:105
constexpr DlScalar getRedF() const
Definition dl_color.h:114
constexpr DlScalar getAlphaF() const
Definition dl_color.h:113
DlColor withColorSpace(DlColorSpace color_space) const
Definition dl_color.cc:40
constexpr DlScalar getBlueF() const
Definition dl_color.h:116
constexpr DlScalar getGreenF() const
Definition dl_color.h:115
static constexpr DlColor kRed()
Definition dl_color.h:71
static constexpr DlColor kGreen()
Definition dl_color.h:72
int getGreen() const
Definition dl_color.h:108
int getBlue() const
Definition dl_color.h:111