Skip to content

Commit f1be1e1

Browse files
pranav1344chearon
authored andcommitted
Add support for percent values in rgb
Fix parsing for alpha with percent values. Fixes #1391
1 parent fee0478 commit f1be1e1

3 files changed

Lines changed: 65 additions & 22 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ project adheres to [Semantic Versioning](http://semver.org/).
99
==================
1010
### Changed
1111
### Added
12+
* Add support for percentage in rgb and rgba parsing.
1213
### Fixed
1314
* Check a type tag before unwrapping a native object, so a forged prototype chain can no longer crash the process (#2628)
1415
* Load images from Node.js object URLs (#2525)
1516
* `isPointInPath()` now ignores the current transform and no longer counts points that are only inside the stroke (#2634)
1617
* Fix off-by-one RGB values from `getImageData` on semi-transparent pixels (#1933)
1718
* Fix compilation on platforms that use ijg-libjpeg instead of libjpeg-turbo (#2563)
1819
* `actualBoundingBox*` from `measureText()` no longer always include the text origin, so `actualBoundingBoxLeft` can be negative when the ink starts right of the origin (#2620)
20+
* Fix bug with parsing for alpha and percentages.
1921

2022
3.2.3
2123
==================

‎src/color.cc‎

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,45 @@ wrap_float(T value, T limit) {
148148
// return (value % limit + limit) % limit;
149149
// }
150150

151+
/*
152+
* Parse and clip a percentage value. Returns a float in the range [0, 1].
153+
*/
154+
155+
156+
static bool
157+
check_percentage(const char* str) {
158+
while (*str && *str != '%' && *str != ',' && *str != ' ' && *str != '/') ++str;
159+
return *str == '%';
160+
}
161+
162+
static bool
163+
parse_clipped_percentage(const char** pStr, float *pFraction) {
164+
float percentage;
165+
bool result = parse_css_number(pStr,&percentage);
166+
const char*& str = *pStr;
167+
if (result) {
168+
if (*str == '%') {
169+
++str;
170+
*pFraction = clip(percentage, 0.0f, 100.0f) / 100.0f;
171+
return result;
172+
}
173+
}
174+
return false;
175+
}
176+
151177
/*
152178
* Parse color channel value
153179
*/
154180

155181
static bool
156182
parse_rgb_channel(const char** pStr, uint8_t *pChannel) {
157183
float f_channel;
158-
if (parse_css_number(pStr, &f_channel)) {
184+
bool percentage = check_percentage(*pStr);
185+
if(percentage && parse_clipped_percentage(pStr, &f_channel)) {
186+
int channel = (int) ceil(255 * f_channel);
187+
*pChannel = channel;
188+
return true;
189+
} else if (parse_css_number(pStr, &f_channel)) {
159190
int channel = (int) ceil(f_channel);
160191
*pChannel = clip(channel, 0, 255);
161192
return true;
@@ -177,24 +208,6 @@ parse_degrees(const char** pStr, float *pDegrees) {
177208
return false;
178209
}
179210

180-
/*
181-
* Parse and clip a percentage value. Returns a float in the range [0, 1].
182-
*/
183-
184-
static bool
185-
parse_clipped_percentage(const char** pStr, float *pFraction) {
186-
float percentage;
187-
bool result = parse_css_number(pStr,&percentage);
188-
const char*& str = *pStr;
189-
if (result) {
190-
if (*str == '%') {
191-
++str;
192-
*pFraction = clip(percentage, 0.0f, 100.0f) / 100.0f;
193-
return result;
194-
}
195-
}
196-
return false;
197-
}
198211

199212
/*
200213
* Macros to help with parsing inside rgba_from_*_string
@@ -226,13 +239,15 @@ parse_clipped_percentage(const char** pStr, float *pFraction) {
226239
#define ALPHA(NAME) \
227240
if (*str >= '1' && *str <= '9') { \
228241
NAME = 0; \
229-
float n = .1f; \
230242
while(*str >='0' && *str <= '9') { \
231-
NAME += (*str - '0') * n; \
243+
NAME = NAME * 10 + (*str - '0'); \
232244
str++; \
233245
} \
234246
while(*str == ' ')str++; \
235-
if(*str != '%') { \
247+
if(*str == '%') { \
248+
NAME *= 0.01f; \
249+
++str; \
250+
} else { \
236251
NAME = 1; \
237252
} \
238253
} else { \
@@ -248,6 +263,11 @@ parse_clipped_percentage(const char** pStr, float *pFraction) {
248263
NAME += (*str++ - '0') * n; \
249264
n *= .1f; \
250265
} \
266+
while(*str == ' ')str++; \
267+
if(*str == '%') { \
268+
NAME *= 0.01f; \
269+
++str; \
270+
} \
251271
} \
252272
} \
253273
do {} while (0) // require trailing semicolon

‎test/canvas.test.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,27 @@ describe('Canvas', function () {
269269

270270
ctx.fillStyle = 'rgb( 255, 300.09, 90, 40%)'
271271
assert.equal('rgba(255, 255, 90, 0.40)', ctx.fillStyle)
272+
273+
ctx.fillStyle = 'rgb( 255, 100%, 50%, 60%)'
274+
assert.equal('rgba(255, 255, 128, 0.60)', ctx.fillStyle)
275+
276+
ctx.fillStyle = 'rgb( 255, 20%, 90, 70%)'
277+
assert.equal('rgba(255, 51, 90, 0.70)', ctx.fillStyle)
278+
279+
ctx.fillStyle = 'rgb( 100%, 300%, 90, 50%)'
280+
assert.equal('rgba(255, 255, 90, 0.50)', ctx.fillStyle)
281+
282+
ctx.fillStyle = 'rgb( 100%, 300.09, 90%, 0)'
283+
assert.equal('rgba(255, 255, 230, 0.00)', ctx.fillStyle)
284+
285+
ctx.fillStyle = 'rgb( 100%, 99%, 80% 50%)'
286+
assert.equal('rgba(255, 253, 204, 0.50)', ctx.fillStyle)
287+
288+
ctx.fillStyle = 'rgb( 100%, 99.99%, 40% / 50%)'
289+
assert.equal('rgba(255, 255, 102, 0.50)', ctx.fillStyle)
290+
291+
ctx.fillStyle = 'rgb( 100%, 33.33%, 66.66%, 2%)'
292+
assert.equal('rgba(255, 85, 170, 0.02)', ctx.fillStyle)
272293
// hsl / hsla tests
273294

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

0 commit comments

Comments
 (0)