Skip to content

Commit

Permalink
feat: support more formats in getBackgroundColor
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Jan 17, 2022
1 parent e504a8d commit bf2c82d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
12 changes: 7 additions & 5 deletions docs/api/browser-window.md
Expand Up @@ -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_

Expand Down Expand Up @@ -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])`

Expand Down
7 changes: 5 additions & 2 deletions shell/browser/api/electron_api_base_window.cc
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion shell/browser/api/electron_api_base_window.h
Expand Up @@ -159,7 +159,7 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
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);
Expand Down
20 changes: 20 additions & 0 deletions shell/common/color_util.cc
Expand Up @@ -6,13 +6,33 @@

#include <vector>

#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))
Expand Down
2 changes: 2 additions & 0 deletions shell/common/color_util.h
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion spec-main/api-browser-window-spec.ts
Expand Up @@ -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%)');
});
});

Expand Down

0 comments on commit bf2c82d

Please sign in to comment.