Simplify color parsing and allow more decimals
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user