From 7186b9d64c1b4cb6555a9c67903e8090c0cf59c2 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 3 Mar 2022 11:40:21 -0800 Subject: [PATCH] refactor: simple getBackgroundColor --- docs/api/browser-view.md | 4 +- docs/api/browser-window.md | 6 +- shell/browser/api/electron_api_base_window.cc | 5 +- shell/common/color_util.cc | 60 ++++++++----------- shell/common/color_util.h | 3 - spec-main/api-browser-window-spec.ts | 20 ++----- 6 files changed, 36 insertions(+), 62 deletions(-) diff --git a/docs/api/browser-view.md b/docs/api/browser-view.md index db1fbd757e0d6..df5a2ce1114e8 100644 --- a/docs/api/browser-view.md +++ b/docs/api/browser-view.md @@ -77,9 +77,9 @@ Examples of valid `color` values: * Hex * #fff (shorthand RGB) - * #ffff (shorthand RGBA) + * #ffff (shorthand ARGB) * #ffffff (RGB) - * #ffffffff (RGBA) + * #ffffffff (ARGB) * RGB * rgb\(([\d]+),\s*([\d]+),\s*([\d]+)\) * e.g. rgb(255, 255, 255) diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 5bd17965f5919..9c2f7984b7302 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -211,7 +211,7 @@ It creates a new `BrowserWindow` with native properties as set by the `options`. * `enableLargerThanScreen` boolean (optional) - Enable the window to be resized larger than screen. Only relevant for macOS, as other OSes allow larger-than-screen windows by default. Default is `false`. - * `backgroundColor` string (optional) - The window's background color in Hex, RGB, RGBA, HSL, HSLA or named CSS color format. Alpha in #RRGGBBAA format is supported if `transparent` is set to `true`. Default is `#FFF` (white). See [win.setBackgroundColor](browser-window.md#winsetbackgroundcolorbackgroundcolor) for more information. + * `backgroundColor` string (optional) - The window's background color in Hex, RGB, RGBA, HSL, HSLA or named CSS color format. Alpha in #AARRGGBB format is supported if `transparent` is set to `true`. Default is `#FFF` (white). See [win.setBackgroundColor](browser-window.md#winsetbackgroundcolorbackgroundcolor) for more information. * `hasShadow` boolean (optional) - Whether window should have a shadow. Default is `true`. * `opacity` number (optional) - Set the initial opacity of the window, between 0.0 (fully transparent) and 1.0 (fully opaque). This is only implemented on Windows and macOS. @@ -1008,9 +1008,9 @@ Examples of valid `backgroundColor` values: * Hex * #fff (shorthand RGB) - * #ffff (shorthand RGBA) + * #ffff (shorthand ARGB) * #ffffff (RGB) - * #ffffffff (RGBA) + * #ffffffff (ARGB) * RGB * rgb\(([\d]+),\s*([\d]+),\s*([\d]+)\) * e.g. rgb(255, 255, 255) diff --git a/shell/browser/api/electron_api_base_window.cc b/shell/browser/api/electron_api_base_window.cc index 42a09239e05bb..3024eba2098ec 100644 --- a/shell/browser/api/electron_api_base_window.cc +++ b/shell/browser/api/electron_api_base_window.cc @@ -648,10 +648,7 @@ void BaseWindow::SetBackgroundColor(const std::string& color_name) { } std::string BaseWindow::GetBackgroundColor(gin_helper::Arguments* args) { - std::string format; - args->GetNext(&format); - - return SkColorToColorString(window_->GetBackgroundColor(), format); + return ToRGBHex(window_->GetBackgroundColor()); } void BaseWindow::SetHasShadow(bool has_shadow) { diff --git a/shell/common/color_util.cc b/shell/common/color_util.cc index d029547d60a61..53a37c848ba64 100644 --- a/shell/common/color_util.cc +++ b/shell/common/color_util.cc @@ -13,49 +13,41 @@ #include "content/public/common/color_parser.h" #include "ui/gfx/color_utils.h" -namespace electron { +namespace { -std::string SkColorToColorString(SkColor color, const std::string& format) { - const double alpha_double = double(SkColorGetA(color)) / 255.0; - const double alpha = base::clamp(alpha_double, 0.0, 1.0); +bool IsHexFormat(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) + return false; - if (format == "hsl" || format == "hsla") { - color_utils::HSL hsl; + if (!std::all_of(str.begin() + 1, str.end(), ::isxdigit)) + return false; - // Hue ranges between 0 - 360, and saturation/lightness both range from 0 - - // 100%, so multiply appropriately to convert to correct int values. - color_utils::SkColorToHSL(color, &hsl); - if (format == "hsla") { - return base::StringPrintf("hsl(%ld, %ld%%, %ld%%, %.1f)", - lround(hsl.h * 360), lround(hsl.s * 100), - lround(hsl.l * 100), alpha); - } else { - return base::StringPrintf("hsl(%ld, %ld%%, %ld%%)", lround(hsl.h * 360), - lround(hsl.s * 100), lround(hsl.l * 100)); - } - } + return true; +} - if (format == "rgba") { - return base::StringPrintf("rgba(%d, %d, %d, %.1f)", SkColorGetR(color), - SkColorGetG(color), SkColorGetB(color), alpha); - } +} // namespace - if (format == "rgb") { - return base::StringPrintf("rgb(%d, %d, %d)", SkColorGetR(color), - SkColorGetG(color), SkColorGetB(color)); - } +namespace electron { - if (alpha != 1.0) { - return ToRGBAHex(color, true); +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]); + } } - // Return hex by default. - return ToRGBHex(color); -} - -SkColor ParseCSSColor(const std::string& color_string) { SkColor color; - if (!content::ParseCssColorString(color_string, &color)) + if (!content::ParseCssColorString(color_str, &color)) color = SK_ColorWHITE; return color; diff --git a/shell/common/color_util.h b/shell/common/color_util.h index 95b5e2de6f055..9bcceef2fbd3a 100644 --- a/shell/common/color_util.h +++ b/shell/common/color_util.h @@ -11,9 +11,6 @@ namespace electron { -// Converts an SKColor to either Hex, HSL, HSLA, RBG, or RGBA color string. -std::string SkColorToColorString(SkColor color, const std::string& format); - // Parses a CSS-style color string from hex, rgb(), rgba(), // hsl(), hsla(), or color name formats. SkColor ParseCSSColor(const std::string& color_string); diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 2bd74ad795862..5abfa6e54ad0f 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -1080,32 +1080,20 @@ describe('BrowserWindow module', () => { w.destroy(); w = new BrowserWindow({}); + w.setBackgroundColor('#AABBFF'); + expect(w.getBackgroundColor()).to.equal('#AABBFF'); + w.setBackgroundColor('blueviolet'); expect(w.getBackgroundColor()).to.equal('#8A2BE2'); - expect(w.getBackgroundColor('rgb')).to.equal('rgb(138, 43, 226)'); - expect(w.getBackgroundColor('rgba')).to.equal('rgba(138, 43, 226, 1.0)'); - expect(w.getBackgroundColor('hsl')).to.equal('hsl(271, 76%, 53%)'); - expect(w.getBackgroundColor('hsla')).to.equal('hsl(271, 76%, 53%, 1.0)'); w.setBackgroundColor('rgb(255, 0, 185)'); expect(w.getBackgroundColor('hex')).to.equal('#FF00B9'); - expect(w.getBackgroundColor('hsl')).to.equal('hsl(316, 100%, 50%)'); w.setBackgroundColor('rgba(245, 40, 145, 0.8)'); - expect(w.getBackgroundColor('hex')).to.equal('#F52891CC'); - expect(w.getBackgroundColor('hsl')).to.equal('hsl(329, 91%, 56%)'); - expect(w.getBackgroundColor('hsla')).to.equal('hsl(329, 91%, 56%, 0.8)'); - - w.setBackgroundColor('#23853466'); - expect(w.getBackgroundColor('rgb')).to.equal('rgb(35, 133, 52)'); - expect(w.getBackgroundColor('rgba')).to.equal('rgba(35, 133, 52, 0.4)'); - expect(w.getBackgroundColor('hsl')).to.equal('hsl(130, 58%, 33%)'); - expect(w.getBackgroundColor('hsla')).to.equal('hsl(130, 58%, 33%, 0.4)'); + expect(w.getBackgroundColor('hex')).to.equal('#F52891'); w.setBackgroundColor('hsl(155, 100%, 50%)'); expect(w.getBackgroundColor()).to.equal('#00FF95'); - expect(w.getBackgroundColor('rgb')).to.equal('rgb(0, 255, 149)'); - expect(w.getBackgroundColor('hsl')).to.equal('hsl(155, 100%, 50%)'); }); });