Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
Add toArray method, remove old code
Browse files Browse the repository at this point in the history
  • Loading branch information
dy committed Nov 30, 2014
1 parent 51cffba commit 8997e8e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
40 changes: 22 additions & 18 deletions color.js
Expand Up @@ -90,10 +90,11 @@ Color.prototype = {
},
hwbArray: function() {
this.actualizeSpace('hwb');
var hwb = this.values.slice()
if (this._alpha !== 1) {
return this.values.concat(this._alpha);
return hwb.concat(this._alpha);
}
return this.values.slice();
return hwb;
},
cmykArray: function() {
this.actualizeSpace('cmyk');
Expand Down Expand Up @@ -362,6 +363,15 @@ Color.prototype = {
return new Color(this.rgbArray());
},

//somewhat generic getters
toArray: function(){
var vals = this.values.slice();
if (this._alpha === 1) {
return vals.concat(this._alpha);
}
return vals;
},

toString: function(){
return string[this.space + 'String'](this.values, this._alpha);
}
Expand Down Expand Up @@ -412,7 +422,9 @@ Color.prototype.setValues = function(space, vals) {
}
alpha = vals.alpha;
}

this._alpha = Math.max(0, Math.min(1, (alpha !== undefined ? alpha : this._alpha) ));

if (space == "alpha") {
return;
}
Expand All @@ -432,22 +444,14 @@ Color.prototype.actualizeSpace = function(space){
var currSpace = this.space;

//space is already actual
if (currSpace === space) return this;
if (space === 'alpha') return this;

// cap values of the space prior converting all values
//FIXME
// for (var i = space.length; i--;) {
// var capped = Math.max(0, Math.min(convert[space].max[i], this.values[space][i]));
// this.values[space][i] = Math.round(capped);
// }

//calc new space values
this.values = convert[currSpace][space](this.values);
for (var i = this.values.length; i--;) this.values[i] = Math.round(this.values[i]);

//save last actual space
this.space = space;
if (currSpace !== space && space !== 'alpha') {
//calc new space values
this.values = convert[currSpace][space](this.values);
for (var i = this.values.length; i--;) this.values[i] = Math.round(this.values[i]);

//save last actual space
this.space = space;
}

return this;
};
Expand Down
3 changes: 1 addition & 2 deletions test/basic.js
Expand Up @@ -190,8 +190,7 @@ var space = 'hsl';

console.time('performance');
for (var x = 0, end = 2e5; x < end; x++){
color.setChannel(space, 1, 255 * x / end);

color.setChannel(space, 1, 100 * x / end);
color.red();
color.green();
color.blue();
Expand Down

0 comments on commit 8997e8e

Please sign in to comment.