cdf
Version:
A library for creating oldschool demo-like animations with JavaScript
51 lines (46 loc) • 1.34 kB
JavaScript
(function(global,cdf_varname){var lib = global[cdf_varname] || {};
var Color = lib.Color = lib.color = function(r,g,b,a){
if(this instanceof Color) {
if (arguments.length >= 3) this.rgba(r, g, b, a);
else this.rgba(0, 0, 0, 1);
} else {
return Color.rgba.apply(this,arguments);
}
};
Color.rgba = function (r, g, b, a) {
return new Color(r,g,b,a);
};
Color.arr = function(arr){
return Color.rgba.apply(this,arr);
};
Color.prototype = {
toHex: function(){
return '#'+[this.r,this.g,this.b,this.a].map(function(c){return c.toString(16)}).join('');
},
rgba:function(r,g,b,a){
this.a=a||1;
this.r=r;
this.g=g;
this.b=b;
return this;
},
rgba2:function(r,g,b,a){
return this.rgba(r/255,g/255,b/255,a);
},
toCss: function () {
if(this.a===1){
return 'rgb('
}
return 'rgba('+Math.round(this.r*255)+','+Math.round(this.g*255)+','+Math.round(this.b*255)+','+a+')';
},
add:function(f){
var b=this;
return new Color(
(f.r * f.a) + (b.r * (1 - f.a)),
(f.g * f.a) + (b.g * (1 - f.a)),
(f.b * f.a) + (b.b * (1 - f.a)),
1
);
}
};
if(cdf_varname) global[cdf_varname]=lib;})(window, (typeof module!=='undefined'&& module.exports)?false:'cdf');