diff --git a/src/ol/color/color.js b/src/ol/color/color.js index 27ba174246..1d8ec045e6 100644 --- a/src/ol/color/color.js +++ b/src/ol/color/color.js @@ -33,9 +33,17 @@ ol.Color; /** * @type {RegExp} * @private - * This RegExp matches # followed by 3, 4, 6, or 8 hex digits. + * This RegExp matches # followed by 3 or 6 hex digits. */ -ol.color.hexColorRe_ = /^#(?:[0-9a-f]{3,4}){1,2}$/i; +ol.color.hexColorRe_ = /^#(?:[0-9a-f]{3}){1,2}$/i; + + +/** + * @type {RegExp} + * @private + * This RegExp matches # followed by 4 or 8 hex digits. + */ +ol.color.hexaColorRe_ = /^#(?:[0-9a-f]{4}){1,2}$/i; /** @@ -210,8 +218,8 @@ ol.color.fromStringInternal_ = function(s) { var r, g, b, a, color, match; if (isHex || (match = ol.color.hexColorRe_.exec(s))) { // hex var n = s.length - 1; // number of hex digits - goog.asserts.assert(goog.array.indexOf([3, 4, 6, 8], n) != -1); - var d = n < 6 ? 1 : 2; // number of digits per channel + goog.asserts.assert(n == 3 || n == 6); + var d = n == 3 ? 1 : 2; // number of digits per channel r = parseInt(s.substr(1 + 0 * d, d), 16); g = parseInt(s.substr(1 + 1 * d, d), 16); b = parseInt(s.substr(1 + 2 * d, d), 16); @@ -220,11 +228,7 @@ ol.color.fromStringInternal_ = function(s) { g = (g << 4) + g; b = (b << 4) + b; } - if ((n >> 1) & 1) { - a = 1; - } else { // has alpha channel - a = parseInt(s.substr(1 + 3 * d, d), 16) / (d == 1 ? 15 : 255); - } + a = 1; color = [r, g, b, a]; goog.asserts.assert(ol.color.isValid(color)); return color; diff --git a/test/spec/ol/color.test.js b/test/spec/ol/color.test.js index 8cb37f7397..e773a15a48 100644 --- a/test/spec/ol/color.test.js +++ b/test/spec/ol/color.test.js @@ -21,19 +21,10 @@ describe('ol.color', function() { expect(ol.color.fromString('#087')).to.eql([0, 136, 119, 1]); }); - it('can parse 4-digit hex colors', function() { - expect(ol.color.fromString('#1234')).to.eql([17, 34, 51, 68 / 255]); - }); - it('can parse 6-digit hex colors', function() { expect(ol.color.fromString('#56789a')).to.eql([86, 120, 154, 1]); }); - it('can parse 8-digit hex colors', function() { - expect(ol.color.fromString('#bcdef012')).to.eql( - [188, 222, 240, 18 / 255]); - }); - it('can parse rgb colors', function() { expect(ol.color.fromString('rgb(0, 0, 255)')).to.eql([0, 0, 255, 1]); });