Skip to content

Commit e6d55d8

Browse files
pranav1344zbjornson
authored andcommitted
Add fix for alpha '%' parsing
1 parent 7726ea5 commit e6d55d8

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/color.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,16 @@ parse_clipped_percentage(const char** pStr, float *pFraction) {
226226

227227
#define ALPHA(NAME) \
228228
if (*str >= '1' && *str <= '9') { \
229-
NAME = 1; \
229+
NAME = 0; \
230+
float n = .1f; \
231+
while(*str >='0' && *str <= '9') { \
232+
NAME += (*str - '0') * n; \
233+
str++; \
234+
} \
235+
while(*str == ' ')str++; \
236+
if(*str != '%') { \
237+
NAME = 1; \
238+
} \
230239
} else { \
231240
if ('0' == *str) { \
232241
NAME = 0; \

test/canvas.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,17 @@ describe('Canvas', function () {
213213
ctx.fillStyle = 'rgba(124, 58, 26, 0)';
214214
assert.equal('rgba(124, 58, 26, 0.00)', ctx.fillStyle);
215215

216+
ctx.fillStyle = 'rgba( 255, 200, 90, 40%)'
217+
assert.equal('rgba(255, 200, 90, 0.40)', ctx.fillStyle)
218+
219+
ctx.fillStyle = 'rgba( 255, 200, 90, 50 %)'
220+
assert.equal('rgba(255, 200, 90, 0.50)', ctx.fillStyle)
221+
222+
ctx.fillStyle = 'rgba( 255, 200, 90, 10%)'
223+
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)
224+
225+
ctx.fillStyle = 'rgba( 255, 200, 90, 10 %)'
226+
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)
216227
// hsl / hsla tests
217228

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

0 commit comments

Comments
 (0)