OLD | NEW |
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" |
| 13 #include "ui/gfx/rect.h" |
12 | 14 |
13 using color_utils::FindClosestColor; | 15 using color_utils::FindClosestColor; |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
17 const unsigned char k1x1White[] = { | 19 const unsigned char k1x1White[] = { |
18 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, | 20 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, |
19 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, | 21 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, |
20 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, | 22 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
21 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53, | 23 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53, |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 // Test a bitmap with an alpha channel. | 252 // Test a bitmap with an alpha channel. |
251 bitmap.eraseARGB(128, 100, 150, 200); | 253 bitmap.eraseARGB(128, 100, 150, 200); |
252 color = color_utils::CalculateKMeanColorOfBitmap(bitmap); | 254 color = color_utils::CalculateKMeanColorOfBitmap(bitmap); |
253 | 255 |
254 // Alpha channel should be ignored for dominant color calculation. | 256 // Alpha channel should be ignored for dominant color calculation. |
255 EXPECT_EQ(255u, SkColorGetA(color)); | 257 EXPECT_EQ(255u, SkColorGetA(color)); |
256 EXPECT_TRUE(ChannelApproximatelyEqual(100, SkColorGetR(color))); | 258 EXPECT_TRUE(ChannelApproximatelyEqual(100, SkColorGetR(color))); |
257 EXPECT_TRUE(ChannelApproximatelyEqual(150, SkColorGetG(color))); | 259 EXPECT_TRUE(ChannelApproximatelyEqual(150, SkColorGetG(color))); |
258 EXPECT_TRUE(ChannelApproximatelyEqual(200, SkColorGetB(color))); | 260 EXPECT_TRUE(ChannelApproximatelyEqual(200, SkColorGetB(color))); |
259 } | 261 } |
| 262 |
| 263 TEST_F(ColorAnalysisTest, ComputeColorCovarianceTrivial) { |
| 264 SkBitmap bitmap; |
| 265 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 200); |
| 266 |
| 267 EXPECT_EQ(gfx::Matrix3F::Zeros(), |
| 268 color_utils::ComputeColorCovariance(bitmap)); |
| 269 bitmap.allocPixels(); |
| 270 bitmap.eraseRGB(50, 150, 200); |
| 271 gfx::Matrix3F covariance = color_utils::ComputeColorCovariance(bitmap); |
| 272 // The answer should be all zeros. |
| 273 EXPECT_TRUE(covariance == gfx::Matrix3F::Zeros()); |
| 274 } |
| 275 |
| 276 TEST_F(ColorAnalysisTest, ComputeColorCovarianceWithCanvas) { |
| 277 gfx::Canvas canvas(gfx::Size(250, 200), ui::SCALE_FACTOR_100P, true); |
| 278 // The image consists of vertical stripes, with color bands set to 100 |
| 279 // in overlapping stripes 150 pixels wide. |
| 280 canvas.FillRect(gfx::Rect(0, 0, 50, 200), SkColorSetRGB(100, 0, 0)); |
| 281 canvas.FillRect(gfx::Rect(50, 0, 50, 200), SkColorSetRGB(100, 100, 0)); |
| 282 canvas.FillRect(gfx::Rect(100, 0, 50, 200), SkColorSetRGB(100, 100, 100)); |
| 283 canvas.FillRect(gfx::Rect(150, 0, 50, 200), SkColorSetRGB(0, 100, 100)); |
| 284 canvas.FillRect(gfx::Rect(200, 0, 50, 200), SkColorSetRGB(0, 0, 100)); |
| 285 |
| 286 SkBitmap bitmap = |
| 287 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
| 288 gfx::Matrix3F covariance = color_utils::ComputeColorCovariance(bitmap); |
| 289 |
| 290 gfx::Matrix3F expected_covariance = gfx::Matrix3F::Zeros(); |
| 291 expected_covariance.set(2400, 400, -1600, |
| 292 400, 2400, 400, |
| 293 -1600, 400, 2400); |
| 294 EXPECT_EQ(expected_covariance, covariance); |
| 295 } |
OLD | NEW |