From 92a90adc9edbbfea5239b660b18268b3385b2224 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Mon, 11 Apr 2022 18:57:43 +0200 Subject: [PATCH 1/3] fix: argb to rgba conversion --- shell/common/color_util.cc | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/shell/common/color_util.cc b/shell/common/color_util.cc index afa4c7f7a40bc..6f56f9214244e 100644 --- a/shell/common/color_util.cc +++ b/shell/common/color_util.cc @@ -9,6 +9,7 @@ #include #include "base/cxx17_backports.h" +#include "base/logging.h" #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" #include "content/public/common/color_parser.h" @@ -35,16 +36,11 @@ namespace electron { SkColor ParseCSSColor(const std::string& color_string) { // ParseCssColorString expects RGBA and we historically use ARGB // so we need to convert before passing to ParseCssColorString. - std::string color_str = color_string; - if (IsHexFormat(color_str)) { - if (color_str.length() == 5) { - // #ARGB => #RGBA - std::swap(color_str[1], color_str[4]); - } else { - // #AARRGGBB => #RRGGBBAA - std::swap(color_str[1], color_str[7]); - std::swap(color_str[2], color_str[8]); - } + std::string color_str; + if (IsHexFormat(color_string)) { + std::string temp = color_string; + int len = color_string.length() == 5 ? 1 : 2; + color_str = temp.erase(1, len) + color_string.substr(1, len); } SkColor color; From 166b0747c0a8917050c18c5e35f4d5e6f38016cd Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Mon, 11 Apr 2022 19:02:13 +0200 Subject: [PATCH 2/3] chore: remove logging import --- shell/common/color_util.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/shell/common/color_util.cc b/shell/common/color_util.cc index 6f56f9214244e..1aed97cfb8da5 100644 --- a/shell/common/color_util.cc +++ b/shell/common/color_util.cc @@ -9,7 +9,6 @@ #include #include "base/cxx17_backports.h" -#include "base/logging.h" #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" #include "content/public/common/color_parser.h" @@ -17,7 +16,7 @@ namespace { -bool IsHexFormat(const std::string& str) { +bool IsHexFormatWithAlpha(const std::string& str) { // Must be either #ARGB or #AARRGGBB. bool is_hex_length = str.length() == 5 || str.length() == 9; if (str[0] != '#' || !is_hex_length) @@ -37,10 +36,12 @@ SkColor ParseCSSColor(const std::string& color_string) { // ParseCssColorString expects RGBA and we historically use ARGB // so we need to convert before passing to ParseCssColorString. std::string color_str; - if (IsHexFormat(color_string)) { + if (IsHexFormatWithAlpha(color_string)) { std::string temp = color_string; int len = color_string.length() == 5 ? 1 : 2; color_str = temp.erase(1, len) + color_string.substr(1, len); + } else { + color_str = color_string; } SkColor color; From 252c597bf521e64673011d992913059a4d3f9fab Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Tue, 12 Apr 2022 10:14:57 +0200 Subject: [PATCH 3/3] refactor: color_str -> converted_color_str --- shell/common/color_util.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/shell/common/color_util.cc b/shell/common/color_util.cc index 1aed97cfb8da5..73bbc162cca97 100644 --- a/shell/common/color_util.cc +++ b/shell/common/color_util.cc @@ -35,17 +35,17 @@ namespace electron { SkColor ParseCSSColor(const std::string& color_string) { // ParseCssColorString expects RGBA and we historically use ARGB // so we need to convert before passing to ParseCssColorString. - std::string color_str; + std::string converted_color_str; if (IsHexFormatWithAlpha(color_string)) { std::string temp = color_string; int len = color_string.length() == 5 ? 1 : 2; - color_str = temp.erase(1, len) + color_string.substr(1, len); + converted_color_str = temp.erase(1, len) + color_string.substr(1, len); } else { - color_str = color_string; + converted_color_str = color_string; } SkColor color; - if (!content::ParseCssColorString(color_str, &color)) + if (!content::ParseCssColorString(converted_color_str, &color)) color = SK_ColorWHITE; return color;