diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index b60e1bc8d5bba..da8bf586805ae 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -1008,8 +1008,7 @@ APIs like `win.setSize`. * `backgroundColor` string - Window's background color as a hexadecimal value (`#ff00a3` or `#80FFFFFF`), HSL color value (`hsl(230, 100%, 50%)`), CSS color string (`blueviolet`), or RGB color value (`rgb(255, 145, 145)`). Alpha in #AARRGGBB format is supported if `transparent` is set to `true`). Default is `#FFF` (white). -Sets the background color of the window. See [Setting -`backgroundColor`](#setting-the-backgroundcolor-property). +Sets the background color of the window. See [Setting `backgroundColor`](#setting-the-backgroundcolor-property). #### `win.previewFile(path[, displayName])` _macOS_ @@ -1051,10 +1050,13 @@ console.log(win.getBounds()) Returns [`Rectangle`](structures/rectangle.md) - The `bounds` of the window as `Object`. -#### `win.getBackgroundColor()` +#### `win.getBackgroundColor([format])` -Returns `string` - Gets the background color of the window as a [Hexadecimal value](https://www.w3schools.com/colors/colors_hexadecimal.asp). See [Setting -`backgroundColor`](#setting-the-backgroundcolor-property) for more information. +* `format` string (optional) - One of either `hex`, `rgb`, or `hsl`. + +Returns `string` - Gets the background color of the window as a [Hexadecimal value](https://www.w3schools.com/colors/colors_hexadecimal.asp), an [HSL value](https://www.w3schools.com/colors/colors_hsl.asp) or an [RGB value](https://www.w3schools.com/colors/colors_rgb.asp). Default is `hex` if `format` is not passed. + +See [Setting `backgroundColor`](#setting-the-backgroundcolor-property) for more information. #### `win.setContentBounds(bounds[, animate])` diff --git a/shell/browser/api/electron_api_base_window.cc b/shell/browser/api/electron_api_base_window.cc index f3f000943ea23..52eb8442a9828 100644 --- a/shell/browser/api/electron_api_base_window.cc +++ b/shell/browser/api/electron_api_base_window.cc @@ -647,8 +647,11 @@ void BaseWindow::SetBackgroundColor(const std::string& color_name) { window_->SetBackgroundColor(color); } -std::string BaseWindow::GetBackgroundColor() { - return ToRGBHex(window_->GetBackgroundColor()); +std::string BaseWindow::GetBackgroundColor(gin_helper::Arguments* args) { + std::string format; + args->GetNext(&format); + + return SkColorToColorString(window_->GetBackgroundColor(), format); } void BaseWindow::SetHasShadow(bool has_shadow) { diff --git a/shell/browser/api/electron_api_base_window.h b/shell/browser/api/electron_api_base_window.h index 04ae990ab153a..2ef454a2b55c9 100644 --- a/shell/browser/api/electron_api_base_window.h +++ b/shell/browser/api/electron_api_base_window.h @@ -159,7 +159,7 @@ class BaseWindow : public gin_helper::TrackableObject, bool IsKiosk(); bool IsTabletMode() const; virtual void SetBackgroundColor(const std::string& color_name); - std::string GetBackgroundColor(); + std::string GetBackgroundColor(gin_helper::Arguments* args); void SetHasShadow(bool has_shadow); bool HasShadow(); void SetOpacity(const double opacity); diff --git a/shell/common/color_util.cc b/shell/common/color_util.cc index 4fded267ca8e4..9888f83c93977 100644 --- a/shell/common/color_util.cc +++ b/shell/common/color_util.cc @@ -6,13 +6,33 @@ #include +#include "base/logging.h" #include "base/strings/string_number_conversions.h" #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" #include "content/public/common/color_parser.h" +#include "ui/gfx/color_utils.h" namespace electron { +std::string SkColorToColorString(SkColor color, const std::string& format) { + if (format == "hsl") { + color_utils::HSL hsl; + color_utils::SkColorToHSL(color, &hsl); + // Hue ranges between 0 - 360, and saturation/lightness both range from 0 - + // 100%, so multiply appropriately to convert to correct int values. + return base::StringPrintf("hsl(%ld, %ld%%, %ld%%)", lround(hsl.h * 360), + lround(hsl.s * 100), lround(hsl.l * 100)); + } else if (format == "rgb") { + return base::StringPrintf("rgb(%d, %d, %d)", SkColorGetR(color), + SkColorGetG(color), SkColorGetB(color)); + } + + // Return #AARRGGBB hex by default. + return base::StringPrintf("#%02X%02X%02X", SkColorGetR(color), + SkColorGetG(color), SkColorGetB(color)); +} + SkColor ParseCSSColor(const std::string& color_string) { SkColor color; if (!content::ParseCssColorString(color_string, &color)) diff --git a/shell/common/color_util.h b/shell/common/color_util.h index 9abce0d4dd1e0..2aea6e0f6f4ff 100644 --- a/shell/common/color_util.h +++ b/shell/common/color_util.h @@ -11,6 +11,8 @@ namespace electron { +std::string SkColorToColorString(SkColor color, const std::string& format); + // Parses a CSS-style color string from hex (3- or 6-digit), rgb(), rgba(), // hsl() or hsla() 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 7b84dc6be930a..88f686c972c62 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -1057,12 +1057,17 @@ describe('BrowserWindow module', () => { w.setBackgroundColor('blueviolet'); expect(w.getBackgroundColor()).to.equal('#8A2BE2'); + expect(w.getBackgroundColor('rgb')).to.equal('rgb(138, 43, 226)'); + expect(w.getBackgroundColor('hsl')).to.equal('hsl(271, 76%, 53%)'); w.setBackgroundColor('rgb(255, 0, 185)'); - expect(w.getBackgroundColor()).to.equal('#FF00B9'); + expect(w.getBackgroundColor('hex')).to.equal('#FF00B9'); + expect(w.getBackgroundColor('hsl')).to.equal('hsl(316, 100%, 50%)'); 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%)'); }); });