Add color object to accept array of rgb(a)

This commit is contained in:
Niklas von Hertzen
2014-12-13 18:30:52 +02:00
parent 3b0352a3d7
commit fcbcb9bfaa
4 changed files with 53 additions and 5 deletions

View File

@ -5,7 +5,8 @@ function Color(value) {
this.g = 0;
this.b = 0;
this.a = null;
var result = this.namedColor(value) ||
var result = this.fromArray(value) ||
this.namedColor(value) ||
this.rgb(value) ||
this.rgba(value) ||
this.hex6(value) ||
@ -16,6 +17,19 @@ Color.prototype.isTransparent = function() {
return this.a === 0;
};
Color.prototype.fromArray = function(array) {
if (Array.isArray(array)) {
this.r = array[0];
this.g = array[1];
this.b = array[2];
if (array.length > 3) {
this.a = array[3];
}
}
return (Array.isArray(array));
};
var _hex3 = /^#([a-f0-9]{3})$/i;
Color.prototype.hex3 = function(value) {
@ -67,7 +81,7 @@ Color.prototype.rgba = function(value) {
};
Color.prototype.toString = function() {
return this.a !== null ?
return this.a !== null && this.a !== 1 ?
"rgba(" + [this.r, this.g, this.b, this.a].join(",") + ")" :
"rgb(" + [this.r, this.g, this.b].join(",") + ")";
};