adjust browser_tests exclusion list for DrMemory to find suitable tests
[chromium-blink-merge.git] / ui / gfx / color_utils_unittest.cc
blobb90ae8e2c4268c576f3b9bbe6153d516ab0ba24f
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.
5 #include <stdlib.h>
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 namespace color_utils {
14 TEST(ColorUtils, SkColorToHSLRed) {
15 HSL hsl = {0, 0, 0};
16 SkColorToHSL(SK_ColorRED, &hsl);
17 EXPECT_DOUBLE_EQ(hsl.h, 0);
18 EXPECT_DOUBLE_EQ(hsl.s, 1);
19 EXPECT_DOUBLE_EQ(hsl.l, 0.5);
22 TEST(ColorUtils, SkColorToHSLGrey) {
23 HSL hsl = {0, 0, 0};
24 SkColorToHSL(SkColorSetARGB(255, 128, 128, 128), &hsl);
25 EXPECT_DOUBLE_EQ(hsl.h, 0);
26 EXPECT_DOUBLE_EQ(hsl.s, 0);
27 EXPECT_EQ(static_cast<int>(hsl.l * 100),
28 static_cast<int>(0.5 * 100)); // Accurate to two decimal places.
31 TEST(ColorUtils, HSLToSkColorWithAlpha) {
32 SkColor red = SkColorSetARGB(128, 255, 0, 0);
33 HSL hsl = {0, 1, 0.5};
34 SkColor result = HSLToSkColor(hsl, 128);
35 EXPECT_EQ(SkColorGetA(red), SkColorGetA(result));
36 EXPECT_EQ(SkColorGetR(red), SkColorGetR(result));
37 EXPECT_EQ(SkColorGetG(red), SkColorGetG(result));
38 EXPECT_EQ(SkColorGetB(red), SkColorGetB(result));
41 TEST(ColorUtils, RGBtoHSLRoundTrip) {
42 // Just spot check values near the edges.
43 for (int r = 0; r < 10; ++r) {
44 for (int g = 0; g < 10; ++g) {
45 for (int b = 0; b < 10; ++b) {
46 SkColor rgb = SkColorSetARGB(255, r, g, b);
47 HSL hsl = {0, 0, 0};
48 SkColorToHSL(rgb, &hsl);
49 SkColor out = HSLToSkColor(hsl, 255);
50 EXPECT_EQ(SkColorGetR(out), SkColorGetR(rgb));
51 EXPECT_EQ(SkColorGetG(out), SkColorGetG(rgb));
52 EXPECT_EQ(SkColorGetB(out), SkColorGetB(rgb));
56 for (int r = 240; r < 256; ++r) {
57 for (int g = 240; g < 256; ++g) {
58 for (int b = 240; b < 256; ++b) {
59 SkColor rgb = SkColorSetARGB(255, r, g, b);
60 HSL hsl = {0, 0, 0};
61 SkColorToHSL(rgb, &hsl);
62 SkColor out = HSLToSkColor(hsl, 255);
63 EXPECT_EQ(SkColorGetR(out), SkColorGetR(rgb));
64 EXPECT_EQ(SkColorGetG(out), SkColorGetG(rgb));
65 EXPECT_EQ(SkColorGetB(out), SkColorGetB(rgb));
71 TEST(ColorUtils, IsWithinHSLRange) {
72 HSL hsl = {0.3, 0.4, 0.5};
73 HSL lower = {0.2, 0.3, 0.4};
74 HSL upper = {0.4, 0.5, 0.6};
75 EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
76 // Bounds are inclusive.
77 hsl.h = 0.2;
78 EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
79 hsl.h = 0.4;
80 EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
81 hsl.s = 0.3;
82 EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
83 hsl.s = 0.5;
84 EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
85 hsl.l = 0.4;
86 EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
87 hsl.l = 0.6;
88 EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
91 TEST(ColorUtils, IsWithinHSLRangeHueWrapAround) {
92 HSL hsl = {0.3, 0.4, 0.5};
93 HSL lower = {0.8, -1, -1};
94 HSL upper = {1.2, -1, -1};
95 hsl.h = 0.1;
96 EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
97 hsl.h = 0.9;
98 EXPECT_TRUE(IsWithinHSLRange(hsl, lower, upper));
99 hsl.h = 0.3;
100 EXPECT_FALSE(IsWithinHSLRange(hsl, lower, upper));
103 TEST(ColorUtils, ColorToHSLRegisterSpill) {
104 // In a opt build on Linux, this was causing a register spill on my laptop
105 // (Pentium M) when converting from SkColor to HSL.
106 SkColor input = SkColorSetARGB(255, 206, 154, 89);
107 HSL hsl = {-1, -1, -1};
108 SkColor result = HSLShift(input, hsl);
109 // |result| should be the same as |input| since we passed in a value meaning
110 // no color shift.
111 EXPECT_EQ(SkColorGetA(input), SkColorGetA(result));
112 EXPECT_EQ(SkColorGetR(input), SkColorGetR(result));
113 EXPECT_EQ(SkColorGetG(input), SkColorGetG(result));
114 EXPECT_EQ(SkColorGetB(input), SkColorGetB(result));
117 TEST(ColorUtils, AlphaBlend) {
118 SkColor fore = SkColorSetARGB(255, 200, 200, 200);
119 SkColor back = SkColorSetARGB(255, 100, 100, 100);
121 EXPECT_TRUE(AlphaBlend(fore, back, 255) == fore);
122 EXPECT_TRUE(AlphaBlend(fore, back, 0) == back);
124 // One is fully transparent, result is partially transparent.
125 back = SkColorSetA(back, 0);
126 EXPECT_EQ(136U, SkColorGetA(AlphaBlend(fore, back, 136)));
128 // Both are fully transparent, result is fully transparent.
129 fore = SkColorSetA(fore, 0);
130 EXPECT_EQ(0U, SkColorGetA(AlphaBlend(fore, back, 255)));
133 } // namespace color_utils