1 // Copyright (c) 2006-2008 The Chromium 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.
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "third_party/skia/include/core/SkBitmap.h"
9 #include "third_party/skia/include/core/SkColorPriv.h"
10 #include "ui/gfx/color_utils.h"
12 TEST(ColorUtils
, SkColorToHSLRed
) {
13 color_utils::HSL hsl
= { 0, 0, 0 };
14 color_utils::SkColorToHSL(SK_ColorRED
, &hsl
);
15 EXPECT_DOUBLE_EQ(hsl
.h
, 0);
16 EXPECT_DOUBLE_EQ(hsl
.s
, 1);
17 EXPECT_DOUBLE_EQ(hsl
.l
, 0.5);
20 TEST(ColorUtils
, SkColorToHSLGrey
) {
21 color_utils::HSL hsl
= { 0, 0, 0 };
22 color_utils::SkColorToHSL(SkColorSetARGB(255, 128, 128, 128), &hsl
);
23 EXPECT_DOUBLE_EQ(hsl
.h
, 0);
24 EXPECT_DOUBLE_EQ(hsl
.s
, 0);
25 EXPECT_EQ(static_cast<int>(hsl
.l
* 100),
26 static_cast<int>(0.5 * 100)); // Accurate to two decimal places.
29 TEST(ColorUtils
, HSLToSkColorWithAlpha
) {
30 SkColor red
= SkColorSetARGB(128, 255, 0, 0);
31 color_utils::HSL hsl
= { 0, 1, 0.5 };
32 SkColor result
= color_utils::HSLToSkColor(hsl
, 128);
33 EXPECT_EQ(SkColorGetA(red
), SkColorGetA(result
));
34 EXPECT_EQ(SkColorGetR(red
), SkColorGetR(result
));
35 EXPECT_EQ(SkColorGetG(red
), SkColorGetG(result
));
36 EXPECT_EQ(SkColorGetB(red
), SkColorGetB(result
));
40 TEST(ColorUtils
, RGBtoHSLRoundTrip
) {
41 // Just spot check values near the edges.
42 for (int r
= 0; r
< 10; ++r
) {
43 for (int g
= 0; g
< 10; ++g
) {
44 for (int b
= 0; b
< 10; ++b
) {
45 SkColor rgb
= SkColorSetARGB(255, r
, g
, b
);
46 color_utils::HSL hsl
= { 0, 0, 0 };
47 color_utils::SkColorToHSL(rgb
, &hsl
);
48 SkColor out
= color_utils::HSLToSkColor(hsl
, 255);
49 EXPECT_EQ(SkColorGetR(out
), SkColorGetR(rgb
));
50 EXPECT_EQ(SkColorGetG(out
), SkColorGetG(rgb
));
51 EXPECT_EQ(SkColorGetB(out
), SkColorGetB(rgb
));
55 for (int r
= 240; r
< 256; ++r
) {
56 for (int g
= 240; g
< 256; ++g
) {
57 for (int b
= 240; b
< 256; ++b
) {
58 SkColor rgb
= SkColorSetARGB(255, r
, g
, b
);
59 color_utils::HSL hsl
= { 0, 0, 0 };
60 color_utils::SkColorToHSL(rgb
, &hsl
);
61 SkColor out
= color_utils::HSLToSkColor(hsl
, 255);
62 EXPECT_EQ(SkColorGetR(out
), SkColorGetR(rgb
));
63 EXPECT_EQ(SkColorGetG(out
), SkColorGetG(rgb
));
64 EXPECT_EQ(SkColorGetB(out
), SkColorGetB(rgb
));
70 TEST(ColorUtils
, ColorToHSLRegisterSpill
) {
71 // In a opt build on Linux, this was causing a register spill on my laptop
72 // (Pentium M) when converting from SkColor to HSL.
73 SkColor input
= SkColorSetARGB(255, 206, 154, 89);
74 color_utils::HSL hsl
= { -1, -1, -1 };
75 SkColor result
= color_utils::HSLShift(input
, hsl
);
76 // |result| should be the same as |input| since we passed in a value meaning
78 EXPECT_EQ(SkColorGetA(input
), SkColorGetA(result
));
79 EXPECT_EQ(SkColorGetR(input
), SkColorGetR(result
));
80 EXPECT_EQ(SkColorGetG(input
), SkColorGetG(result
));
81 EXPECT_EQ(SkColorGetB(input
), SkColorGetB(result
));
84 TEST(ColorUtils
, AlphaBlend
) {
85 SkColor fore
= SkColorSetARGB(255, 200, 200, 200);
86 SkColor back
= SkColorSetARGB(255, 100, 100, 100);
88 EXPECT_TRUE(color_utils::AlphaBlend(fore
, back
, 255) ==
90 EXPECT_TRUE(color_utils::AlphaBlend(fore
, back
, 0) ==
93 // One is fully transparent, result is partially transparent.
94 back
= SkColorSetA(back
, 0);
95 EXPECT_EQ(136U, SkColorGetA(color_utils::AlphaBlend(fore
, back
, 136)));
97 // Both are fully transparent, result is fully transparent.
98 fore
= SkColorSetA(fore
, 0);
99 EXPECT_EQ(0U, SkColorGetA(color_utils::AlphaBlend(fore
, back
, 255)));