Skip to content

Commit

Permalink
Parse rgba(r,g,b) correctly
Browse files Browse the repository at this point in the history
`rgb()` and `rgba()` are supposed to have identical grammar and behavior: https://www.w3.org/TR/css-color-4/#rgb-functions.

Fixes #2029
  • Loading branch information
zbjornson committed Jul 7, 2022
1 parent b0d4f44 commit 5255195
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
* Export `pangoVersion`
### Added
### Fixed
* `rgba(r,g,b)` with no alpha should parse as opaque, not transparent. ([#2029](https://github.com/Automattic/node-canvas/issues/2029))

2.9.3
==================
Expand Down
5 changes: 3 additions & 2 deletions src/color.cc
Expand Up @@ -225,12 +225,13 @@ parse_clipped_percentage(const char** pStr, float *pFraction) {
#define LIGHTNESS(NAME) SATURATION(NAME)

#define ALPHA(NAME) \
if (*str >= '1' && *str <= '9') { \
if (*str >= '1' && *str <= '9') { \
NAME = 1; \
} else { \
if ('0' == *str) ++str; \
if ('.' == *str) { \
++str; \
NAME = 0; \
float n = .1f; \
while (*str >= '0' && *str <= '9') { \
NAME += (*str++ - '0') * n; \
Expand Down Expand Up @@ -630,7 +631,7 @@ rgba_from_rgba_string(const char *str, short *ok) {
str += 5;
WHITESPACE;
uint8_t r = 0, g = 0, b = 0;
float a = 0;
float a = 1.f;
CHANNEL(r);
WHITESPACE_OR_COMMA;
CHANNEL(g);
Expand Down
3 changes: 3 additions & 0 deletions test/canvas.test.js
Expand Up @@ -202,6 +202,9 @@ describe('Canvas', function () {
ctx.fillStyle = 'rgba(0, 0, 0, 42.42)'
assert.equal('#000000', ctx.fillStyle)

ctx.fillStyle = 'rgba(255, 250, 255)';
assert.equal('#fffaff', ctx.fillStyle);

// hsl / hsla tests

ctx.fillStyle = 'hsl(0, 0%, 0%)'
Expand Down

0 comments on commit 5255195

Please sign in to comment.