Simplify color parsing and allow more decimals
This commit is contained in:
+13
-20
@@ -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
|
* @const
|
||||||
* @type {RegExp}
|
* @type {RegExp}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ol.color.RGB_COLOR_RE_ =
|
ol.color.RGB_COLOR_RE_ = /^rgb\(/i;
|
||||||
/^(?:rgb)?\((0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2})\)$/i;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Regular expression for matching and capturing RGBA style strings.
|
* Regular expression for matching RGBA style strings.
|
||||||
* @const
|
* @const
|
||||||
* @type {RegExp}
|
* @type {RegExp}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ol.color.RGBA_COLOR_RE_ =
|
ol.color.RGBA_COLOR_RE_ = /^rgba\(/;
|
||||||
/^(?: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;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,8 +37,7 @@ ol.color.RGBA_COLOR_RE_ =
|
|||||||
* @type {RegExp}
|
* @type {RegExp}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ol.color.NAMED_COLOR_RE_ =
|
ol.color.NAMED_COLOR_RE_ = /^([a-z]*)$/i;
|
||||||
/^([a-z]*)$/i;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -151,7 +148,7 @@ ol.color.fromString = (
|
|||||||
* @return {ol.Color} Color.
|
* @return {ol.Color} Color.
|
||||||
*/
|
*/
|
||||||
ol.color.fromStringInternal_ = function(s) {
|
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)) {
|
if (ol.color.NAMED_COLOR_RE_.exec(s)) {
|
||||||
s = ol.color.fromNamed(s);
|
s = ol.color.fromNamed(s);
|
||||||
@@ -171,17 +168,13 @@ ol.color.fromStringInternal_ = function(s) {
|
|||||||
}
|
}
|
||||||
a = 1;
|
a = 1;
|
||||||
color = [r, g, b, a];
|
color = [r, g, b, a];
|
||||||
} else if ((match = ol.color.RGBA_COLOR_RE_.exec(s))) { // rgba()
|
} else if (ol.color.RGBA_COLOR_RE_.test(s)) { // rgba()
|
||||||
r = Number(match[1]);
|
parts = s.slice(5, -1).split(',').map(Number);
|
||||||
g = Number(match[2]);
|
color = ol.color.normalize(parts);
|
||||||
b = Number(match[3]);
|
} else if (ol.color.RGB_COLOR_RE_.test(s)) { // rgb()
|
||||||
a = Number(match[4]);
|
parts = s.slice(4, -1).split(',').map(Number);
|
||||||
color = ol.color.normalize([r, g, b, a]);
|
parts.push(1);
|
||||||
} else if ((match = ol.color.RGB_COLOR_RE_.exec(s))) { // rgb()
|
color = ol.color.normalize(parts);
|
||||||
r = Number(match[1]);
|
|
||||||
g = Number(match[2]);
|
|
||||||
b = Number(match[3]);
|
|
||||||
color = ol.color.normalize([r, g, b, 1]);
|
|
||||||
} else {
|
} else {
|
||||||
ol.asserts.assert(false, 14); // Invalid color
|
ol.asserts.assert(false, 14); // Invalid color
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,11 +78,44 @@ describe('ol.color', function() {
|
|||||||
expect(ol.color.fromString('rgb(0, 0, 255)')).to.eql([0, 0, 255, 1]);
|
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() {
|
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(
|
expect(ol.color.fromString('rgba(255, 255, 0, 0.1)')).to.eql(
|
||||||
[255, 255, 0, 0.1]);
|
[255, 255, 0, 0.1]);
|
||||||
expect(ol.color.fromString('rgba(255, 255, 0, 0.3333333333333333)')).to.eql(
|
// opacity 0.1111111111111111 (float with 16 digits)
|
||||||
[255, 255, 0, 0.3333333333333333]);
|
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() {
|
it('caches parsed values', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user