Skip to content

Commit

Permalink
Fix parsing of alpha in RGBA
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav1344 authored and zbjornson committed Jun 23, 2024
1 parent b3ecff1 commit 3e0b75c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/color.cc
Original file line number Diff line number Diff line change
Expand Up @@ -625,13 +625,15 @@ rgba_from_rgb_string(const char *str, short *ok) {
str += 4;
WHITESPACE;
uint8_t r = 0, g = 0, b = 0;
float a=1.f;
CHANNEL(r);
WHITESPACE_OR_COMMA;
CHANNEL(g);
WHITESPACE_OR_COMMA;
CHANNEL(b);
WHITESPACE;
return *ok = 1, rgba_from_rgb(r, g, b);
WHITESPACE_OR_COMMA_OR_SLASH;
ALPHA(a);
return *ok = 1, rgba_from_rgba(r, g, b, (int) (255 * a));
}
return *ok = 0;
}
Expand Down
40 changes: 40 additions & 0 deletions test/canvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,46 @@ describe('Canvas', function () {
ctx.fillStyle = 'rgba( 255 200 90 0.1)'
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)

ctx.fillStyle = 'rgb(0, 0, 0, 42.42)'
assert.equal('#000000', ctx.fillStyle)

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

ctx.fillStyle = 'rgb(124, 58, 26, 0)';
assert.equal('rgba(124, 58, 26, 0.00)', ctx.fillStyle);

ctx.fillStyle = 'rgb( 255, 200, 90, 40%)'
assert.equal('rgba(255, 200, 90, 0.40)', ctx.fillStyle)

ctx.fillStyle = 'rgb( 255, 200, 90, 50 %)'
assert.equal('rgba(255, 200, 90, 0.50)', ctx.fillStyle)

ctx.fillStyle = 'rgb( 255, 200, 90, 10%)'
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)

ctx.fillStyle = 'rgb( 255, 200, 90, 10 %)'
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)

ctx.fillStyle = 'rgb( 255, 200, 90 / 40%)'
assert.equal('rgba(255, 200, 90, 0.40)', ctx.fillStyle)

ctx.fillStyle = 'rgb( 255, 200, 90 / 0.5)'
assert.equal('rgba(255, 200, 90, 0.50)', ctx.fillStyle)

ctx.fillStyle = 'rgb( 255, 200, 90 / 10%)'
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)

ctx.fillStyle = 'rgb( 255, 200, 90 / 0.1)'
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)

ctx.fillStyle = 'rgb( 255 200 90 / 10%)'
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)

ctx.fillStyle = 'rgb( 255 200 90 0.1)'
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)


// hsl / hsla tests

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

0 comments on commit 3e0b75c

Please sign in to comment.