Skip to content

Commit

Permalink
refactor: simple getBackgroundColor
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Mar 3, 2022
1 parent 6dae2a9 commit 7186b9d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 62 deletions.
4 changes: 2 additions & 2 deletions docs/api/browser-view.md
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions docs/api/browser-window.md
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions shell/browser/api/electron_api_base_window.cc
Expand Up @@ -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) {
Expand Down
60 changes: 26 additions & 34 deletions shell/common/color_util.cc
Expand Up @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions shell/common/color_util.h
Expand Up @@ -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);
Expand Down
20 changes: 4 additions & 16 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -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%)');
});
});

Expand Down

0 comments on commit 7186b9d

Please sign in to comment.