Revert of Refactor the avatar button/icon class (patchset #14 id:320001 of https...
[chromium-blink-merge.git] / components / favicon_base / fallback_icon_style.cc
bloba3aa658cad2bc12b42426521faf1208babf5f655
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
3 // found in the LICENSE file.
5 #include "components/favicon_base/fallback_icon_style.h"
7 #include <algorithm>
9 #include "ui/gfx/color_analysis.h"
10 #include "ui/gfx/color_utils.h"
12 namespace favicon_base {
14 namespace {
16 // Luminance threshold for background color determine whether to use dark or
17 // light text color.
18 const int kDarkTextLuminanceThreshold = 190;
20 // The maximum luminance of the background color to ensure light text is
21 // readable.
22 const double kMaxBackgroundColorLuminance = 0.67;
24 // Default values for FallbackIconStyle.
25 const SkColor kDefaultBackgroundColor = SkColorSetRGB(0x78, 0x78, 0x78);
26 const SkColor kDefaultTextColorDark = SK_ColorBLACK;
27 const SkColor kDefaultTextColorLight = SK_ColorWHITE;
28 const double kDefaultFontSizeRatio = 0.44;
29 const double kDefaultRoundness = 0; // Square. Round corners are applied
30 // externally (Javascript or Java).
32 } // namespace
34 FallbackIconStyle::FallbackIconStyle()
35 : background_color(kDefaultBackgroundColor),
36 text_color(kDefaultTextColorLight),
37 font_size_ratio(kDefaultFontSizeRatio),
38 roundness(kDefaultRoundness) {
41 FallbackIconStyle::~FallbackIconStyle() {
44 bool FallbackIconStyle::operator==(const FallbackIconStyle& other) const {
45 return background_color == other.background_color &&
46 text_color == other.text_color &&
47 font_size_ratio == other.font_size_ratio &&
48 roundness == other.roundness;
51 void MatchFallbackIconTextColorAgainstBackgroundColor(
52 FallbackIconStyle* style) {
53 int luminance = color_utils::GetLuminanceForColor(style->background_color);
54 style->text_color = (luminance >= kDarkTextLuminanceThreshold ?
55 kDefaultTextColorDark : kDefaultTextColorLight);
58 bool ValidateFallbackIconStyle(const FallbackIconStyle& style) {
59 return style.font_size_ratio >= 0.0 && style.font_size_ratio <= 1.0 &&
60 style.roundness >= 0.0 && style.roundness <= 1.0;
63 void SetDominantColorAsBackground(
64 const scoped_refptr<base::RefCountedMemory>& bitmap_data,
65 FallbackIconStyle* style) {
66 SkColor dominant_color =
67 color_utils::CalculateKMeanColorOfPNG(bitmap_data);
68 // Assumes |style.text_color| is light, and clamps luminance down to a
69 // reasonable maximum value so text is readable.
70 color_utils::HSL color_hsl;
71 color_utils::SkColorToHSL(dominant_color, &color_hsl);
72 color_hsl.l = std::min(color_hsl.l, kMaxBackgroundColorLuminance);
73 style->background_color =
74 color_utils::HSLToSkColor(color_hsl, SK_AlphaOPAQUE);
77 } // namespace favicon_base