Skip to content

Commit 3e0b75c

Browse files
pranav1344zbjornson
authored andcommitted
Fix parsing of alpha in RGBA
1 parent b3ecff1 commit 3e0b75c

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/color.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,13 +625,15 @@ rgba_from_rgb_string(const char *str, short *ok) {
625625
str += 4;
626626
WHITESPACE;
627627
uint8_t r = 0, g = 0, b = 0;
628+
float a=1.f;
628629
CHANNEL(r);
629630
WHITESPACE_OR_COMMA;
630631
CHANNEL(g);
631632
WHITESPACE_OR_COMMA;
632633
CHANNEL(b);
633-
WHITESPACE;
634-
return *ok = 1, rgba_from_rgb(r, g, b);
634+
WHITESPACE_OR_COMMA_OR_SLASH;
635+
ALPHA(a);
636+
return *ok = 1, rgba_from_rgba(r, g, b, (int) (255 * a));
635637
}
636638
return *ok = 0;
637639
}

test/canvas.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,46 @@ describe('Canvas', function () {
243243
ctx.fillStyle = 'rgba( 255 200 90 0.1)'
244244
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)
245245

246+
ctx.fillStyle = 'rgb(0, 0, 0, 42.42)'
247+
assert.equal('#000000', ctx.fillStyle)
248+
249+
ctx.fillStyle = 'rgb(255, 250, 255)';
250+
assert.equal('#fffaff', ctx.fillStyle);
251+
252+
ctx.fillStyle = 'rgb(124, 58, 26, 0)';
253+
assert.equal('rgba(124, 58, 26, 0.00)', ctx.fillStyle);
254+
255+
ctx.fillStyle = 'rgb( 255, 200, 90, 40%)'
256+
assert.equal('rgba(255, 200, 90, 0.40)', ctx.fillStyle)
257+
258+
ctx.fillStyle = 'rgb( 255, 200, 90, 50 %)'
259+
assert.equal('rgba(255, 200, 90, 0.50)', ctx.fillStyle)
260+
261+
ctx.fillStyle = 'rgb( 255, 200, 90, 10%)'
262+
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)
263+
264+
ctx.fillStyle = 'rgb( 255, 200, 90, 10 %)'
265+
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)
266+
267+
ctx.fillStyle = 'rgb( 255, 200, 90 / 40%)'
268+
assert.equal('rgba(255, 200, 90, 0.40)', ctx.fillStyle)
269+
270+
ctx.fillStyle = 'rgb( 255, 200, 90 / 0.5)'
271+
assert.equal('rgba(255, 200, 90, 0.50)', ctx.fillStyle)
272+
273+
ctx.fillStyle = 'rgb( 255, 200, 90 / 10%)'
274+
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)
275+
276+
ctx.fillStyle = 'rgb( 255, 200, 90 / 0.1)'
277+
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)
278+
279+
ctx.fillStyle = 'rgb( 255 200 90 / 10%)'
280+
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)
281+
282+
ctx.fillStyle = 'rgb( 255 200 90 0.1)'
283+
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)
284+
285+
246286
// hsl / hsla tests
247287

248288
ctx.fillStyle = 'hsl(0, 0%, 0%)'

0 commit comments

Comments
 (0)