Files
less.js/lib/less/node/color.js
2010-03-01 21:48:19 -05:00

45 lines
1.3 KiB
JavaScript

if (typeof(window) === 'undefined') { var tree = require(require('path').join(__dirname, '..', '..', 'less', 'tree')); }
//
// RGB Colors - #ff0014, #eee
//
tree.Color = function Color(rgb, a) {
if (Array.isArray(rgb)) {
this.rgb = rgb;
this.alpha = a;
} else if (rgb.length == 6) {
this.rgb = rgb.match(/.{2}/g).map(function (c) {
return parseInt(c, 16);
});
} else {
this.rgb = rgb.split('').map(function (c) {
return parseInt(c + c, 16);
});
}
};
tree.Color.prototype = {
eval: function () { return this },
toCSS: function () {
if (this.alpha && this.alpha < 1.0) {
return "rgba(" + this.rgb.concat(this.alpha).join(', ') + ")";
} else {
return '#' + this.rgb.map(function (i) {
i = Math.round(i);
i = (i > 255 ? 255 : (i < 0 ? 0 : i)).toString(16);
return i.length === 1 ? '0' + i : i;
}).join('');
}
},
operate: function (op, other) {
var result = [];
if (! (other instanceof tree.Color)) {
other = other.toColor();
}
for (var c = 0; c < 3; c++) {
result[c] = tree.operate(op, this.rgb[c], other.rgb[c]);
}
return new(tree.Color)(result);
}
};