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