From 02d41caa8afaa89434d62cd40e9c586ec8d75b59 Mon Sep 17 00:00:00 2001 From: Marc Jansen Date: Wed, 23 Nov 2016 16:06:08 +0100 Subject: [PATCH 1/2] Simplify color parsing and allow more decimals --- src/ol/color.js | 33 +++++++++++++-------------------- test/spec/ol/color.test.js | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/ol/color.js b/src/ol/color.js index 0bbe0c06eb..1fc83c33c2 100644 --- a/src/ol/color.js +++ b/src/ol/color.js @@ -14,23 +14,21 @@ ol.color.HEX_COLOR_RE_ = /^#(?:[0-9a-f]{3}){1,2}$/i; /** - * Regular expression for matching and capturing RGB style strings. + * Regular expression for matching RGB style strings. * @const * @type {RegExp} * @private */ -ol.color.RGB_COLOR_RE_ = - /^(?:rgb)?\((0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2})\)$/i; +ol.color.RGB_COLOR_RE_ = /^rgb\(/i; /** - * Regular expression for matching and capturing RGBA style strings. + * Regular expression for matching RGBA style strings. * @const * @type {RegExp} * @private */ -ol.color.RGBA_COLOR_RE_ = - /^(?:rgba)?\((0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2}),\s?(0|1|0\.\d{0,16})\)$/i; +ol.color.RGBA_COLOR_RE_ = /^rgba\(/; /** @@ -39,8 +37,7 @@ ol.color.RGBA_COLOR_RE_ = * @type {RegExp} * @private */ -ol.color.NAMED_COLOR_RE_ = - /^([a-z]*)$/i; +ol.color.NAMED_COLOR_RE_ = /^([a-z]*)$/i; /** @@ -151,7 +148,7 @@ ol.color.fromString = ( * @return {ol.Color} Color. */ ol.color.fromStringInternal_ = function(s) { - var r, g, b, a, color, match; + var r, g, b, a, color, parts; if (ol.color.NAMED_COLOR_RE_.exec(s)) { s = ol.color.fromNamed(s); @@ -171,17 +168,13 @@ ol.color.fromStringInternal_ = function(s) { } a = 1; color = [r, g, b, a]; - } else if ((match = ol.color.RGBA_COLOR_RE_.exec(s))) { // rgba() - r = Number(match[1]); - g = Number(match[2]); - b = Number(match[3]); - a = Number(match[4]); - color = ol.color.normalize([r, g, b, a]); - } else if ((match = ol.color.RGB_COLOR_RE_.exec(s))) { // rgb() - r = Number(match[1]); - g = Number(match[2]); - b = Number(match[3]); - color = ol.color.normalize([r, g, b, 1]); + } else if (ol.color.RGBA_COLOR_RE_.test(s)) { // rgba() + parts = s.slice(5, -1).split(',').map(Number); + color = ol.color.normalize(parts); + } else if (ol.color.RGB_COLOR_RE_.test(s)) { // rgb() + parts = s.slice(4, -1).split(',').map(Number); + parts.push(1); + color = ol.color.normalize(parts); } else { ol.asserts.assert(false, 14); // Invalid color } diff --git a/test/spec/ol/color.test.js b/test/spec/ol/color.test.js index f8599acbe5..ad7f178552 100644 --- a/test/spec/ol/color.test.js +++ b/test/spec/ol/color.test.js @@ -78,11 +78,44 @@ describe('ol.color', function() { expect(ol.color.fromString('rgb(0, 0, 255)')).to.eql([0, 0, 255, 1]); }); + it('ignores whitespace before, between & after numbers (rgb)', function() { + expect(ol.color.fromString('rgb( \t 0 , 0 \n , 255 )')).to.eql( + [0, 0, 255, 1]); + }); + it('can parse rgba colors', function() { + // opacity 0 + expect(ol.color.fromString('rgba(255, 255, 0, 0)')).to.eql( + [255, 255, 0, 0]); + // opacity 0.0 (simple float) + expect(ol.color.fromString('rgba(255, 255, 0, 0.0)')).to.eql( + [255, 255, 0, 0]); + // opacity 0.0000000000000000 (float with 16 digits) + expect(ol.color.fromString('rgba(255, 255, 0, 0.0000000000000000)')).to.eql( + [255, 255, 0, 0]); + // opacity 0.1 (simple float) expect(ol.color.fromString('rgba(255, 255, 0, 0.1)')).to.eql( [255, 255, 0, 0.1]); - expect(ol.color.fromString('rgba(255, 255, 0, 0.3333333333333333)')).to.eql( - [255, 255, 0, 0.3333333333333333]); + // opacity 0.1111111111111111 (float with 16 digits) + expect(ol.color.fromString('rgba(255, 255, 0, 0.1111111111111111)')).to.eql( + [255, 255, 0, 0.1111111111111111]); + // opacity 1 + expect(ol.color.fromString('rgba(255, 255, 0, 1)')).to.eql( + [255, 255, 0, 1]); + // opacity 1.0 + expect(ol.color.fromString('rgba(255, 255, 0, 1.0)')).to.eql( + [255, 255, 0, 1]); + // opacity 1.0000000000000000 + expect(ol.color.fromString('rgba(255, 255, 0, 1.0000000000000000)')).to.eql( + [255, 255, 0, 1]); + // with 30 decimal digits + expect(ol.color.fromString('rgba(255, 255, 0, 0.123456789012345678901234567890)')).to.eql( + [255, 255, 0, 0.123456789012345678901234567890]); + }); + + it('ignores whitespace before, between & after numbers (rgba)', function() { + expect(ol.color.fromString('rgba( \t 0 , 0 \n , 255 , 0.4711 )')).to.eql( + [0, 0, 255, 0.4711]); }); it('caches parsed values', function() { From 88cfdb435c57c7bd762b446d89e53b2a430688c1 Mon Sep 17 00:00:00 2001 From: Marc Jansen Date: Tue, 29 Nov 2016 10:55:04 +0100 Subject: [PATCH 2/2] Use indexOf instead of RegExp as suggested --- src/ol/color.js | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/src/ol/color.js b/src/ol/color.js index 1fc83c33c2..b9b040740a 100644 --- a/src/ol/color.js +++ b/src/ol/color.js @@ -13,24 +13,6 @@ goog.require('ol.math'); ol.color.HEX_COLOR_RE_ = /^#(?:[0-9a-f]{3}){1,2}$/i; -/** - * Regular expression for matching RGB style strings. - * @const - * @type {RegExp} - * @private - */ -ol.color.RGB_COLOR_RE_ = /^rgb\(/i; - - -/** - * Regular expression for matching RGBA style strings. - * @const - * @type {RegExp} - * @private - */ -ol.color.RGBA_COLOR_RE_ = /^rgba\(/; - - /** * Regular expression for matching potential named color style strings. * @const @@ -168,10 +150,10 @@ ol.color.fromStringInternal_ = function(s) { } a = 1; color = [r, g, b, a]; - } else if (ol.color.RGBA_COLOR_RE_.test(s)) { // rgba() + } else if (s.indexOf('rgba(') == 0) { // rgba() parts = s.slice(5, -1).split(',').map(Number); color = ol.color.normalize(parts); - } else if (ol.color.RGB_COLOR_RE_.test(s)) { // rgb() + } else if (s.indexOf('rgb(') == 0) { // rgb() parts = s.slice(4, -1).split(',').map(Number); parts.push(1); color = ol.color.normalize(parts);