OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 | 6 |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "third_party/skia/include/core/SkBitmap.h" | 8 #include "third_party/skia/include/core/SkBitmap.h" |
9 #include "third_party/skia/include/core/SkColorPriv.h" | 9 #include "third_party/skia/include/core/SkColorPriv.h" |
10 #include "ui/gfx/color_utils.h" | 10 #include "ui/gfx/color_utils.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 TEST(ColorUtils, HSLToSkColorWithAlpha) { | 31 TEST(ColorUtils, HSLToSkColorWithAlpha) { |
32 SkColor red = SkColorSetARGB(128, 255, 0, 0); | 32 SkColor red = SkColorSetARGB(128, 255, 0, 0); |
33 HSL hsl = {0, 1, 0.5}; | 33 HSL hsl = {0, 1, 0.5}; |
34 SkColor result = HSLToSkColor(hsl, 128); | 34 SkColor result = HSLToSkColor(hsl, 128); |
35 EXPECT_EQ(SkColorGetA(red), SkColorGetA(result)); | 35 EXPECT_EQ(SkColorGetA(red), SkColorGetA(result)); |
36 EXPECT_EQ(SkColorGetR(red), SkColorGetR(result)); | 36 EXPECT_EQ(SkColorGetR(red), SkColorGetR(result)); |
37 EXPECT_EQ(SkColorGetG(red), SkColorGetG(result)); | 37 EXPECT_EQ(SkColorGetG(red), SkColorGetG(result)); |
38 EXPECT_EQ(SkColorGetB(red), SkColorGetB(result)); | 38 EXPECT_EQ(SkColorGetB(red), SkColorGetB(result)); |
39 } | 39 } |
40 | 40 |
41 | |
42 TEST(ColorUtils, RGBtoHSLRoundTrip) { | 41 TEST(ColorUtils, RGBtoHSLRoundTrip) { |
43 // Just spot check values near the edges. | 42 // Just spot check values near the edges. |
44 for (int r = 0; r < 10; ++r) { | 43 for (int r = 0; r < 10; ++r) { |
45 for (int g = 0; g < 10; ++g) { | 44 for (int g = 0; g < 10; ++g) { |
46 for (int b = 0; b < 10; ++b) { | 45 for (int b = 0; b < 10; ++b) { |
47 SkColor rgb = SkColorSetARGB(255, r, g, b); | 46 SkColor rgb = SkColorSetARGB(255, r, g, b); |
48 HSL hsl = {0, 0, 0}; | 47 HSL hsl = {0, 0, 0}; |
49 SkColorToHSL(rgb, &hsl); | 48 SkColorToHSL(rgb, &hsl); |
50 SkColor out = HSLToSkColor(hsl, 255); | 49 SkColor out = HSLToSkColor(hsl, 255); |
51 EXPECT_EQ(SkColorGetR(out), SkColorGetR(rgb)); | 50 EXPECT_EQ(SkColorGetR(out), SkColorGetR(rgb)); |
(...skipping 10 matching lines...) Expand all Loading... |
62 SkColorToHSL(rgb, &hsl); | 61 SkColorToHSL(rgb, &hsl); |
63 SkColor out = HSLToSkColor(hsl, 255); | 62 SkColor out = HSLToSkColor(hsl, 255); |
64 EXPECT_EQ(SkColorGetR(out), SkColorGetR(rgb)); | 63 EXPECT_EQ(SkColorGetR(out), SkColorGetR(rgb)); |
65 EXPECT_EQ(SkColorGetG(out), SkColorGetG(rgb)); | 64 EXPECT_EQ(SkColorGetG(out), SkColorGetG(rgb)); |
66 EXPECT_EQ(SkColorGetB(out), SkColorGetB(rgb)); | 65 EXPECT_EQ(SkColorGetB(out), SkColorGetB(rgb)); |
67 } | 66 } |
68 } | 67 } |
69 } | 68 } |
70 } | 69 } |
71 | 70 |
| 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)); |
| 89 } |
| 90 |
| 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)); |
| 101 } |
| 102 |
72 TEST(ColorUtils, ColorToHSLRegisterSpill) { | 103 TEST(ColorUtils, ColorToHSLRegisterSpill) { |
73 // In a opt build on Linux, this was causing a register spill on my laptop | 104 // In a opt build on Linux, this was causing a register spill on my laptop |
74 // (Pentium M) when converting from SkColor to HSL. | 105 // (Pentium M) when converting from SkColor to HSL. |
75 SkColor input = SkColorSetARGB(255, 206, 154, 89); | 106 SkColor input = SkColorSetARGB(255, 206, 154, 89); |
76 HSL hsl = {-1, -1, -1}; | 107 HSL hsl = {-1, -1, -1}; |
77 SkColor result = HSLShift(input, hsl); | 108 SkColor result = HSLShift(input, hsl); |
78 // |result| should be the same as |input| since we passed in a value meaning | 109 // |result| should be the same as |input| since we passed in a value meaning |
79 // no color shift. | 110 // no color shift. |
80 EXPECT_EQ(SkColorGetA(input), SkColorGetA(result)); | 111 EXPECT_EQ(SkColorGetA(input), SkColorGetA(result)); |
81 EXPECT_EQ(SkColorGetR(input), SkColorGetR(result)); | 112 EXPECT_EQ(SkColorGetR(input), SkColorGetR(result)); |
(...skipping 11 matching lines...) Expand all Loading... |
93 // One is fully transparent, result is partially transparent. | 124 // One is fully transparent, result is partially transparent. |
94 back = SkColorSetA(back, 0); | 125 back = SkColorSetA(back, 0); |
95 EXPECT_EQ(136U, SkColorGetA(AlphaBlend(fore, back, 136))); | 126 EXPECT_EQ(136U, SkColorGetA(AlphaBlend(fore, back, 136))); |
96 | 127 |
97 // Both are fully transparent, result is fully transparent. | 128 // Both are fully transparent, result is fully transparent. |
98 fore = SkColorSetA(fore, 0); | 129 fore = SkColorSetA(fore, 0); |
99 EXPECT_EQ(0U, SkColorGetA(AlphaBlend(fore, back, 255))); | 130 EXPECT_EQ(0U, SkColorGetA(AlphaBlend(fore, back, 255))); |
100 } | 131 } |
101 | 132 |
102 } // namespace color_utils | 133 } // namespace color_utils |
OLD | NEW |