| 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> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ui/gfx/color_analysis.h" | 9 #include "ui/gfx/color_analysis.h" |
| 10 #include "ui/gfx/color_utils.h" | 10 #include "ui/gfx/color_utils.h" |
| 11 | 11 |
| 12 namespace favicon_base { | 12 namespace favicon_base { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 // Luma threshold for background color determine whether to use dark or light | |
| 17 // text color. | |
| 18 const uint8_t kDarkTextLumaThreshold = 190; | |
| 19 | |
| 20 // The maximum lightness of the background color to ensure light text is | 16 // The maximum lightness of the background color to ensure light text is |
| 21 // readable. | 17 // readable. |
| 22 const double kMaxBackgroundColorLightness = 0.67; | 18 const double kMaxBackgroundColorLightness = 0.67; |
| 23 const double kMinBackgroundColorLightness = 0.15; | 19 const double kMinBackgroundColorLightness = 0.15; |
| 24 | 20 |
| 25 // Default values for FallbackIconStyle. | 21 // Default values for FallbackIconStyle. |
| 26 const SkColor kDefaultBackgroundColor = SkColorSetRGB(0x78, 0x78, 0x78); | 22 const SkColor kDefaultBackgroundColor = SkColorSetRGB(0x78, 0x78, 0x78); |
| 27 const SkColor kDefaultTextColorDark = SK_ColorBLACK; | 23 const SkColor kDefaultTextColor = SK_ColorWHITE; |
| 28 const SkColor kDefaultTextColorLight = SK_ColorWHITE; | |
| 29 const double kDefaultFontSizeRatio = 0.44; | |
| 30 const double kDefaultRoundness = 0; // Square. Round corners are applied | |
| 31 // externally (Javascript or Java). | |
| 32 | 24 |
| 33 } // namespace | 25 } // namespace |
| 34 | 26 |
| 35 FallbackIconStyle::FallbackIconStyle() | 27 FallbackIconStyle::FallbackIconStyle() |
| 36 : background_color(kDefaultBackgroundColor), | 28 : background_color(kDefaultBackgroundColor), |
| 37 is_default_background_color(true), | 29 is_default_background_color(true), |
| 38 text_color(kDefaultTextColorLight), | 30 text_color(kDefaultTextColor) {} |
| 39 font_size_ratio(kDefaultFontSizeRatio), | |
| 40 roundness(kDefaultRoundness) {} | |
| 41 | 31 |
| 42 FallbackIconStyle::~FallbackIconStyle() { | 32 FallbackIconStyle::~FallbackIconStyle() { |
| 43 } | 33 } |
| 44 | 34 |
| 45 bool FallbackIconStyle::operator==(const FallbackIconStyle& other) const { | 35 bool FallbackIconStyle::operator==(const FallbackIconStyle& other) const { |
| 46 return background_color == other.background_color && | 36 return background_color == other.background_color && |
| 47 is_default_background_color == other.is_default_background_color && | 37 is_default_background_color == other.is_default_background_color && |
| 48 text_color == other.text_color && | 38 text_color == other.text_color; |
| 49 font_size_ratio == other.font_size_ratio && | |
| 50 roundness == other.roundness; | |
| 51 } | |
| 52 | |
| 53 void MatchFallbackIconTextColorAgainstBackgroundColor( | |
| 54 FallbackIconStyle* style) { | |
| 55 const uint8_t luma = color_utils::GetLuma(style->background_color); | |
| 56 style->text_color = (luma >= kDarkTextLumaThreshold) ? | |
| 57 kDefaultTextColorDark : kDefaultTextColorLight; | |
| 58 } | |
| 59 | |
| 60 bool ValidateFallbackIconStyle(const FallbackIconStyle& style) { | |
| 61 return style.font_size_ratio >= 0.0 && style.font_size_ratio <= 1.0 && | |
| 62 style.roundness >= 0.0 && style.roundness <= 1.0; | |
| 63 } | 39 } |
| 64 | 40 |
| 65 void SetDominantColorAsBackground( | 41 void SetDominantColorAsBackground( |
| 66 const scoped_refptr<base::RefCountedMemory>& bitmap_data, | 42 const scoped_refptr<base::RefCountedMemory>& bitmap_data, |
| 67 FallbackIconStyle* style) { | 43 FallbackIconStyle* style) { |
| 68 // Try to ensure color's lightness isn't too large so that light text is | 44 // Try to ensure color's lightness isn't too large so that light text is |
| 69 // visible. Set an upper bound for the dominant color. | 45 // visible. Set an upper bound for the dominant color. |
| 70 const color_utils::HSL lower_bound{-1.0, -1.0, kMinBackgroundColorLightness}; | 46 const color_utils::HSL lower_bound{-1.0, -1.0, kMinBackgroundColorLightness}; |
| 71 const color_utils::HSL upper_bound{-1.0, -1.0, kMaxBackgroundColorLightness}; | 47 const color_utils::HSL upper_bound{-1.0, -1.0, kMaxBackgroundColorLightness}; |
| 72 color_utils::GridSampler sampler; | 48 color_utils::GridSampler sampler; |
| 73 SkColor dominant_color = color_utils::CalculateKMeanColorOfPNG( | 49 SkColor dominant_color = color_utils::CalculateKMeanColorOfPNG( |
| 74 bitmap_data, lower_bound, upper_bound, &sampler); | 50 bitmap_data, lower_bound, upper_bound, &sampler); |
| 75 // |CalculateKMeanColorOfPNG| will try to return a color that lies within the | 51 // |CalculateKMeanColorOfPNG| will try to return a color that lies within the |
| 76 // specified bounds if one exists in the image. If there's no such color, it | 52 // specified bounds if one exists in the image. If there's no such color, it |
| 77 // will return the dominant color which may be lighter than our upper bound. | 53 // will return the dominant color which may be lighter than our upper bound. |
| 78 // Clamp lightness down to a reasonable maximum value so text is readable. | 54 // Clamp lightness down to a reasonable maximum value so text is readable. |
| 79 color_utils::HSL color_hsl; | 55 color_utils::HSL color_hsl; |
| 80 color_utils::SkColorToHSL(dominant_color, &color_hsl); | 56 color_utils::SkColorToHSL(dominant_color, &color_hsl); |
| 81 color_hsl.l = std::min(color_hsl.l, kMaxBackgroundColorLightness); | 57 color_hsl.l = std::min(color_hsl.l, kMaxBackgroundColorLightness); |
| 82 style->background_color = | 58 style->background_color = |
| 83 color_utils::HSLToSkColor(color_hsl, SK_AlphaOPAQUE); | 59 color_utils::HSLToSkColor(color_hsl, SK_AlphaOPAQUE); |
| 84 style->is_default_background_color = false; | 60 style->is_default_background_color = false; |
| 85 } | 61 } |
| 86 | 62 |
| 87 } // namespace favicon_base | 63 } // namespace favicon_base |
| OLD | NEW |