Simplify color parsing and allow more decimals

This commit is contained in:
Marc Jansen
2016-11-23 16:06:08 +01:00
parent c8d35e4fb0
commit 02d41caa8a
2 changed files with 48 additions and 22 deletions

View File

@@ -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() {