Chromium Code Reviews| Index: components/favicon_base/fallback_icon_style_builder.cc |
| diff --git a/components/favicon_base/fallback_icon_style_builder.cc b/components/favicon_base/fallback_icon_style_builder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a605c33781c44cd3bf1aa98ee5a5bfbc20f682e0 |
| --- /dev/null |
| +++ b/components/favicon_base/fallback_icon_style_builder.cc |
| @@ -0,0 +1,163 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/favicon_base/fallback_icon_style_builder.h" |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_split.h" |
| +#include "ui/gfx/color_utils.h" |
| + |
| +namespace favicon_base { |
| + |
| +namespace { |
| + |
| +double kMaxFontSizeRatio = 1.0; |
| +double kMaxRoundness = 1.0; |
| + |
| +// Luminance threshold for background color determine whether to use dark or |
| +// light text color. |
| +int kDarkTextLuminanceThreshold = 190; |
| + |
| +// Default values. |
| +SkColor kDefaultBackgroundColor = SkColorSetRGB(0x80, 0x80, 0x80); |
| +SkColor kDefaultTextColorDark = SK_ColorBLACK; |
| +SkColor kDefaultTextColorLight = SK_ColorWHITE; |
| +double kDefaultFontSizeRatio = 0.8; |
| +double kDefaultRoundness = 0.125; // 1 / 8. |
| + |
| +} // namespace |
| + |
| +FallbackIconStyleBuilder::FallbackIconStyleBuilder() |
| + : is_init_(0U) { |
| +} |
| + |
| +FallbackIconStyleBuilder::~FallbackIconStyleBuilder() { |
| +} |
| + |
| +bool FallbackIconStyleBuilder::Reset() { |
| + is_init_ = 0U; |
| + specs_ = FallbackIconStyle(); |
| + return false; // This is to simplify Parse(). |
| +} |
| + |
| +bool FallbackIconStyleBuilder::Has(DataMember mask) const { |
| + return (is_init_ & mask) != 0; |
| +} |
| + |
| +// Setters, which does not validate |value|. |
| +void FallbackIconStyleBuilder::SetBackgroundColor(SkColor value) { |
| + is_init_ |= BACKGROUND_COLOR; |
| + specs_.background_color = value; |
| +} |
| + |
| +void FallbackIconStyleBuilder::SetTextColor(SkColor value) { |
| + is_init_ |= TEXT_COLOR; |
| + specs_.text_color = value; |
| +} |
| + |
| +void FallbackIconStyleBuilder::SetFontSizeRatio(double value) { |
| + is_init_ |= FONT_SIZE_RATIO; |
| + specs_.font_size_ratio = value; |
| +} |
| + |
| +void FallbackIconStyleBuilder::SetRoundness(double value) { |
| + is_init_ |= ROUNDNESS; |
| + specs_.roundness = value; |
| +} |
| + |
| +// static |
| +bool FallbackIconStyleBuilder::ParseColor(const std::string& str, |
| + SkColor* color) { |
| + size_t len = str.length(); |
| + if (len != 3 && len != 6 && len != 8) |
| + return false; |
| + // Translate and validate each digits. |
| + int d[8]; |
| + 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
|
| + char ch = str[i]; |
| + if (ch >= '0' && ch <= '9') |
| + d[i] = ch - '0'; |
| + else if (ch >= 'A' && ch <= 'F') |
| + d[i] = (ch - 'A') + 10; |
| + else if (ch >= 'a' && ch <= 'f') |
| + d[i] = (ch - 'a') + 10; |
| + else |
| + return false; |
| + } |
| + if (len == 3) { // RGB. |
| + *color = SkColorSetRGB(d[0] * 0x11, d[1] * 0x11, d[2] * 0x11); |
| + } else if (len == 6) { // RRGGBB. |
| + *color = SkColorSetRGB( |
| + (d[0] << 4) | d[1], (d[2] << 4) | d[3], (d[4] << 4) | d[5]); |
| + } else { // RRGGBBAA. |
| + DCHECK(len == 8); |
| + *color = SkColorSetARGB((d[6] << 4) | d[7], (d[0] << 4) | d[1], |
| + (d[2] << 4) | d[3], (d[4] << 4) | d[5]); |
| + } |
| + return true; |
| +} |
| + |
| +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 ",".
|
| + Reset(); |
| + std::vector<std::string> tokens; |
| + base::SplitStringDontTrim(specs_str, ',', &tokens); |
| + 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.
|
| + return Reset(); // Returns false. |
| + |
| + SkColor temp_color; |
| + double temp_double; |
| + if (tokens.size() > 0 && !tokens[0].empty()) { |
| + if (!ParseColor(tokens[0], &temp_color)) |
| + 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
|
| + SetBackgroundColor(temp_color); |
| + } |
| + if (tokens.size() > 1 && !tokens[1].empty()) { |
| + if (!ParseColor(tokens[1], &temp_color)) |
| + return Reset(); |
| + SetTextColor(temp_color); |
| + } |
| + if (tokens.size() > 2 && !tokens[2].empty()) { |
| + if (!base::StringToDouble(tokens[2], &temp_double) || |
| + temp_double < 0.0 || temp_double > kMaxFontSizeRatio) { |
| + return Reset(); |
| + } |
| + SetFontSizeRatio(temp_double); |
| + } |
| + if (tokens.size() > 3 && !tokens[3].empty()) { |
| + if (!base::StringToDouble(tokens[3], &temp_double) || |
| + temp_double < 0.0 || temp_double > kMaxRoundness) { |
| + return Reset(); |
| + } |
| + SetRoundness(temp_double); |
| + } |
| + return true; |
| +} |
| + |
| +void FallbackIconStyleBuilder::AssignDefaults() { |
| + if (!Has(BACKGROUND_COLOR)) |
| + SetBackgroundColor(kDefaultBackgroundColor); |
| + |
| + // Uses dark/light text for background with high/low luminance. |
| + if (!Has(TEXT_COLOR)) { |
| + int luminance = color_utils::GetLuminanceForColor(specs_.background_color); |
| + SetTextColor(luminance >= kDarkTextLuminanceThreshold ? |
| + kDefaultTextColorDark : kDefaultTextColorLight); |
| + } |
| + |
| + if (!Has(FONT_SIZE_RATIO)) |
| + SetFontSizeRatio(kDefaultFontSizeRatio); |
| + |
| + if (!Has(ROUNDNESS)) |
| + SetRoundness(kDefaultRoundness); |
| +} |
| + |
| +const FallbackIconStyle& FallbackIconStyleBuilder::Build() { |
| + AssignDefaults(); |
| + return specs_; |
| +} |
| + |
| +} // namespace favicon_base |