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/canvas.h"
11 #include "ui/gfx/color_utils.h"
12 #include "ui/gfx/geometry/rect.h"
14 namespace color_utils
{
16 TEST(ColorUtils
, SkColorToHSLRed
) {
18 SkColorToHSL(SK_ColorRED
, &hsl
);
19 EXPECT_DOUBLE_EQ(hsl
.h
, 0);
20 EXPECT_DOUBLE_EQ(hsl
.s
, 1);
21 EXPECT_DOUBLE_EQ(hsl
.l
, 0.5);
24 TEST(ColorUtils
, SkColorToHSLGrey
) {
26 SkColorToHSL(SkColorSetARGB(255, 128, 128, 128), &hsl
);
27 EXPECT_DOUBLE_EQ(hsl
.h
, 0);
28 EXPECT_DOUBLE_EQ(hsl
.s
, 0);
29 EXPECT_EQ(static_cast<int>(hsl
.l
* 100),
30 static_cast<int>(0.5 * 100)); // Accurate to two decimal places.
33 TEST(ColorUtils
, HSLToSkColorWithAlpha
) {
34 SkColor red
= SkColorSetARGB(128, 255, 0, 0);
35 HSL hsl
= {0, 1, 0.5};
36 SkColor result
= HSLToSkColor(hsl
, 128);
37 EXPECT_EQ(SkColorGetA(red
), SkColorGetA(result
));
38 EXPECT_EQ(SkColorGetR(red
), SkColorGetR(result
));
39 EXPECT_EQ(SkColorGetG(red
), SkColorGetG(result
));
40 EXPECT_EQ(SkColorGetB(red
), SkColorGetB(result
));
43 TEST(ColorUtils
, RGBtoHSLRoundTrip
) {
44 // Just spot check values near the edges.
45 for (int r
= 0; r
< 10; ++r
) {
46 for (int g
= 0; g
< 10; ++g
) {
47 for (int b
= 0; b
< 10; ++b
) {
48 SkColor rgb
= SkColorSetARGB(255, r
, g
, b
);
50 SkColorToHSL(rgb
, &hsl
);
51 SkColor out
= HSLToSkColor(hsl
, 255);
52 EXPECT_EQ(SkColorGetR(out
), SkColorGetR(rgb
));
53 EXPECT_EQ(SkColorGetG(out
), SkColorGetG(rgb
));
54 EXPECT_EQ(SkColorGetB(out
), SkColorGetB(rgb
));
58 for (int r
= 240; r
< 256; ++r
) {
59 for (int g
= 240; g
< 256; ++g
) {
60 for (int b
= 240; b
< 256; ++b
) {
61 SkColor rgb
= SkColorSetARGB(255, r
, g
, b
);
63 SkColorToHSL(rgb
, &hsl
);
64 SkColor out
= HSLToSkColor(hsl
, 255);
65 EXPECT_EQ(SkColorGetR(out
), SkColorGetR(rgb
));
66 EXPECT_EQ(SkColorGetG(out
), SkColorGetG(rgb
));
67 EXPECT_EQ(SkColorGetB(out
), SkColorGetB(rgb
));
73 TEST(ColorUtils
, IsWithinHSLRange
) {
74 HSL hsl
= {0.3, 0.4, 0.5};
75 HSL lower
= {0.2, 0.3, 0.4};
76 HSL upper
= {0.4, 0.5, 0.6};
77 EXPECT_TRUE(IsWithinHSLRange(hsl
, lower
, upper
));
78 // Bounds are inclusive.
80 EXPECT_TRUE(IsWithinHSLRange(hsl
, lower
, upper
));
82 EXPECT_TRUE(IsWithinHSLRange(hsl
, lower
, upper
));
84 EXPECT_TRUE(IsWithinHSLRange(hsl
, lower
, upper
));
86 EXPECT_TRUE(IsWithinHSLRange(hsl
, lower
, upper
));
88 EXPECT_TRUE(IsWithinHSLRange(hsl
, lower
, upper
));
90 EXPECT_TRUE(IsWithinHSLRange(hsl
, lower
, upper
));
93 TEST(ColorUtils
, IsWithinHSLRangeHueWrapAround
) {
94 HSL hsl
= {0.3, 0.4, 0.5};
95 HSL lower
= {0.8, -1, -1};
96 HSL upper
= {1.2, -1, -1};
98 EXPECT_TRUE(IsWithinHSLRange(hsl
, lower
, upper
));
100 EXPECT_TRUE(IsWithinHSLRange(hsl
, lower
, upper
));
102 EXPECT_FALSE(IsWithinHSLRange(hsl
, lower
, upper
));
105 TEST(ColorUtils
, ColorToHSLRegisterSpill
) {
106 // In a opt build on Linux, this was causing a register spill on my laptop
107 // (Pentium M) when converting from SkColor to HSL.
108 SkColor input
= SkColorSetARGB(255, 206, 154, 89);
109 HSL hsl
= {-1, -1, -1};
110 SkColor result
= HSLShift(input
, hsl
);
111 // |result| should be the same as |input| since we passed in a value meaning
113 EXPECT_EQ(SkColorGetA(input
), SkColorGetA(result
));
114 EXPECT_EQ(SkColorGetR(input
), SkColorGetR(result
));
115 EXPECT_EQ(SkColorGetG(input
), SkColorGetG(result
));
116 EXPECT_EQ(SkColorGetB(input
), SkColorGetB(result
));
119 TEST(ColorUtils
, CalculateBoringScore_Empty
) {
121 EXPECT_DOUBLE_EQ(1.0, CalculateBoringScore(bitmap
));
124 TEST(ColorUtils
, CalculateBoringScore_SingleColor
) {
125 const gfx::Size
kSize(20, 10);
126 gfx::Canvas
canvas(kSize
, 1.0f
, true);
127 // Fill all pixels in black.
128 canvas
.FillRect(gfx::Rect(kSize
), SK_ColorBLACK
);
131 skia::GetTopDevice(*canvas
.sk_canvas())->accessBitmap(false);
132 // The thumbnail should deserve the highest boring score.
133 EXPECT_DOUBLE_EQ(1.0, CalculateBoringScore(bitmap
));
136 TEST(ColorUtils
, CalculateBoringScore_TwoColors
) {
137 const gfx::Size
kSize(20, 10);
139 gfx::Canvas
canvas(kSize
, 1.0f
, true);
140 // Fill all pixels in black.
141 canvas
.FillRect(gfx::Rect(kSize
), SK_ColorBLACK
);
142 // Fill the left half pixels in white.
143 canvas
.FillRect(gfx::Rect(0, 0, kSize
.width() / 2, kSize
.height()),
147 skia::GetTopDevice(*canvas
.sk_canvas())->accessBitmap(false);
148 ASSERT_EQ(kSize
.width(), bitmap
.width());
149 ASSERT_EQ(kSize
.height(), bitmap
.height());
150 // The thumbnail should be less boring because two colors are used.
151 EXPECT_DOUBLE_EQ(0.5, CalculateBoringScore(bitmap
));
154 TEST(ColorUtils
, AlphaBlend
) {
155 SkColor fore
= SkColorSetARGB(255, 200, 200, 200);
156 SkColor back
= SkColorSetARGB(255, 100, 100, 100);
158 EXPECT_TRUE(AlphaBlend(fore
, back
, 255) == fore
);
159 EXPECT_TRUE(AlphaBlend(fore
, back
, 0) == back
);
161 // One is fully transparent, result is partially transparent.
162 back
= SkColorSetA(back
, 0);
163 EXPECT_EQ(136U, SkColorGetA(AlphaBlend(fore
, back
, 136)));
165 // Both are fully transparent, result is fully transparent.
166 fore
= SkColorSetA(fore
, 0);
167 EXPECT_EQ(0U, SkColorGetA(AlphaBlend(fore
, back
, 255)));
170 } // namespace color_utils