2010-03-13 16:34:48 +08:00
|
|
|
if (typeof(require) !== 'undefined') { var tree = require('less/tree') }
|
2010-03-02 09:03:43 +08:00
|
|
|
|
|
|
|
tree.functions = {
|
2010-03-02 08:47:48 +08:00
|
|
|
rgb: function (r, g, b) {
|
|
|
|
return this.rgba(r, g, b, 1.0);
|
|
|
|
},
|
|
|
|
rgba: function (r, g, b, a) {
|
2010-03-02 10:48:19 +08:00
|
|
|
var rgb = [r, g, b].map(function (c) { return number(c) }),
|
|
|
|
a = number(a);
|
|
|
|
return new(tree.Color)(rgb, a);
|
2010-03-02 08:47:48 +08:00
|
|
|
},
|
|
|
|
hsl: function (h, s, l) {
|
|
|
|
return this.hsla(h, s, l, 1.0);
|
|
|
|
},
|
|
|
|
hsla: function (h, s, l, a) {
|
2010-03-02 10:48:19 +08:00
|
|
|
h = number(h); s = number(s);
|
|
|
|
l = number(l); a = number(a);
|
|
|
|
|
2010-03-02 08:47:48 +08:00
|
|
|
var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
|
|
|
|
var m1 = l * 2 - m2;
|
|
|
|
|
|
|
|
return this.rgba(hue(h + 1/3), hue(h), hue(h - 1/3), a);
|
|
|
|
|
|
|
|
function hue(h) {
|
|
|
|
h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h);
|
|
|
|
if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
|
|
|
|
else if (h * 2 < 1) return m2;
|
|
|
|
else if (h * 3 < 2) return m1 + (m2 - m1) * (2/3 - h) * 6;
|
|
|
|
else return m1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2010-03-02 09:03:43 +08:00
|
|
|
|
2010-03-02 10:48:19 +08:00
|
|
|
function number(n) {
|
|
|
|
if (n instanceof tree.Dimension) {
|
|
|
|
return parseFloat(n.value);
|
|
|
|
} else if (typeof(n) === 'number') {
|
|
|
|
return n;
|
|
|
|
} else {
|
|
|
|
throw {
|
|
|
|
error: "RuntimeError",
|
|
|
|
message: "color functions take numbers as parameters"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|