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

Unified Diff: ui/gfx/color_analysis.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, 7 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 side-by-side diff with in-line comments
Download patch
Index: ui/gfx/color_analysis.cc
diff --git a/ui/gfx/color_analysis.cc b/ui/gfx/color_analysis.cc
index c243970da4fcb286de4aaa41ceee99bd81a296bf..babc6bb863bd398af845e71c10587c80b902b28e 100644
--- a/ui/gfx/color_analysis.cc
+++ b/ui/gfx/color_analysis.cc
@@ -13,14 +13,16 @@
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkUnPreMultiply.h"
#include "ui/gfx/codec/png_codec.h"
+#include "ui/gfx/color_utils.h"
namespace {
// RGBA KMean Constants
const uint32_t kNumberOfClusters = 4;
const int kNumberOfIterations = 50;
-const uint32_t kMaxBrightness = 665;
-const uint32_t kMinDarkness = 100;
+
+const color_utils::HSL kDefaultLowerHSLBound = {-1, -1, 0.15};
Alexei Svitkine (slow) 2014/05/30 14:56:39 Nit: Move the anonymous namespace inside color_uti
calamity 2014/06/02 03:53:17 Done.
+const color_utils::HSL kDefaultUpperHSLBound = {-1, -1, 0.85};
// Background Color Modification Constants
const SkColor kDefaultBgColor = SK_ColorWHITE;
@@ -214,8 +216,8 @@ SkColor FindClosestColor(const uint8_t* image,
SkColor CalculateKMeanColorOfBuffer(uint8_t* decoded_data,
int img_width,
int img_height,
- uint32_t darkness_limit,
- uint32_t brightness_limit,
+ const HSL& lower_bound,
+ const HSL& upper_bound,
KMeanImageSampler* sampler) {
SkColor color = kDefaultBgColor;
if (img_width > 0 && img_height > 0) {
@@ -331,21 +333,19 @@ SkColor CalculateKMeanColorOfBuffer(uint8_t* decoded_data,
cluster != clusters.end(); ++cluster) {
uint8_t r, g, b;
cluster->GetCentroid(&r, &g, &b);
- // Sum the RGB components to determine if the color is too bright or too
- // dark.
- // TODO (dtrainor): Look into using HSV here instead. This approximation
- // might be fine though.
- uint32_t summed_color = r + g + b;
- if (summed_color < brightness_limit && summed_color > darkness_limit) {
+ SkColor current_color = SkColorSetARGB(SK_AlphaOPAQUE, r, g, b);
+ HSL hsl;
+ SkColorToHSL(current_color, &hsl);
+ if (color_utils::IsWithinHSLRange(hsl, lower_bound, upper_bound)) {
Alexei Svitkine (slow) 2014/05/30 14:56:39 Nit: color_utils:: unecessary
calamity 2014/06/02 03:53:17 Done.
// If we found a valid color just set it and break. We don't want to
// check the other ones.
- color = SkColorSetARGB(0xFF, r, g, b);
+ color = current_color;
break;
} else if (cluster == clusters.begin()) {
// We haven't found a valid color, but we are at the first color so
// set the color anyway to make sure we at least have a value here.
- color = SkColorSetARGB(0xFF, r, g, b);
+ color = current_color;
}
}
}
@@ -356,16 +356,15 @@ SkColor CalculateKMeanColorOfBuffer(uint8_t* decoded_data,
}
SkColor CalculateKMeanColorOfPNG(scoped_refptr<base::RefCountedMemory> png,
- uint32_t darkness_limit,
- uint32_t brightness_limit,
+ const HSL& lower_bound,
+ const HSL& upper_bound,
KMeanImageSampler* sampler) {
int img_width = 0;
int img_height = 0;
std::vector<uint8_t> decoded_data;
SkColor color = kDefaultBgColor;
- if (png.get() &&
- png->size() &&
+ if (png.get() && png->size() &&
gfx::PNGCodec::Decode(png->front(),
png->size(),
gfx::PNGCodec::FORMAT_BGRA,
@@ -375,8 +374,8 @@ SkColor CalculateKMeanColorOfPNG(scoped_refptr<base::RefCountedMemory> png,
return CalculateKMeanColorOfBuffer(&decoded_data[0],
img_width,
img_height,
- darkness_limit,
- brightness_limit,
+ lower_bound,
+ upper_bound,
sampler);
}
return color;
@@ -384,12 +383,13 @@ SkColor CalculateKMeanColorOfPNG(scoped_refptr<base::RefCountedMemory> png,
SkColor CalculateKMeanColorOfPNG(scoped_refptr<base::RefCountedMemory> png) {
GridSampler sampler;
- return CalculateKMeanColorOfPNG(png, kMinDarkness, kMaxBrightness, &sampler);
+ return CalculateKMeanColorOfPNG(
+ png, kDefaultLowerHSLBound, kDefaultUpperHSLBound, &sampler);
}
SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap,
- uint32_t darkness_limit,
- uint32_t brightness_limit,
+ const HSL& lower_bound,
+ const HSL& upper_bound,
KMeanImageSampler* sampler) {
// SkBitmap uses pre-multiplied alpha but the KMean clustering function
// above uses non-pre-multiplied alpha. Transform the bitmap before we
@@ -401,15 +401,15 @@ SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap,
return CalculateKMeanColorOfBuffer(reinterpret_cast<uint8_t*>(image.get()),
bitmap.width(),
bitmap.height(),
- darkness_limit,
- brightness_limit,
+ lower_bound,
+ upper_bound,
sampler);
}
SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap) {
GridSampler sampler;
return CalculateKMeanColorOfBitmap(
- bitmap, kMinDarkness, kMaxBrightness, &sampler);
+ bitmap, kDefaultLowerHSLBound, kDefaultUpperHSLBound, &sampler);
}
gfx::Matrix3F ComputeColorCovariance(const SkBitmap& bitmap) {

Powered by Google App Engine
This is Rietveld 408576698