OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/favicon_base/fallback_icon_style.h" | 5 #include "components/favicon_base/fallback_icon_style.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "ui/gfx/color_analysis.h" |
7 #include "ui/gfx/color_utils.h" | 10 #include "ui/gfx/color_utils.h" |
8 | 11 |
9 namespace favicon_base { | 12 namespace favicon_base { |
10 | 13 |
11 namespace { | 14 namespace { |
12 | 15 |
13 // Luminance threshold for background color determine whether to use dark or | 16 // Luminance threshold for background color determine whether to use dark or |
14 // light text color. | 17 // light text color. |
15 int kDarkTextLuminanceThreshold = 190; | 18 const int kDarkTextLuminanceThreshold = 190; |
| 19 |
| 20 // The maximum luminance of the background color to ensure light text is |
| 21 // readable. |
| 22 const double kMaxBackgroundColorLuminance = 0.67; |
16 | 23 |
17 // Default values for FallbackIconStyle. | 24 // Default values for FallbackIconStyle. |
18 SkColor kDefaultBackgroundColor = SkColorSetRGB(0x80, 0x80, 0x80); | 25 const SkColor kDefaultBackgroundColor = SkColorSetRGB(0x78, 0x78, 0x78); |
19 SkColor kDefaultTextColorDark = SK_ColorBLACK; | 26 const SkColor kDefaultTextColorDark = SK_ColorBLACK; |
20 SkColor kDefaultTextColorLight = SK_ColorWHITE; | 27 const SkColor kDefaultTextColorLight = SK_ColorWHITE; |
21 double kDefaultFontSizeRatio = 0.8; | 28 const double kDefaultFontSizeRatio = 0.44; |
22 double kDefaultRoundness = 0.125; // 1 / 8. | 29 const double kDefaultRoundness = 0; // Square. Round corners are applied |
| 30 // externally (Javascript or Java). |
23 | 31 |
24 } // namespace | 32 } // namespace |
25 | 33 |
26 FallbackIconStyle::FallbackIconStyle() | 34 FallbackIconStyle::FallbackIconStyle() |
27 : background_color(kDefaultBackgroundColor), | 35 : background_color(kDefaultBackgroundColor), |
28 text_color(kDefaultTextColorLight), | 36 text_color(kDefaultTextColorLight), |
29 font_size_ratio(kDefaultFontSizeRatio), | 37 font_size_ratio(kDefaultFontSizeRatio), |
30 roundness(kDefaultRoundness) { | 38 roundness(kDefaultRoundness) { |
31 } | 39 } |
32 | 40 |
33 FallbackIconStyle::~FallbackIconStyle() { | 41 FallbackIconStyle::~FallbackIconStyle() { |
34 } | 42 } |
35 | 43 |
36 void MatchFallbackIconTextColorAgainstBackgroundColor( | 44 void MatchFallbackIconTextColorAgainstBackgroundColor( |
37 FallbackIconStyle* style) { | 45 FallbackIconStyle* style) { |
38 int luminance = color_utils::GetLuminanceForColor(style->background_color); | 46 int luminance = color_utils::GetLuminanceForColor(style->background_color); |
39 style->text_color = (luminance >= kDarkTextLuminanceThreshold ? | 47 style->text_color = (luminance >= kDarkTextLuminanceThreshold ? |
40 kDefaultTextColorDark : kDefaultTextColorLight); | 48 kDefaultTextColorDark : kDefaultTextColorLight); |
41 } | 49 } |
42 | 50 |
43 bool ValidateFallbackIconStyle(const FallbackIconStyle& style) { | 51 bool ValidateFallbackIconStyle(const FallbackIconStyle& style) { |
44 return style.font_size_ratio >= 0.0 && style.font_size_ratio <= 1.0 && | 52 return style.font_size_ratio >= 0.0 && style.font_size_ratio <= 1.0 && |
45 style.roundness >= 0.0 && style.roundness <= 1.0; | 53 style.roundness >= 0.0 && style.roundness <= 1.0; |
46 } | 54 } |
47 | 55 |
| 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); |
| 68 } |
| 69 |
48 } // namespace favicon_base | 70 } // namespace favicon_base |
OLD | NEW |