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

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

Issue 835903005: [Favicon] Add new fallback icon rendering flow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactor by extracting FallbackIconSpecsBuilder. 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_specs_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 kMaxCornerRadiusRatio = 0.5;
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 kDefaultCornerRadiusRatio = 0.0625; // 1 / 16.
31
32 } // namespace
33
34 FallbackIconSpecsBuilder::FallbackIconSpecsBuilder()
35 : is_init_(0U) {
36 }
37
38 FallbackIconSpecsBuilder::~FallbackIconSpecsBuilder() {
39 }
40
41 bool FallbackIconSpecsBuilder::Reset() {
42 is_init_ = 0U;
43 specs_ = FallbackIconSpecs();
44 return false; // This is to simplify Parse().
45 }
46
47 bool FallbackIconSpecsBuilder::Has(DataMember mask) const {
48 return (is_init_ & mask) != 0;
49 }
50
51 // Setters, which does not validate |value|.
52 void FallbackIconSpecsBuilder::SetBackgroundColor(SkColor value) {
53 is_init_ |= BACKGROUND_COLOR;
54 specs_.background_color = value;
55 }
56
57 void FallbackIconSpecsBuilder::SetTextColor(SkColor value) {
58 is_init_ |= TEXT_COLOR;
59 specs_.text_color = value;
60 }
61
62 void FallbackIconSpecsBuilder::SetFontSizeRatio(double value) {
63 is_init_ |= FONT_SIZE_RATIO;
64 specs_.font_size_ratio = value;
65 }
66
67 void FallbackIconSpecsBuilder::SetCornerRadiusRatio(double value) {
68 is_init_ |= CORNER_RADIUS_RATIO;
69 specs_.corner_radius_ratio = value;
70 }
71
72 // static
73 bool FallbackIconSpecsBuilder::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) {
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 FallbackIconSpecsBuilder::Parse(const std::string& specs_str) {
105 Reset();
106 std::vector<std::string> tokens;
107 base::SplitStringDontTrim(specs_str, ',', &tokens);
108 if (tokens.size() > 4) // Too many fields.
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();
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 > kMaxCornerRadiusRatio) {
133 return Reset();
134 }
135 SetCornerRadiusRatio(temp_double);
136 }
137 return true;
138 }
139
140 void FallbackIconSpecsBuilder::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(CORNER_RADIUS_RATIO))
155 SetCornerRadiusRatio(kDefaultCornerRadiusRatio);
156 }
157
158 const FallbackIconSpecs& FallbackIconSpecsBuilder::Build() {
159 AssignDefaults();
160 return specs_;
161 }
162
163 } // namespace favicon_base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698