Merge pull request #6164 from marcjansen/color-parsing
Simplify color parsing and allow more decimals
This commit is contained in:
@@ -13,34 +13,13 @@ goog.require('ol.math');
|
||||
ol.color.HEX_COLOR_RE_ = /^#(?:[0-9a-f]{3}){1,2}$/i;
|
||||
|
||||
|
||||
/**
|
||||
* Regular expression for matching and capturing 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;
|
||||
|
||||
|
||||
/**
|
||||
* Regular expression for matching and capturing 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;
|
||||
|
||||
|
||||
/**
|
||||
* Regular expression for matching potential named color style strings.
|
||||
* @const
|
||||
* @type {RegExp}
|
||||
* @private
|
||||
*/
|
||||
ol.color.NAMED_COLOR_RE_ =
|
||||
/^([a-z]*)$/i;
|
||||
ol.color.NAMED_COLOR_RE_ = /^([a-z]*)$/i;
|
||||
|
||||
|
||||
/**
|
||||
@@ -151,7 +130,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 +150,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 (s.indexOf('rgba(') == 0) { // rgba()
|
||||
parts = s.slice(5, -1).split(',').map(Number);
|
||||
color = ol.color.normalize(parts);
|
||||
} else if (s.indexOf('rgb(') == 0) { // 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
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user