Add support for hex colors with alpha

This commit is contained in:
Tim Schaub
2017-11-07 16:16:37 -07:00
parent 3a6fc7a1f2
commit 6668d03122
2 changed files with 37 additions and 14 deletions
+19 -8
View File
@@ -1,12 +1,10 @@
goog.require('ol.color');
goog.require('ol');
describe('ol.color', function() {
describe('ol.color.asArray()', function() {
describe('asArray()', function() {
it('returns the same for an array', function() {
var color = [1, 2, 3, 0.4];
@@ -29,9 +27,14 @@ describe('ol.color', function() {
expect(color).to.eql([0, 204, 255, 1]);
});
it('returns an array given a hex string with alpha', function() {
var color = ol.color.asArray('#00ccffb0');
expect(color).to.eql([0, 204, 255, 176 / 255]);
});
});
describe('ol.color.asString()', function() {
describe('asString()', function() {
it('returns the same for a string', function() {
var color = 'rgba(0,1,2,0.3)';
@@ -51,7 +54,7 @@ describe('ol.color', function() {
});
describe('ol.color.fromString', function() {
describe('fromString()', function() {
before(function() {
sinon.spy(ol.color, 'fromStringInternal_');
@@ -72,10 +75,18 @@ 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('#0876')).to.eql([0, 136, 119, 102 / 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('#56789acc')).to.eql([86, 120, 154, 204 / 255]);
});
it('can parse rgb colors', function() {
expect(ol.color.fromString('rgb(0, 0, 255)')).to.eql([0, 0, 255, 1]);
});
@@ -130,7 +141,7 @@ describe('ol.color', function() {
});
it('throws an error on invalid colors', function() {
var invalidColors = ['tuesday', '#1234567', 'rgb(255.0,0,0)'];
var invalidColors = ['tuesday', '#12345', '#1234567', 'rgb(255.0,0,0)'];
var i, ii;
for (i = 0, ii < invalidColors.length; i < ii; ++i) {
expect(function() {
@@ -141,7 +152,7 @@ describe('ol.color', function() {
});
describe('ol.color.normalize', function() {
describe('normalize()', function() {
it('clamps out-of-range channels', function() {
expect(ol.color.normalize([-1, 256, 0, 2])).to.eql([0, 255, 0, 1]);
@@ -153,7 +164,7 @@ describe('ol.color', function() {
});
describe('ol.color.toString', function() {
describe('toString()', function() {
it('converts valid colors', function() {
expect(ol.color.toString([1, 2, 3, 0.4])).to.be('rgba(1,2,3,0.4)');