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 18 matching lines...) Expand all Loading... |
29 TEST(ColorUtils, HSLToSkColorWithAlpha) { | 29 TEST(ColorUtils, HSLToSkColorWithAlpha) { |
30 SkColor red = SkColorSetARGB(128, 255, 0, 0); | 30 SkColor red = SkColorSetARGB(128, 255, 0, 0); |
31 color_utils::HSL hsl = { 0, 1, 0.5 }; | 31 color_utils::HSL hsl = { 0, 1, 0.5 }; |
32 SkColor result = color_utils::HSLToSkColor(hsl, 128); | 32 SkColor result = color_utils::HSLToSkColor(hsl, 128); |
33 EXPECT_EQ(SkColorGetA(red), SkColorGetA(result)); | 33 EXPECT_EQ(SkColorGetA(red), SkColorGetA(result)); |
34 EXPECT_EQ(SkColorGetR(red), SkColorGetR(result)); | 34 EXPECT_EQ(SkColorGetR(red), SkColorGetR(result)); |
35 EXPECT_EQ(SkColorGetG(red), SkColorGetG(result)); | 35 EXPECT_EQ(SkColorGetG(red), SkColorGetG(result)); |
36 EXPECT_EQ(SkColorGetB(red), SkColorGetB(result)); | 36 EXPECT_EQ(SkColorGetB(red), SkColorGetB(result)); |
37 } | 37 } |
38 | 38 |
39 | |
40 TEST(ColorUtils, RGBtoHSLRoundTrip) { | 39 TEST(ColorUtils, RGBtoHSLRoundTrip) { |
41 // Just spot check values near the edges. | 40 // Just spot check values near the edges. |
42 for (int r = 0; r < 10; ++r) { | 41 for (int r = 0; r < 10; ++r) { |
43 for (int g = 0; g < 10; ++g) { | 42 for (int g = 0; g < 10; ++g) { |
44 for (int b = 0; b < 10; ++b) { | 43 for (int b = 0; b < 10; ++b) { |
45 SkColor rgb = SkColorSetARGB(255, r, g, b); | 44 SkColor rgb = SkColorSetARGB(255, r, g, b); |
46 color_utils::HSL hsl = { 0, 0, 0 }; | 45 color_utils::HSL hsl = { 0, 0, 0 }; |
47 color_utils::SkColorToHSL(rgb, &hsl); | 46 color_utils::SkColorToHSL(rgb, &hsl); |
48 SkColor out = color_utils::HSLToSkColor(hsl, 255); | 47 SkColor out = color_utils::HSLToSkColor(hsl, 255); |
49 EXPECT_EQ(SkColorGetR(out), SkColorGetR(rgb)); | 48 EXPECT_EQ(SkColorGetR(out), SkColorGetR(rgb)); |
(...skipping 10 matching lines...) Expand all Loading... |
60 color_utils::SkColorToHSL(rgb, &hsl); | 59 color_utils::SkColorToHSL(rgb, &hsl); |
61 SkColor out = color_utils::HSLToSkColor(hsl, 255); | 60 SkColor out = color_utils::HSLToSkColor(hsl, 255); |
62 EXPECT_EQ(SkColorGetR(out), SkColorGetR(rgb)); | 61 EXPECT_EQ(SkColorGetR(out), SkColorGetR(rgb)); |
63 EXPECT_EQ(SkColorGetG(out), SkColorGetG(rgb)); | 62 EXPECT_EQ(SkColorGetG(out), SkColorGetG(rgb)); |
64 EXPECT_EQ(SkColorGetB(out), SkColorGetB(rgb)); | 63 EXPECT_EQ(SkColorGetB(out), SkColorGetB(rgb)); |
65 } | 64 } |
66 } | 65 } |
67 } | 66 } |
68 } | 67 } |
69 | 68 |
| 69 TEST(ColorUtils, IsWithinHSLRange) { |
| 70 color_utils::HSL hsl = {0.3, 0.4, 0.5}; |
| 71 { |
| 72 color_utils::HSL lower = {0.2, 0.3, 0.4}; |
| 73 color_utils::HSL upper = {0.4, 0.5, 0.6}; |
| 74 EXPECT_TRUE(color_utils::IsWithinHSLRange(hsl, lower, upper)); |
| 75 // Bounds are inclusive. |
| 76 hsl.h = 0.2; |
| 77 EXPECT_TRUE(color_utils::IsWithinHSLRange(hsl, lower, upper)); |
| 78 hsl.h = 0.4; |
| 79 EXPECT_TRUE(color_utils::IsWithinHSLRange(hsl, lower, upper)); |
| 80 hsl.s = 0.3; |
| 81 EXPECT_TRUE(color_utils::IsWithinHSLRange(hsl, lower, upper)); |
| 82 hsl.s = 0.5; |
| 83 EXPECT_TRUE(color_utils::IsWithinHSLRange(hsl, lower, upper)); |
| 84 hsl.l = 0.4; |
| 85 EXPECT_TRUE(color_utils::IsWithinHSLRange(hsl, lower, upper)); |
| 86 hsl.l = 0.6; |
| 87 EXPECT_TRUE(color_utils::IsWithinHSLRange(hsl, lower, upper)); |
| 88 } |
| 89 { |
| 90 // Check hue wrap-around. |
| 91 color_utils::HSL lower = {0.8, -1, -1}; |
| 92 color_utils::HSL upper = {1.2, -1, -1}; |
| 93 hsl.h = 0.1; |
| 94 EXPECT_TRUE(color_utils::IsWithinHSLRange(hsl, lower, upper)); |
| 95 hsl.h = 0.9; |
| 96 EXPECT_TRUE(color_utils::IsWithinHSLRange(hsl, lower, upper)); |
| 97 hsl.h = 0.3; |
| 98 EXPECT_FALSE(color_utils::IsWithinHSLRange(hsl, lower, upper)); |
| 99 } |
| 100 } |
70 TEST(ColorUtils, ColorToHSLRegisterSpill) { | 101 TEST(ColorUtils, ColorToHSLRegisterSpill) { |
71 // In a opt build on Linux, this was causing a register spill on my laptop | 102 // In a opt build on Linux, this was causing a register spill on my laptop |
72 // (Pentium M) when converting from SkColor to HSL. | 103 // (Pentium M) when converting from SkColor to HSL. |
73 SkColor input = SkColorSetARGB(255, 206, 154, 89); | 104 SkColor input = SkColorSetARGB(255, 206, 154, 89); |
74 color_utils::HSL hsl = { -1, -1, -1 }; | 105 color_utils::HSL hsl = { -1, -1, -1 }; |
75 SkColor result = color_utils::HSLShift(input, hsl); | 106 SkColor result = color_utils::HSLShift(input, hsl); |
76 // |result| should be the same as |input| since we passed in a value meaning | 107 // |result| should be the same as |input| since we passed in a value meaning |
77 // no color shift. | 108 // no color shift. |
78 EXPECT_EQ(SkColorGetA(input), SkColorGetA(result)); | 109 EXPECT_EQ(SkColorGetA(input), SkColorGetA(result)); |
79 EXPECT_EQ(SkColorGetR(input), SkColorGetR(result)); | 110 EXPECT_EQ(SkColorGetR(input), SkColorGetR(result)); |
(...skipping 11 matching lines...) Expand all Loading... |
91 back); | 122 back); |
92 | 123 |
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(color_utils::AlphaBlend(fore, back, 136))); | 126 EXPECT_EQ(136U, SkColorGetA(color_utils::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(color_utils::AlphaBlend(fore, back, 255))); | 130 EXPECT_EQ(0U, SkColorGetA(color_utils::AlphaBlend(fore, back, 255))); |
100 } | 131 } |
OLD | NEW |