Commit b644bbc7 authored by calamity@chromium.org's avatar calamity@chromium.org

Put color util and color analysis tests into color_utils namespace.

This CL is a precursor for https://codereview.chromium.org/289283004/.

BUG=376613

Review URL: https://codereview.chromium.org/309773002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274343 0039d316-1c4b-4281-b951-d872f2087c98
parent da5886de
......@@ -12,9 +12,7 @@
#include "ui/gfx/canvas.h"
#include "ui/gfx/rect.h"
using color_utils::FindClosestColor;
namespace {
namespace color_utils {
const unsigned char k1x1White[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
......@@ -87,7 +85,7 @@ const unsigned char k1x3BlueRed[] = {
0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
};
class MockKMeanImageSampler : public color_utils::KMeanImageSampler {
class MockKMeanImageSampler : public KMeanImageSampler {
public:
MockKMeanImageSampler() : current_result_index_(0) {
}
......@@ -147,8 +145,6 @@ void Calculate8bitBitmapMinMax(const SkBitmap& bitmap,
}
}
} // namespace
class ColorAnalysisTest : public testing::Test {
};
......@@ -162,8 +158,7 @@ TEST_F(ColorAnalysisTest, CalculatePNGKMeanAllWhite) {
k1x1White,
k1x1White + sizeof(k1x1White) / sizeof(unsigned char))));
SkColor color =
color_utils::CalculateKMeanColorOfPNG(png, 100, 600, &test_sampler);
SkColor color = CalculateKMeanColorOfPNG(png, 100, 600, &test_sampler);
EXPECT_EQ(color, SK_ColorWHITE);
}
......@@ -180,8 +175,7 @@ TEST_F(ColorAnalysisTest, CalculatePNGKMeanIgnoreWhite) {
k1x3BlueWhite,
k1x3BlueWhite + sizeof(k1x3BlueWhite) / sizeof(unsigned char))));
SkColor color =
color_utils::CalculateKMeanColorOfPNG(png, 100, 600, &test_sampler);
SkColor color = CalculateKMeanColorOfPNG(png, 100, 600, &test_sampler);
EXPECT_EQ(color, SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF));
}
......@@ -198,14 +192,13 @@ TEST_F(ColorAnalysisTest, CalculatePNGKMeanPickMostCommon) {
k1x3BlueRed,
k1x3BlueRed + sizeof(k1x3BlueRed) / sizeof(unsigned char))));
SkColor color =
color_utils::CalculateKMeanColorOfPNG(png, 100, 600, &test_sampler);
SkColor color = CalculateKMeanColorOfPNG(png, 100, 600, &test_sampler);
EXPECT_EQ(color, SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00));
}
TEST_F(ColorAnalysisTest, GridSampler) {
color_utils::GridSampler sampler;
GridSampler sampler;
const int kWidth = 16;
const int kHeight = 16;
// Sample starts at 1,1.
......@@ -254,7 +247,7 @@ TEST_F(ColorAnalysisTest, CalculateKMeanColorOfBitmap) {
bitmap.allocPixels();
bitmap.eraseARGB(255, 100, 150, 200);
SkColor color = color_utils::CalculateKMeanColorOfBitmap(bitmap);
SkColor color = CalculateKMeanColorOfBitmap(bitmap);
EXPECT_EQ(255u, SkColorGetA(color));
// Color values are not exactly equal due to reversal of premultiplied alpha.
EXPECT_TRUE(ChannelApproximatelyEqual(100, SkColorGetR(color)));
......@@ -263,7 +256,7 @@ TEST_F(ColorAnalysisTest, CalculateKMeanColorOfBitmap) {
// Test a bitmap with an alpha channel.
bitmap.eraseARGB(128, 100, 150, 200);
color = color_utils::CalculateKMeanColorOfBitmap(bitmap);
color = CalculateKMeanColorOfBitmap(bitmap);
// Alpha channel should be ignored for dominant color calculation.
EXPECT_EQ(255u, SkColorGetA(color));
......@@ -276,11 +269,10 @@ TEST_F(ColorAnalysisTest, ComputeColorCovarianceTrivial) {
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 200);
EXPECT_EQ(gfx::Matrix3F::Zeros(),
color_utils::ComputeColorCovariance(bitmap));
EXPECT_EQ(gfx::Matrix3F::Zeros(), ComputeColorCovariance(bitmap));
bitmap.allocPixels();
bitmap.eraseARGB(255, 50, 150, 200);
gfx::Matrix3F covariance = color_utils::ComputeColorCovariance(bitmap);
gfx::Matrix3F covariance = ComputeColorCovariance(bitmap);
// The answer should be all zeros.
EXPECT_TRUE(covariance == gfx::Matrix3F::Zeros());
}
......@@ -297,7 +289,7 @@ TEST_F(ColorAnalysisTest, ComputeColorCovarianceWithCanvas) {
SkBitmap bitmap =
skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
gfx::Matrix3F covariance = color_utils::ComputeColorCovariance(bitmap);
gfx::Matrix3F covariance = ComputeColorCovariance(bitmap);
gfx::Matrix3F expected_covariance = gfx::Matrix3F::Zeros();
expected_covariance.set(2400, 400, -1600,
......@@ -319,8 +311,7 @@ TEST_F(ColorAnalysisTest, ApplyColorReductionSingleColor) {
gfx::Vector3dF transform(1.0f, .5f, 0.1f);
// This transform, if not scaled, should result in GL=145.
EXPECT_TRUE(color_utils::ApplyColorReduction(
source, transform, false, &result));
EXPECT_TRUE(ApplyColorReduction(source, transform, false, &result));
uint8_t min_gl = 0;
uint8_t max_gl = 0;
......@@ -329,24 +320,21 @@ TEST_F(ColorAnalysisTest, ApplyColorReductionSingleColor) {
EXPECT_EQ(145, max_gl);
// Now scan requesting rescale. Expect all 0.
EXPECT_TRUE(color_utils::ApplyColorReduction(
source, transform, true, &result));
EXPECT_TRUE(ApplyColorReduction(source, transform, true, &result));
Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
EXPECT_EQ(0, min_gl);
EXPECT_EQ(0, max_gl);
// Test cliping to upper limit.
transform.set_z(1.1f);
EXPECT_TRUE(color_utils::ApplyColorReduction(
source, transform, false, &result));
EXPECT_TRUE(ApplyColorReduction(source, transform, false, &result));
Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
EXPECT_EQ(0xFF, min_gl);
EXPECT_EQ(0xFF, max_gl);
// Test cliping to upper limit.
transform.Scale(-1.0f);
EXPECT_TRUE(color_utils::ApplyColorReduction(
source, transform, false, &result));
EXPECT_TRUE(ApplyColorReduction(source, transform, false, &result));
Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
EXPECT_EQ(0x0, min_gl);
EXPECT_EQ(0x0, max_gl);
......@@ -367,8 +355,7 @@ TEST_F(ColorAnalysisTest, ApplyColorReductionBlackAndWhite) {
result.allocPixels();
gfx::Vector3dF transform(1.0f, 0.5f, 0.1f);
EXPECT_TRUE(color_utils::ApplyColorReduction(
source, transform, true, &result));
EXPECT_TRUE(ApplyColorReduction(source, transform, true, &result));
uint8_t min_gl = 0;
uint8_t max_gl = 0;
Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
......@@ -380,8 +367,7 @@ TEST_F(ColorAnalysisTest, ApplyColorReductionBlackAndWhite) {
// Reverse test.
transform.Scale(-1.0f);
EXPECT_TRUE(color_utils::ApplyColorReduction(
source, transform, true, &result));
EXPECT_TRUE(ApplyColorReduction(source, transform, true, &result));
min_gl = 0;
max_gl = 0;
Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
......@@ -408,8 +394,7 @@ TEST_F(ColorAnalysisTest, ApplyColorReductionMultiColor) {
result.allocPixels();
gfx::Vector3dF transform(1.0f, 0.5f, 0.1f);
EXPECT_TRUE(color_utils::ApplyColorReduction(
source, transform, false, &result));
EXPECT_TRUE(ApplyColorReduction(source, transform, false, &result));
uint8_t min_gl = 0;
uint8_t max_gl = 0;
Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
......@@ -419,8 +404,7 @@ TEST_F(ColorAnalysisTest, ApplyColorReductionMultiColor) {
EXPECT_EQ(max_gl, SkColorGetA(result.getColor(150, 0)));
EXPECT_EQ(100U, SkColorGetA(result.getColor(0, 0)));
EXPECT_TRUE(color_utils::ApplyColorReduction(
source, transform, true, &result));
EXPECT_TRUE(ApplyColorReduction(source, transform, true, &result));
Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
EXPECT_EQ(0, min_gl);
EXPECT_EQ(255, max_gl);
......@@ -439,7 +423,7 @@ TEST_F(ColorAnalysisTest, ComputePrincipalComponentImageNotComputable) {
source.eraseARGB(255, 50, 150, 200);
// This computation should fail since all colors always vary together.
EXPECT_FALSE(color_utils::ComputePrincipalComponentImage(source, &result));
EXPECT_FALSE(ComputePrincipalComponentImage(source, &result));
}
TEST_F(ColorAnalysisTest, ComputePrincipalComponentImage) {
......@@ -456,7 +440,7 @@ TEST_F(ColorAnalysisTest, ComputePrincipalComponentImage) {
result.allocPixels();
// This computation should fail since all colors always vary together.
EXPECT_TRUE(color_utils::ComputePrincipalComponentImage(source, &result));
EXPECT_TRUE(ComputePrincipalComponentImage(source, &result));
uint8_t min_gl = 0;
uint8_t max_gl = 0;
......@@ -468,3 +452,5 @@ TEST_F(ColorAnalysisTest, ComputePrincipalComponentImage) {
EXPECT_EQ(max_gl, SkColorGetA(result.getColor(299, 199)));
EXPECT_EQ(93U, SkColorGetA(result.getColor(150, 0)));
}
} // namespace color_utils
......@@ -9,17 +9,19 @@
#include "third_party/skia/include/core/SkColorPriv.h"
#include "ui/gfx/color_utils.h"
namespace color_utils {
TEST(ColorUtils, SkColorToHSLRed) {
color_utils::HSL hsl = { 0, 0, 0 };
color_utils::SkColorToHSL(SK_ColorRED, &hsl);
HSL hsl = {0, 0, 0};
SkColorToHSL(SK_ColorRED, &hsl);
EXPECT_DOUBLE_EQ(hsl.h, 0);
EXPECT_DOUBLE_EQ(hsl.s, 1);
EXPECT_DOUBLE_EQ(hsl.l, 0.5);
}
TEST(ColorUtils, SkColorToHSLGrey) {
color_utils::HSL hsl = { 0, 0, 0 };
color_utils::SkColorToHSL(SkColorSetARGB(255, 128, 128, 128), &hsl);
HSL hsl = {0, 0, 0};
SkColorToHSL(SkColorSetARGB(255, 128, 128, 128), &hsl);
EXPECT_DOUBLE_EQ(hsl.h, 0);
EXPECT_DOUBLE_EQ(hsl.s, 0);
EXPECT_EQ(static_cast<int>(hsl.l * 100),
......@@ -28,8 +30,8 @@ TEST(ColorUtils, SkColorToHSLGrey) {
TEST(ColorUtils, HSLToSkColorWithAlpha) {
SkColor red = SkColorSetARGB(128, 255, 0, 0);
color_utils::HSL hsl = { 0, 1, 0.5 };
SkColor result = color_utils::HSLToSkColor(hsl, 128);
HSL hsl = {0, 1, 0.5};
SkColor result = HSLToSkColor(hsl, 128);
EXPECT_EQ(SkColorGetA(red), SkColorGetA(result));
EXPECT_EQ(SkColorGetR(red), SkColorGetR(result));
EXPECT_EQ(SkColorGetG(red), SkColorGetG(result));
......@@ -43,9 +45,9 @@ TEST(ColorUtils, RGBtoHSLRoundTrip) {
for (int g = 0; g < 10; ++g) {
for (int b = 0; b < 10; ++b) {
SkColor rgb = SkColorSetARGB(255, r, g, b);
color_utils::HSL hsl = { 0, 0, 0 };
color_utils::SkColorToHSL(rgb, &hsl);
SkColor out = color_utils::HSLToSkColor(hsl, 255);
HSL hsl = {0, 0, 0};
SkColorToHSL(rgb, &hsl);
SkColor out = HSLToSkColor(hsl, 255);
EXPECT_EQ(SkColorGetR(out), SkColorGetR(rgb));
EXPECT_EQ(SkColorGetG(out), SkColorGetG(rgb));
EXPECT_EQ(SkColorGetB(out), SkColorGetB(rgb));
......@@ -56,9 +58,9 @@ TEST(ColorUtils, RGBtoHSLRoundTrip) {
for (int g = 240; g < 256; ++g) {
for (int b = 240; b < 256; ++b) {
SkColor rgb = SkColorSetARGB(255, r, g, b);
color_utils::HSL hsl = { 0, 0, 0 };
color_utils::SkColorToHSL(rgb, &hsl);
SkColor out = color_utils::HSLToSkColor(hsl, 255);
HSL hsl = {0, 0, 0};
SkColorToHSL(rgb, &hsl);
SkColor out = HSLToSkColor(hsl, 255);
EXPECT_EQ(SkColorGetR(out), SkColorGetR(rgb));
EXPECT_EQ(SkColorGetG(out), SkColorGetG(rgb));
EXPECT_EQ(SkColorGetB(out), SkColorGetB(rgb));
......@@ -71,8 +73,8 @@ TEST(ColorUtils, ColorToHSLRegisterSpill) {
// In a opt build on Linux, this was causing a register spill on my laptop
// (Pentium M) when converting from SkColor to HSL.
SkColor input = SkColorSetARGB(255, 206, 154, 89);
color_utils::HSL hsl = { -1, -1, -1 };
SkColor result = color_utils::HSLShift(input, hsl);
HSL hsl = {-1, -1, -1};
SkColor result = HSLShift(input, hsl);
// |result| should be the same as |input| since we passed in a value meaning
// no color shift.
EXPECT_EQ(SkColorGetA(input), SkColorGetA(result));
......@@ -85,16 +87,16 @@ TEST(ColorUtils, AlphaBlend) {
SkColor fore = SkColorSetARGB(255, 200, 200, 200);
SkColor back = SkColorSetARGB(255, 100, 100, 100);
EXPECT_TRUE(color_utils::AlphaBlend(fore, back, 255) ==
fore);
EXPECT_TRUE(color_utils::AlphaBlend(fore, back, 0) ==
back);
EXPECT_TRUE(AlphaBlend(fore, back, 255) == fore);
EXPECT_TRUE(AlphaBlend(fore, back, 0) == back);
// One is fully transparent, result is partially transparent.
back = SkColorSetA(back, 0);
EXPECT_EQ(136U, SkColorGetA(color_utils::AlphaBlend(fore, back, 136)));
EXPECT_EQ(136U, SkColorGetA(AlphaBlend(fore, back, 136)));
// Both are fully transparent, result is fully transparent.
fore = SkColorSetA(fore, 0);
EXPECT_EQ(0U, SkColorGetA(color_utils::AlphaBlend(fore, back, 255)));
EXPECT_EQ(0U, SkColorGetA(AlphaBlend(fore, back, 255)));
}
} // namespace color_utils
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment