Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Side by Side Diff: ui/gfx/color_analysis_unittest.cc

Issue 289283004: Add ability to constrain dominant color selection to a HSL range. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ui/gfx/color_analysis.h" 5 #include "ui/gfx/color_analysis.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkBitmap.h" 10 #include "third_party/skia/include/core/SkBitmap.h"
11 #include "third_party/skia/include/core/SkColor.h" 11 #include "third_party/skia/include/core/SkColor.h"
12 #include "ui/gfx/canvas.h" 12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/color_utils.h"
14 #include "ui/gfx/image/image.h"
13 #include "ui/gfx/rect.h" 15 #include "ui/gfx/rect.h"
14 16
15 using color_utils::FindClosestColor; 17 using color_utils::FindClosestColor;
Alexei Svitkine (slow) 2014/05/30 14:56:39 Nit: Put everything in the file into the color_uti
calamity 2014/06/02 03:53:17 Done.
16 18
17 namespace { 19 namespace {
18 20
19 const unsigned char k1x1White[] = { 21 const unsigned char k1x1White[] = {
20 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 22 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
21 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 23 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
22 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 24 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
23 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53, 25 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53,
24 0xde, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47, 26 0xde, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47,
25 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00, 27 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, 82 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69,
81 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x57, 83 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x57,
82 0x81, 0x0e, 0x17, 0x00, 0x00, 0x00, 0x14, 0x49, 84 0x81, 0x0e, 0x17, 0x00, 0x00, 0x00, 0x14, 0x49,
83 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xcf, 85 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xcf,
84 0xc0, 0xc0, 0xc4, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 86 0xc0, 0xc0, 0xc4, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0,
85 0xf0, 0x1f, 0x00, 0x0c, 0x10, 0x02, 0x01, 0x2c, 87 0xf0, 0x1f, 0x00, 0x0c, 0x10, 0x02, 0x01, 0x2c,
86 0x8f, 0x8b, 0x8c, 0x00, 0x00, 0x00, 0x00, 0x49, 88 0x8f, 0x8b, 0x8c, 0x00, 0x00, 0x00, 0x00, 0x49,
87 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82 89 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
88 }; 90 };
89 91
92 const color_utils::HSL kDefaultLowerBound = {-1, -1, 0.15};
93 const color_utils::HSL kDefaultUpperBound = {-1, -1, 0.85};
94
95 // Creates a 1-dimensional png of the pixel colors found in |colors|.
96 scoped_refptr<base::RefCountedMemory> CreateTestPNG(
97 const std::vector<SkColor>& colors) {
98 SkBitmap bitmap;
99 bitmap.setConfig(SkBitmap::kARGB_8888_Config, colors.size(), 1);
100 bitmap.allocPixels();
101
102 SkAutoLockPixels lock(bitmap);
103 for (size_t i = 0; i < colors.size(); ++i) {
104 bitmap.eraseArea(SkIRect::MakeXYWH(i, 0, 1, 1), colors[i]);
105 }
106 return gfx::Image::CreateFrom1xBitmap(bitmap).As1xPNGBytes();
107 }
108
90 class MockKMeanImageSampler : public color_utils::KMeanImageSampler { 109 class MockKMeanImageSampler : public color_utils::KMeanImageSampler {
91 public: 110 public:
92 MockKMeanImageSampler() : current_result_index_(0) { 111 MockKMeanImageSampler() : current_result_index_(0) {
93 } 112 }
94 113
95 explicit MockKMeanImageSampler(const std::vector<int>& samples) 114 explicit MockKMeanImageSampler(const std::vector<int>& samples)
96 : prebaked_sample_results_(samples), 115 : prebaked_sample_results_(samples),
97 current_result_index_(0) { 116 current_result_index_(0) {
98 } 117 }
99 118
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 TEST_F(ColorAnalysisTest, CalculatePNGKMeanAllWhite) { 174 TEST_F(ColorAnalysisTest, CalculatePNGKMeanAllWhite) {
156 MockKMeanImageSampler test_sampler; 175 MockKMeanImageSampler test_sampler;
157 test_sampler.AddSample(0); 176 test_sampler.AddSample(0);
158 177
159 scoped_refptr<base::RefCountedBytes> png( 178 scoped_refptr<base::RefCountedBytes> png(
160 new base::RefCountedBytes( 179 new base::RefCountedBytes(
161 std::vector<unsigned char>( 180 std::vector<unsigned char>(
162 k1x1White, 181 k1x1White,
163 k1x1White + sizeof(k1x1White) / sizeof(unsigned char)))); 182 k1x1White + sizeof(k1x1White) / sizeof(unsigned char))));
164 183
165 SkColor color = 184 SkColor color = color_utils::CalculateKMeanColorOfPNG(
166 color_utils::CalculateKMeanColorOfPNG(png, 100, 600, &test_sampler); 185 png, kDefaultLowerBound, kDefaultUpperBound, &test_sampler);
167 186
168 EXPECT_EQ(color, SK_ColorWHITE); 187 EXPECT_EQ(color, SK_ColorWHITE);
169 } 188 }
170 189
171 TEST_F(ColorAnalysisTest, CalculatePNGKMeanIgnoreWhite) { 190 TEST_F(ColorAnalysisTest, CalculatePNGKMeanIgnoreWhiteLightness) {
172 MockKMeanImageSampler test_sampler; 191 MockKMeanImageSampler test_sampler;
173 test_sampler.AddSample(0); 192 test_sampler.AddSample(0);
174 test_sampler.AddSample(1); 193 test_sampler.AddSample(1);
175 test_sampler.AddSample(2); 194 test_sampler.AddSample(2);
176 195
177 scoped_refptr<base::RefCountedBytes> png( 196 scoped_refptr<base::RefCountedBytes> png(
178 new base::RefCountedBytes( 197 new base::RefCountedBytes(
179 std::vector<unsigned char>( 198 std::vector<unsigned char>(
180 k1x3BlueWhite, 199 k1x3BlueWhite,
181 k1x3BlueWhite + sizeof(k1x3BlueWhite) / sizeof(unsigned char)))); 200 k1x3BlueWhite + sizeof(k1x3BlueWhite) / sizeof(unsigned char))));
182 201
183 SkColor color = 202 SkColor color = color_utils::CalculateKMeanColorOfPNG(
184 color_utils::CalculateKMeanColorOfPNG(png, 100, 600, &test_sampler); 203 png, kDefaultLowerBound, kDefaultUpperBound, &test_sampler);
185 204
186 EXPECT_EQ(color, SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF)); 205 EXPECT_EQ(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF), color);
187 } 206 }
188 207
189 TEST_F(ColorAnalysisTest, CalculatePNGKMeanPickMostCommon) { 208 TEST_F(ColorAnalysisTest, CalculatePNGKMeanPickMostCommon) {
190 MockKMeanImageSampler test_sampler; 209 MockKMeanImageSampler test_sampler;
191 test_sampler.AddSample(0); 210 test_sampler.AddSample(0);
192 test_sampler.AddSample(1); 211 test_sampler.AddSample(1);
193 test_sampler.AddSample(2); 212 test_sampler.AddSample(2);
194 213
195 scoped_refptr<base::RefCountedBytes> png( 214 scoped_refptr<base::RefCountedBytes> png(
196 new base::RefCountedBytes( 215 new base::RefCountedBytes(
197 std::vector<unsigned char>( 216 std::vector<unsigned char>(
198 k1x3BlueRed, 217 k1x3BlueRed,
199 k1x3BlueRed + sizeof(k1x3BlueRed) / sizeof(unsigned char)))); 218 k1x3BlueRed + sizeof(k1x3BlueRed) / sizeof(unsigned char))));
200 219
220 SkColor color = color_utils::CalculateKMeanColorOfPNG(
221 png, kDefaultLowerBound, kDefaultUpperBound, &test_sampler);
222
223 EXPECT_EQ(SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00), color);
224 }
225
226 TEST_F(ColorAnalysisTest, CalculatePNGKMeanIgnoreRedHue) {
227 MockKMeanImageSampler test_sampler;
228 test_sampler.AddSample(0);
229 test_sampler.AddSample(1);
230 test_sampler.AddSample(2);
231
232 std::vector<SkColor> colors(4, SK_ColorRED);
233 colors[1] = SK_ColorBLUE;
234
235 scoped_refptr<base::RefCountedMemory> png = CreateTestPNG(colors);
236
201 SkColor color = 237 SkColor color =
202 color_utils::CalculateKMeanColorOfPNG(png, 100, 600, &test_sampler); 238 color_utils::CalculateKMeanColorOfPNG(png,
239 color_utils::HSL{0.2, -1, 0.15},
240 color_utils::HSL{0.8, -1, 0.85},
241 &test_sampler);
203 242
204 EXPECT_EQ(color, SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00)); 243 EXPECT_EQ(SK_ColorBLUE, color);
244 }
245
246 TEST_F(ColorAnalysisTest, CalculatePNGKMeanIgnoreGreySaturation) {
247 MockKMeanImageSampler test_sampler;
248 test_sampler.AddSample(0);
249 test_sampler.AddSample(1);
250 test_sampler.AddSample(2);
251
252 std::vector<SkColor> colors(4, SK_ColorGRAY);
253 colors[1] = SK_ColorBLUE;
254
255 scoped_refptr<base::RefCountedMemory> png = CreateTestPNG(colors);
256 SkColor color =
257 color_utils::CalculateKMeanColorOfPNG(png,
258 color_utils::HSL{-1, 0.3, -1},
259 color_utils::HSL{-1, 1, -1},
260 &test_sampler);
261
262 EXPECT_EQ(SK_ColorBLUE, color);
205 } 263 }
206 264
207 TEST_F(ColorAnalysisTest, GridSampler) { 265 TEST_F(ColorAnalysisTest, GridSampler) {
208 color_utils::GridSampler sampler; 266 color_utils::GridSampler sampler;
209 const int kWidth = 16; 267 const int kWidth = 16;
210 const int kHeight = 16; 268 const int kHeight = 16;
211 // Sample starts at 1,1. 269 // Sample starts at 1,1.
212 EXPECT_EQ(1 + 1 * kWidth, sampler.GetSample(kWidth, kHeight)); 270 EXPECT_EQ(1 + 1 * kWidth, sampler.GetSample(kWidth, kHeight));
213 EXPECT_EQ(1 + 4 * kWidth, sampler.GetSample(kWidth, kHeight)); 271 EXPECT_EQ(1 + 4 * kWidth, sampler.GetSample(kWidth, kHeight));
214 EXPECT_EQ(1 + 7 * kWidth, sampler.GetSample(kWidth, kHeight)); 272 EXPECT_EQ(1 + 7 * kWidth, sampler.GetSample(kWidth, kHeight));
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 uint8_t min_gl = 0; 519 uint8_t min_gl = 0;
462 uint8_t max_gl = 0; 520 uint8_t max_gl = 0;
463 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); 521 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
464 522
465 EXPECT_EQ(0, min_gl); 523 EXPECT_EQ(0, min_gl);
466 EXPECT_EQ(255, max_gl); 524 EXPECT_EQ(255, max_gl);
467 EXPECT_EQ(min_gl, SkColorGetA(result.getColor(0, 0))); 525 EXPECT_EQ(min_gl, SkColorGetA(result.getColor(0, 0)));
468 EXPECT_EQ(max_gl, SkColorGetA(result.getColor(299, 199))); 526 EXPECT_EQ(max_gl, SkColorGetA(result.getColor(299, 199)));
469 EXPECT_EQ(93U, SkColorGetA(result.getColor(150, 0))); 527 EXPECT_EQ(93U, SkColorGetA(result.getColor(150, 0)));
470 } 528 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698