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

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: make bounds inclusive, add saturation test 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 4c8bfa2007b33ef8ec3caf92c6f359305d315420..cd25b8a0e66f631c7170bd5d8a302fa910423216 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 kLowerHSLBound = {-1, -1, 0.15};
Matt Giuca 2014/05/22 05:03:52 kDefaultLowerHSLBound kDefaultUpperHSLBound
calamity 2014/05/22 07:36:36 Done.
+const color_utils::HSL kUpperHSLBound = {-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)) {
// 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;
}
}
}
@@ -355,17 +355,22 @@ SkColor CalculateKMeanColorOfBuffer(uint8_t* decoded_data,
return FindClosestColor(decoded_data, img_width, img_height, color);
}
+SkColor CalculateKMeanColorOfPNG(scoped_refptr<base::RefCountedMemory> png) {
+ GridSampler sampler;
+ return CalculateKMeanColorOfPNG(
+ png, kLowerHSLBound, kUpperHSLBound, &sampler);
+}
+
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 +380,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;
@@ -391,13 +396,13 @@ SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap) {
UnPreMultiply(bitmap, image.get(), pixel_count);
GridSampler sampler;
- SkColor color = CalculateKMeanColorOfBuffer(
- reinterpret_cast<uint8_t*>(image.get()),
- bitmap.width(),
- bitmap.height(),
- kMinDarkness,
- kMaxBrightness,
- &sampler);
+ SkColor color =
+ CalculateKMeanColorOfBuffer(reinterpret_cast<uint8_t*>(image.get()),
+ bitmap.width(),
+ bitmap.height(),
+ kLowerHSLBound,
+ kUpperHSLBound,
+ &sampler);
return color;
}

Powered by Google App Engine
This is Rietveld 408576698