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 <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 kDarkGray = SkColorSetRGB(0x78, 0x78, 0x78); |
| 19 SkColor kDefaultTextColorDark = SK_ColorBLACK; | 26 const SkColor kDefaultBackgroundColor = kDarkGray; |
| 20 SkColor kDefaultTextColorLight = SK_ColorWHITE; | 27 const SkColor kDefaultTextColorDark = SK_ColorBLACK; |
| 21 double kDefaultFontSizeRatio = 0.8; | 28 const SkColor kDefaultTextColorLight = SK_ColorWHITE; |
| 22 double kDefaultRoundness = 0.125; // 1 / 8. | 29 const double kDefaultFontSizeRatio = 0.44; |
| 30 const double kDefaultRoundness = 0; // Square. Round corners are applied | |
| 31 // externally (Javascript or Java). | |
| 23 | 32 |
| 24 } // namespace | 33 } // namespace |
| 25 | 34 |
| 26 FallbackIconStyle::FallbackIconStyle() | 35 FallbackIconStyle::FallbackIconStyle() |
| 27 : background_color(kDefaultBackgroundColor), | 36 : background_color(kDefaultBackgroundColor), |
| 28 text_color(kDefaultTextColorLight), | 37 text_color(kDefaultTextColorLight), |
| 29 font_size_ratio(kDefaultFontSizeRatio), | 38 font_size_ratio(kDefaultFontSizeRatio), |
| 30 roundness(kDefaultRoundness) { | 39 roundness(kDefaultRoundness) { |
| 31 } | 40 } |
| 32 | 41 |
| 33 FallbackIconStyle::~FallbackIconStyle() { | 42 FallbackIconStyle::~FallbackIconStyle() { |
| 34 } | 43 } |
| 35 | 44 |
| 36 void MatchFallbackIconTextColorAgainstBackgroundColor( | 45 void MatchFallbackIconTextColorAgainstBackgroundColor( |
| 37 FallbackIconStyle* style) { | 46 FallbackIconStyle* style) { |
| 38 int luminance = color_utils::GetLuminanceForColor(style->background_color); | 47 int luminance = color_utils::GetLuminanceForColor(style->background_color); |
| 39 style->text_color = (luminance >= kDarkTextLuminanceThreshold ? | 48 style->text_color = (luminance >= kDarkTextLuminanceThreshold ? |
| 40 kDefaultTextColorDark : kDefaultTextColorLight); | 49 kDefaultTextColorDark : kDefaultTextColorLight); |
| 41 } | 50 } |
| 42 | 51 |
| 43 bool ValidateFallbackIconStyle(const FallbackIconStyle& style) { | 52 bool ValidateFallbackIconStyle(const FallbackIconStyle& style) { |
| 44 return style.font_size_ratio >= 0.0 && style.font_size_ratio <= 1.0 && | 53 return style.font_size_ratio >= 0.0 && style.font_size_ratio <= 1.0 && |
| 45 style.roundness >= 0.0 && style.roundness <= 1.0; | 54 style.roundness >= 0.0 && style.roundness <= 1.0; |
| 46 } | 55 } |
| 47 | 56 |
| 57 void SetDominantColorAsBackground( | |
| 58 const scoped_refptr<base::RefCountedMemory>& bitmap_data, | |
| 59 FallbackIconStyle* style) { | |
| 60 SkColor dominant_color = | |
| 61 color_utils::CalculateKMeanColorOfPNG(bitmap_data); | |
| 62 // Assumes |style.text_color| is light, and clamps luminance down to a | |
| 63 // reasonable maximum value so text is readable. | |
| 64 color_utils::HSL color_hsl; | |
| 65 color_utils::SkColorToHSL(dominant_color, &color_hsl); | |
| 66 color_hsl.l = std::min(color_hsl.l, kMaxBackgroundColorLuminance); | |
| 67 style->background_color = | |
| 68 color_utils::HSLToSkColor(color_hsl, SK_AlphaOPAQUE); | |
| 69 } | |
| 70 | |
|
kmadhusu
2015/04/21 17:00:27
nit: Please remove this blank line.
beaudoin
2015/04/21 18:09:54
Done.
| |
| 71 | |
| 48 } // namespace favicon_base | 72 } // namespace favicon_base |
| OLD | NEW |