1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/favicon_base/fallback_icon_style.h"
9 #include "ui/gfx/color_analysis.h"
10 #include "ui/gfx/color_utils.h"
12 namespace favicon_base {
16 // The maximum lightness of the background color to ensure light text is
18 const double kMaxBackgroundColorLightness = 0.67;
19 const double kMinBackgroundColorLightness = 0.15;
21 // Default values for FallbackIconStyle.
22 const SkColor kDefaultBackgroundColor = SkColorSetRGB(0x78, 0x78, 0x78);
23 const SkColor kDefaultTextColor = SK_ColorWHITE;
27 FallbackIconStyle::FallbackIconStyle()
28 : background_color(kDefaultBackgroundColor),
29 is_default_background_color(true),
30 text_color(kDefaultTextColor) {}
32 FallbackIconStyle::~FallbackIconStyle() {
35 bool FallbackIconStyle::operator==(const FallbackIconStyle& other) const {
36 return background_color == other.background_color &&
37 is_default_background_color == other.is_default_background_color &&
38 text_color == other.text_color;
41 void SetDominantColorAsBackground(
42 const scoped_refptr<base::RefCountedMemory>& bitmap_data,
43 FallbackIconStyle* style) {
44 // Try to ensure color's lightness isn't too large so that light text is
45 // visible. Set an upper bound for the dominant color.
46 const color_utils::HSL lower_bound{-1.0, -1.0, kMinBackgroundColorLightness};
47 const color_utils::HSL upper_bound{-1.0, -1.0, kMaxBackgroundColorLightness};
48 color_utils::GridSampler sampler;
49 SkColor dominant_color = color_utils::CalculateKMeanColorOfPNG(
50 bitmap_data, lower_bound, upper_bound, &sampler);
51 // |CalculateKMeanColorOfPNG| will try to return a color that lies within the
52 // specified bounds if one exists in the image. If there's no such color, it
53 // will return the dominant color which may be lighter than our upper bound.
54 // Clamp lightness down to a reasonable maximum value so text is readable.
55 color_utils::HSL color_hsl;
56 color_utils::SkColorToHSL(dominant_color, &color_hsl);
57 color_hsl.l = std::min(color_hsl.l, kMaxBackgroundColorLightness);
58 style->background_color =
59 color_utils::HSLToSkColor(color_hsl, SK_AlphaOPAQUE);
60 style->is_default_background_color = false;
63 } // namespace favicon_base