Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: components/favicon_base/fallback_icon_style_builder.cc

Issue 835903005: [Favicon] Add new fallback icon rendering flow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adding new host chrome://fallback-icon. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "components/favicon_base/fallback_icon_style_builder.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
12 #include "ui/gfx/color_utils.h"
13
14 namespace favicon_base {
15
16 namespace {
17
18 double kMaxFontSizeRatio = 1.0;
19 double kMaxRoundness = 1.0;
20
21 // Luminance threshold for background color determine whether to use dark or
22 // light text color.
23 int kDarkTextLuminanceThreshold = 190;
24
25 // Default values.
26 SkColor kDefaultBackgroundColor = SkColorSetRGB(0x80, 0x80, 0x80);
27 SkColor kDefaultTextColorDark = SK_ColorBLACK;
28 SkColor kDefaultTextColorLight = SK_ColorWHITE;
29 double kDefaultFontSizeRatio = 0.8;
30 double kDefaultRoundness = 0.125; // 1 / 8.
31
32 } // namespace
33
34 FallbackIconStyleBuilder::FallbackIconStyleBuilder()
35 : is_init_(0U) {
36 }
37
38 FallbackIconStyleBuilder::~FallbackIconStyleBuilder() {
39 }
40
41 bool FallbackIconStyleBuilder::Reset() {
42 is_init_ = 0U;
43 specs_ = FallbackIconStyle();
44 return false; // This is to simplify Parse().
45 }
46
47 bool FallbackIconStyleBuilder::Has(DataMember mask) const {
48 return (is_init_ & mask) != 0;
49 }
50
51 // Setters, which does not validate |value|.
52 void FallbackIconStyleBuilder::SetBackgroundColor(SkColor value) {
53 is_init_ |= BACKGROUND_COLOR;
54 specs_.background_color = value;
55 }
56
57 void FallbackIconStyleBuilder::SetTextColor(SkColor value) {
58 is_init_ |= TEXT_COLOR;
59 specs_.text_color = value;
60 }
61
62 void FallbackIconStyleBuilder::SetFontSizeRatio(double value) {
63 is_init_ |= FONT_SIZE_RATIO;
64 specs_.font_size_ratio = value;
65 }
66
67 void FallbackIconStyleBuilder::SetRoundness(double value) {
68 is_init_ |= ROUNDNESS;
69 specs_.roundness = value;
70 }
71
72 // static
73 bool FallbackIconStyleBuilder::ParseColor(const std::string& str,
74 SkColor* color) {
75 size_t len = str.length();
76 if (len != 3 && len != 6 && len != 8)
77 return false;
78 // Translate and validate each digits.
79 int d[8];
80 for (size_t i = 0; i < len; ++i) {
pkotwicz 2015/01/21 19:44:15 Nit: can you use base::HexStringToInt() ?
huangs 2015/01/22 01:13:28 Note that I wish to avoid memory allocation in thi
81 char ch = str[i];
82 if (ch >= '0' && ch <= '9')
83 d[i] = ch - '0';
84 else if (ch >= 'A' && ch <= 'F')
85 d[i] = (ch - 'A') + 10;
86 else if (ch >= 'a' && ch <= 'f')
87 d[i] = (ch - 'a') + 10;
88 else
89 return false;
90 }
91 if (len == 3) { // RGB.
92 *color = SkColorSetRGB(d[0] * 0x11, d[1] * 0x11, d[2] * 0x11);
93 } else if (len == 6) { // RRGGBB.
94 *color = SkColorSetRGB(
95 (d[0] << 4) | d[1], (d[2] << 4) | d[3], (d[4] << 4) | d[5]);
96 } else { // RRGGBBAA.
97 DCHECK(len == 8);
98 *color = SkColorSetARGB((d[6] << 4) | d[7], (d[0] << 4) | d[1],
99 (d[2] << 4) | d[3], (d[4] << 4) | d[5]);
100 }
101 return true;
102 }
103
104 bool FallbackIconStyleBuilder::Parse(const std::string& specs_str) {
pkotwicz 2015/01/21 19:44:15 Can this method return a FallbackIconStyle? (And c
huangs 2015/01/22 01:13:28 I consider Parse() to be a sibling of SetBackgroun
pkotwicz 2015/01/22 16:14:20 Ok, I am fine with allowing empty arguments
huangs 2015/01/22 22:51:39 Done, how we always have ",".
105 Reset();
106 std::vector<std::string> tokens;
107 base::SplitStringDontTrim(specs_str, ',', &tokens);
108 if (tokens.size() > 4) // Too many fields.
pkotwicz 2015/01/21 19:44:15 I think that it makes more sense to make all of th
huangs 2015/01/22 01:13:28 As mentioned above, Parse() can still leave blanks
pkotwicz 2015/01/22 16:14:20 I think we can make all of the fields mandatory an
huangs 2015/01/22 22:51:39 Done.
109 return Reset(); // Returns false.
110
111 SkColor temp_color;
112 double temp_double;
113 if (tokens.size() > 0 && !tokens[0].empty()) {
114 if (!ParseColor(tokens[0], &temp_color))
115 return Reset();
pkotwicz 2015/01/21 19:44:16 How about leaving the background color as the defa
huangs 2015/01/22 01:13:28 Note we impose strict limit for FontSizeRatio and
116 SetBackgroundColor(temp_color);
117 }
118 if (tokens.size() > 1 && !tokens[1].empty()) {
119 if (!ParseColor(tokens[1], &temp_color))
120 return Reset();
121 SetTextColor(temp_color);
122 }
123 if (tokens.size() > 2 && !tokens[2].empty()) {
124 if (!base::StringToDouble(tokens[2], &temp_double) ||
125 temp_double < 0.0 || temp_double > kMaxFontSizeRatio) {
126 return Reset();
127 }
128 SetFontSizeRatio(temp_double);
129 }
130 if (tokens.size() > 3 && !tokens[3].empty()) {
131 if (!base::StringToDouble(tokens[3], &temp_double) ||
132 temp_double < 0.0 || temp_double > kMaxRoundness) {
133 return Reset();
134 }
135 SetRoundness(temp_double);
136 }
137 return true;
138 }
139
140 void FallbackIconStyleBuilder::AssignDefaults() {
141 if (!Has(BACKGROUND_COLOR))
142 SetBackgroundColor(kDefaultBackgroundColor);
143
144 // Uses dark/light text for background with high/low luminance.
145 if (!Has(TEXT_COLOR)) {
146 int luminance = color_utils::GetLuminanceForColor(specs_.background_color);
147 SetTextColor(luminance >= kDarkTextLuminanceThreshold ?
148 kDefaultTextColorDark : kDefaultTextColorLight);
149 }
150
151 if (!Has(FONT_SIZE_RATIO))
152 SetFontSizeRatio(kDefaultFontSizeRatio);
153
154 if (!Has(ROUNDNESS))
155 SetRoundness(kDefaultRoundness);
156 }
157
158 const FallbackIconStyle& FallbackIconStyleBuilder::Build() {
159 AssignDefaults();
160 return specs_;
161 }
162
163 } // namespace favicon_base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698