1616
1717namespace {
1818
19- bool IsHexFormat (const std::string& str) {
19+ bool IsHexFormatWithAlpha (const std::string& str) {
2020 // Must be either #ARGB or #AARRGGBB.
2121 bool is_hex_length = str.length () == 5 || str.length () == 9 ;
2222 if (str[0 ] != ' #' || !is_hex_length)
@@ -35,20 +35,17 @@ namespace electron {
3535SkColor ParseCSSColor (const std::string& color_string) {
3636 // ParseCssColorString expects RGBA and we historically use ARGB
3737 // so we need to convert before passing to ParseCssColorString.
38- std::string color_str = color_string;
39- if (IsHexFormat (color_str)) {
40- if (color_str.length () == 5 ) {
41- // #ARGB => #RGBA
42- std::swap (color_str[1 ], color_str[4 ]);
43- } else {
44- // #AARRGGBB => #RRGGBBAA
45- std::swap (color_str[1 ], color_str[7 ]);
46- std::swap (color_str[2 ], color_str[8 ]);
47- }
38+ std::string converted_color_str;
39+ if (IsHexFormatWithAlpha (color_string)) {
40+ std::string temp = color_string;
41+ int len = color_string.length () == 5 ? 1 : 2 ;
42+ converted_color_str = temp.erase (1 , len) + color_string.substr (1 , len);
43+ } else {
44+ converted_color_str = color_string;
4845 }
4946
5047 SkColor color;
51- if (!content::ParseCssColorString (color_str , &color))
48+ if (!content::ParseCssColorString (converted_color_str , &color))
5249 color = SK_ColorWHITE;
5350
5451 return color;
0 commit comments