Skip to content

Commit 83a5126

Browse files
authored
Fixes related to parsing of colors in RGB functions (#2398)
* Fix leading whitespace in color string issue * Add parsing support for floating point numbers in rbg function * Update CHANGELOG.md
1 parent f138b3a commit 83a5126

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
1111
### Added
1212
### Fixed
1313

14+
1415
3.0.0
1516
==================
1617

@@ -36,6 +37,8 @@ This release notably changes to using N-API. 🎉
3637
* Fix a potential memory leak. (#2229)
3738
* Fix the wrong type of setTransform
3839
* Fix the improper parsing of rgb functions issue. (#2300)
40+
* Fix issue related to improper parsing of leading and trailing whitespaces in CSS color. (#2301)
41+
* RGB functions should support real numbers now instead of just integers. (#2339)
3942

4043
2.11.2
4144
==================

src/color.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ wrap_float(T value, T limit) {
159159

160160
static bool
161161
parse_rgb_channel(const char** pStr, uint8_t *pChannel) {
162-
int channel;
163-
if (parse_integer(pStr, &channel)) {
162+
float f_channel;
163+
if (parse_css_number(pStr, &f_channel)) {
164+
int channel = (int) ceil(f_channel);
164165
*pChannel = clip(channel, 0, 255);
165166
return true;
166167
}
@@ -739,6 +740,7 @@ rgba_from_hex_string(const char *str, short *ok) {
739740

740741
static int32_t
741742
rgba_from_name_string(const char *str, short *ok) {
743+
WHITESPACE;
742744
std::string lowered(str);
743745
std::transform(lowered.begin(), lowered.end(), lowered.begin(), tolower);
744746
auto color = named_colors.find(lowered);
@@ -765,6 +767,7 @@ rgba_from_name_string(const char *str, short *ok) {
765767

766768
int32_t
767769
rgba_from_string(const char *str, short *ok) {
770+
WHITESPACE;
768771
if ('#' == str[0])
769772
return rgba_from_hex_string(++str, ok);
770773
if (str == strstr(str, "rgba"))

test/canvas.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ describe('Canvas', function () {
163163
ctx.fillStyle = '#FGG'
164164
assert.equal('#ff0000', ctx.fillStyle)
165165

166+
ctx.fillStyle = ' #FCA'
167+
assert.equal('#ffccaa', ctx.fillStyle)
168+
169+
ctx.fillStyle = ' #ffccaa'
170+
assert.equal('#ffccaa', ctx.fillStyle)
171+
172+
166173
ctx.fillStyle = '#fff'
167174
ctx.fillStyle = 'afasdfasdf'
168175
assert.equal('#ffffff', ctx.fillStyle)
@@ -282,7 +289,20 @@ describe('Canvas', function () {
282289
ctx.fillStyle = 'rgb( 255 200 90 0.1)'
283290
assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle)
284291

292+
ctx.fillStyle = ' rgb( 255 100 90 0.1)'
293+
assert.equal('rgba(255, 100, 90, 0.10)', ctx.fillStyle)
294+
295+
ctx.fillStyle = 'rgb(124.00, 58, 26, 0)';
296+
assert.equal('rgba(124, 58, 26, 0.00)', ctx.fillStyle);
297+
298+
ctx.fillStyle = 'rgb( 255, 200.09, 90, 40%)'
299+
assert.equal('rgba(255, 201, 90, 0.40)', ctx.fillStyle)
300+
301+
ctx.fillStyle = 'rgb( 255.00, 199.03, 90, 50 %)'
302+
assert.equal('rgba(255, 200, 90, 0.50)', ctx.fillStyle)
285303

304+
ctx.fillStyle = 'rgb( 255, 300.09, 90, 40%)'
305+
assert.equal('rgba(255, 255, 90, 0.40)', ctx.fillStyle)
286306
// hsl / hsla tests
287307

288308
ctx.fillStyle = 'hsl(0, 0%, 0%)'
@@ -306,6 +326,9 @@ describe('Canvas', function () {
306326
ctx.fillStyle = 'hsl(237, 76%, 25%)'
307327
assert.equal('#0f1470', ctx.fillStyle)
308328

329+
ctx.fillStyle = ' hsl(0, 150%, 150%)'
330+
assert.equal('#ffffff', ctx.fillStyle)
331+
309332
ctx.fillStyle = 'hsl(240, 73%, 25%)'
310333
assert.equal('#11116e', ctx.fillStyle)
311334

0 commit comments

Comments
 (0)