Chromium Code Reviews| 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 "ui/gfx/color_analysis.h" | |
| 7 #include "ui/gfx/color_utils.h" | 8 #include "ui/gfx/color_utils.h" |
| 8 | 9 |
| 9 namespace favicon_base { | 10 namespace favicon_base { |
| 10 | 11 |
| 11 namespace { | 12 namespace { |
| 12 | 13 |
| 13 // Luminance threshold for background color determine whether to use dark or | 14 // Luminance threshold for background color determine whether to use dark or |
| 14 // light text color. | 15 // light text color. |
| 15 int kDarkTextLuminanceThreshold = 190; | 16 const int kDarkTextLuminanceThreshold = 190; |
| 17 | |
| 18 // The maximum luminance of the background color to ensure light text is | |
| 19 // readable. | |
| 20 const double kMaxBackgroundColorLuminance = 0.67; | |
| 16 | 21 |
| 17 // Default values for FallbackIconStyle. | 22 // Default values for FallbackIconStyle. |
| 18 SkColor kDefaultBackgroundColor = SkColorSetRGB(0x80, 0x80, 0x80); | 23 const SkColor kDarkGray = SkColorSetRGB(0x78, 0x78, 0x78); |
| 19 SkColor kDefaultTextColorDark = SK_ColorBLACK; | 24 const SkColor kDefaultBackgroundColor = kDarkGray; |
| 20 SkColor kDefaultTextColorLight = SK_ColorWHITE; | 25 const SkColor kDefaultTextColorDark = SK_ColorBLACK; |
| 21 double kDefaultFontSizeRatio = 0.8; | 26 const SkColor kDefaultTextColorLight = SK_ColorWHITE; |
| 22 double kDefaultRoundness = 0.125; // 1 / 8. | 27 const double kDefaultFontSizeRatio = 0.44; |
| 28 const double kDefaultRoundness = 0; // Square. Round corners are applied | |
| 29 // externally (Javascript or Java). | |
| 23 | 30 |
| 24 } // namespace | 31 } // namespace |
| 25 | 32 |
| 26 FallbackIconStyle::FallbackIconStyle() | 33 FallbackIconStyle::FallbackIconStyle() |
| 27 : background_color(kDefaultBackgroundColor), | 34 : background_color(kDefaultBackgroundColor), |
| 28 text_color(kDefaultTextColorLight), | 35 text_color(kDefaultTextColorLight), |
| 29 font_size_ratio(kDefaultFontSizeRatio), | 36 font_size_ratio(kDefaultFontSizeRatio), |
| 30 roundness(kDefaultRoundness) { | 37 roundness(kDefaultRoundness) { |
| 31 } | 38 } |
| 32 | 39 |
| 33 FallbackIconStyle::~FallbackIconStyle() { | 40 FallbackIconStyle::~FallbackIconStyle() { |
| 34 } | 41 } |
| 35 | 42 |
| 36 void MatchFallbackIconTextColorAgainstBackgroundColor( | 43 void MatchFallbackIconTextColorAgainstBackgroundColor( |
| 37 FallbackIconStyle* style) { | 44 FallbackIconStyle* style) { |
| 38 int luminance = color_utils::GetLuminanceForColor(style->background_color); | 45 int luminance = color_utils::GetLuminanceForColor(style->background_color); |
| 39 style->text_color = (luminance >= kDarkTextLuminanceThreshold ? | 46 style->text_color = (luminance >= kDarkTextLuminanceThreshold ? |
| 40 kDefaultTextColorDark : kDefaultTextColorLight); | 47 kDefaultTextColorDark : kDefaultTextColorLight); |
| 41 } | 48 } |
| 42 | 49 |
| 43 bool ValidateFallbackIconStyle(const FallbackIconStyle& style) { | 50 bool ValidateFallbackIconStyle(const FallbackIconStyle& style) { |
| 44 return style.font_size_ratio >= 0.0 && style.font_size_ratio <= 1.0 && | 51 return style.font_size_ratio >= 0.0 && style.font_size_ratio <= 1.0 && |
| 45 style.roundness >= 0.0 && style.roundness <= 1.0; | 52 style.roundness >= 0.0 && style.roundness <= 1.0; |
| 46 } | 53 } |
| 47 | 54 |
| 55 void SetDominantColorAsBackground( | |
| 56 const scoped_refptr<base::RefCountedMemory>& bitmap_data, | |
| 57 FallbackIconStyle* style) { | |
| 58 SkColor dominant_color = | |
| 59 color_utils::CalculateKMeanColorOfPNG(bitmap_data); | |
|
huangs
2015/04/17 15:30:47
// Assumes |style.text_color| is light, and clamps
beaudoin
2015/04/17 20:29:05
Done.
| |
| 60 color_utils::HSL color_hsl; | |
| 61 color_utils::SkColorToHSL(dominant_color, &color_hsl); | |
| 62 color_hsl.l = std::min(color_hsl.l, kMaxBackgroundColorLuminance); | |
| 63 style->background_color = | |
| 64 color_utils::HSLToSkColor(color_hsl, SK_AlphaOPAQUE); | |
| 65 } | |
| 66 | |
| 67 | |
| 48 } // namespace favicon_base | 68 } // namespace favicon_base |
| OLD | NEW |