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 // Luminance threshold for background color determine whether to use dark or
18 const int kDarkTextLuminanceThreshold
= 190;
20 // The maximum luminance of the background color to ensure light text is
22 const double kMaxBackgroundColorLuminance
= 0.67;
24 // Default values for FallbackIconStyle.
25 const SkColor kDefaultBackgroundColor
= SkColorSetRGB(0x78, 0x78, 0x78);
26 const SkColor kDefaultTextColorDark
= SK_ColorBLACK
;
27 const SkColor kDefaultTextColorLight
= SK_ColorWHITE
;
28 const double kDefaultFontSizeRatio
= 0.44;
29 const double kDefaultRoundness
= 0; // Square. Round corners are applied
30 // externally (Javascript or Java).
34 FallbackIconStyle::FallbackIconStyle()
35 : background_color(kDefaultBackgroundColor
),
36 text_color(kDefaultTextColorLight
),
37 font_size_ratio(kDefaultFontSizeRatio
),
38 roundness(kDefaultRoundness
) {
41 FallbackIconStyle::~FallbackIconStyle() {
44 void MatchFallbackIconTextColorAgainstBackgroundColor(
45 FallbackIconStyle
* style
) {
46 int luminance
= color_utils::GetLuminanceForColor(style
->background_color
);
47 style
->text_color
= (luminance
>= kDarkTextLuminanceThreshold
?
48 kDefaultTextColorDark
: kDefaultTextColorLight
);
51 bool ValidateFallbackIconStyle(const FallbackIconStyle
& style
) {
52 return style
.font_size_ratio
>= 0.0 && style
.font_size_ratio
<= 1.0 &&
53 style
.roundness
>= 0.0 && style
.roundness
<= 1.0;
56 void SetDominantColorAsBackground(
57 const scoped_refptr
<base::RefCountedMemory
>& bitmap_data
,
58 FallbackIconStyle
* style
) {
59 SkColor dominant_color
=
60 color_utils::CalculateKMeanColorOfPNG(bitmap_data
);
61 // Assumes |style.text_color| is light, and clamps luminance down to a
62 // reasonable maximum value so text is readable.
63 color_utils::HSL color_hsl
;
64 color_utils::SkColorToHSL(dominant_color
, &color_hsl
);
65 color_hsl
.l
= std::min(color_hsl
.l
, kMaxBackgroundColorLuminance
);
66 style
->background_color
=
67 color_utils::HSLToSkColor(color_hsl
, SK_AlphaOPAQUE
);
70 } // namespace favicon_base