Refactor ol.color to use Array.<number>
This commit is contained in:
@@ -1,75 +1,91 @@
|
||||
goog.provide('ol.test.Color');
|
||||
goog.provide('ol.test.color');
|
||||
|
||||
describe('ol.Color', function() {
|
||||
|
||||
describe('constructor', function() {
|
||||
describe('ol.color', function() {
|
||||
|
||||
it('limits r to 0-255', function() {
|
||||
var c;
|
||||
describe('ol.color.fromString', function() {
|
||||
|
||||
// legit r
|
||||
c = new ol.Color(10.5, 11, 12, 0.5);
|
||||
expect(c.r).to.be(10.5);
|
||||
|
||||
// under r
|
||||
c = new ol.Color(-10, 11, 12, 0.5);
|
||||
expect(c.r).to.be(0);
|
||||
|
||||
// over r
|
||||
c = new ol.Color(300, 11, 12, 0.5);
|
||||
expect(c.r).to.be(255);
|
||||
before(function() {
|
||||
sinon.spy(ol.color, 'fromStringInternal_');
|
||||
});
|
||||
|
||||
it('limits g to 0-255', function() {
|
||||
var c;
|
||||
|
||||
// legit g
|
||||
c = new ol.Color(10, 11.5, 12, 0.5);
|
||||
expect(c.g).to.be(11.5);
|
||||
|
||||
// under g
|
||||
c = new ol.Color(10, -11, 12, 0.5);
|
||||
expect(c.g).to.be(0);
|
||||
|
||||
// over g
|
||||
c = new ol.Color(10, 275, 12, 0.5);
|
||||
expect(c.g).to.be(255);
|
||||
after(function() {
|
||||
ol.color.fromStringInternal_.restore();
|
||||
});
|
||||
|
||||
it('limits b to 0-255', function() {
|
||||
var c;
|
||||
|
||||
// legit b
|
||||
c = new ol.Color(10, 11, 12.5, 0.5);
|
||||
expect(c.b).to.be(12.5);
|
||||
|
||||
// under b
|
||||
c = new ol.Color(10, 11, -12, 0.5);
|
||||
expect(c.b).to.be(0);
|
||||
|
||||
// over b
|
||||
c = new ol.Color(10, 11, 500, 0.5);
|
||||
expect(c.b).to.be(255);
|
||||
it('can parse named colors', function() {
|
||||
expect(ol.color.fromString('red')).to.eql([255, 0, 0, 1]);
|
||||
});
|
||||
|
||||
it('limits a to 0-1', function() {
|
||||
var c;
|
||||
it('can parse hex colors', function() {
|
||||
expect(ol.color.fromString('#00ff0080')).to.eql([0, 255, 0, 128 / 255]);
|
||||
});
|
||||
|
||||
// legit a
|
||||
c = new ol.Color(10, 11, 12, 0.5);
|
||||
expect(c.a).to.be(0.5);
|
||||
it('can parse rgb colors', function() {
|
||||
expect(ol.color.fromString('rgb(0, 0, 255)')).to.eql([0, 0, 255, 1]);
|
||||
});
|
||||
|
||||
// under a
|
||||
c = new ol.Color(10, 11, 12, -0.5);
|
||||
expect(c.a).to.be(0);
|
||||
it('can parse rgba colors', function() {
|
||||
expect(ol.color.fromString('rgba(255, 255, 0, 0.1)')).to.eql(
|
||||
[255, 255, 0, 25 / 255]);
|
||||
});
|
||||
|
||||
// over a
|
||||
c = new ol.Color(10, 11, 12, 2.5);
|
||||
expect(c.a).to.be(1);
|
||||
it('caches parsed values', function() {
|
||||
var count = ol.color.fromStringInternal_.callCount;
|
||||
ol.color.fromString('aquamarine');
|
||||
expect(ol.color.fromStringInternal_.callCount).to.be(count + 1);
|
||||
ol.color.fromString('aquamarine');
|
||||
expect(ol.color.fromStringInternal_.callCount).to.be(count + 1);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('ol.color.isValid', function() {
|
||||
|
||||
it('identifies valid colors', function() {
|
||||
expect(ol.color.isValid([0, 0, 0, 0])).to.be(true);
|
||||
expect(ol.color.isValid([255, 255, 255, 1])).to.be(true);
|
||||
});
|
||||
|
||||
it('identifies out-of-range channels', function() {
|
||||
expect(ol.color.isValid([-1, 0, 0, 0])).to.be(false);
|
||||
expect(ol.color.isValid([256, 0, 0, 0])).to.be(false);
|
||||
expect(ol.color.isValid([0, -1, 0, 0])).to.be(false);
|
||||
expect(ol.color.isValid([0, 256, 0, 0])).to.be(false);
|
||||
expect(ol.color.isValid([0, 0, -1, 0])).to.be(false);
|
||||
expect(ol.color.isValid([0, 0, 256, 0])).to.be(false);
|
||||
expect(ol.color.isValid([0, 0, -1, 0])).to.be(false);
|
||||
expect(ol.color.isValid([0, 0, 256, 0])).to.be(false);
|
||||
expect(ol.color.isValid([0, 0, 0, -1])).to.be(false);
|
||||
expect(ol.color.isValid([0, 0, 0, 2])).to.be(false);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('ol.color.normalize', function() {
|
||||
|
||||
it('clamps out-of-range channels', function() {
|
||||
expect(ol.color.normalize([-1, 256, 0, 2])).to.eql([0, 255, 0, 1]);
|
||||
});
|
||||
|
||||
it('rounds color channels to integers', function() {
|
||||
expect(ol.color.normalize([1.2, 2.5, 3.7, 1])).to.eql([1, 3, 4, 1]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('ol.color.toString', function() {
|
||||
|
||||
it('converts valid colors', function() {
|
||||
expect(ol.color.toString([1, 2, 3, 0.4])).to.be('rgba(1,2,3,0.4)');
|
||||
});
|
||||
|
||||
it('rounds to integers if needed', function() {
|
||||
expect(ol.color.toString([1.2, 2.5, 3.7, 0.4])).to.be('rgba(1,3,4,0.4)');
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
goog.require('ol.Color');
|
||||
|
||||
goog.require('ol.color');
|
||||
|
||||
Reference in New Issue
Block a user