2010-06-16 06:44:59 +08:00
|
|
|
(function (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);
|
2011-01-22 05:17:52 +08:00
|
|
|
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-06-16 08:13:19 +08:00
|
|
|
h = (number(h) % 360) / 360;
|
2010-04-24 01:35:36 +08:00
|
|
|
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;
|
|
|
|
|
2010-04-24 01:35:36 +08:00
|
|
|
return this.rgba(hue(h + 1/3) * 255,
|
|
|
|
hue(h) * 255,
|
|
|
|
hue(h - 1/3) * 255,
|
|
|
|
a);
|
2010-03-02 08:47:48 +08:00
|
|
|
|
|
|
|
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-04-23 07:42:02 +08:00
|
|
|
},
|
2010-06-16 08:13:19 +08:00
|
|
|
hue: function (color) {
|
2011-01-22 05:17:52 +08:00
|
|
|
return new tree.Dimension(Math.round(color.toHSL().h));
|
2010-06-16 08:13:19 +08:00
|
|
|
},
|
|
|
|
saturation: function (color) {
|
2011-01-22 05:17:52 +08:00
|
|
|
return new tree.Dimension(Math.round(color.toHSL().s * 100), '%');
|
2010-06-16 08:13:19 +08:00
|
|
|
},
|
|
|
|
lightness: function (color) {
|
2011-01-22 05:17:52 +08:00
|
|
|
return new tree.Dimension(Math.round(color.toHSL().l * 100), '%');
|
2010-06-16 08:13:19 +08:00
|
|
|
},
|
|
|
|
alpha: function (color) {
|
2011-01-22 05:17:52 +08:00
|
|
|
return new tree.Dimension(color.toHSL().a);
|
2010-06-16 08:13:19 +08:00
|
|
|
},
|
2010-04-24 01:36:50 +08:00
|
|
|
saturate: function (color, amount) {
|
|
|
|
var hsl = color.toHSL();
|
|
|
|
|
|
|
|
hsl.s += amount.value / 100;
|
|
|
|
hsl.s = clamp(hsl.s);
|
2010-06-20 02:35:37 +08:00
|
|
|
return hsla(hsl);
|
2010-04-24 01:36:50 +08:00
|
|
|
},
|
|
|
|
desaturate: function (color, amount) {
|
|
|
|
var hsl = color.toHSL();
|
|
|
|
|
|
|
|
hsl.s -= amount.value / 100;
|
|
|
|
hsl.s = clamp(hsl.s);
|
2010-06-20 02:35:37 +08:00
|
|
|
return hsla(hsl);
|
2010-04-24 01:36:50 +08:00
|
|
|
},
|
|
|
|
lighten: function (color, amount) {
|
|
|
|
var hsl = color.toHSL();
|
|
|
|
|
2010-06-16 08:13:19 +08:00
|
|
|
hsl.l += amount.value / 100;
|
2010-04-24 01:36:50 +08:00
|
|
|
hsl.l = clamp(hsl.l);
|
2010-06-20 02:35:37 +08:00
|
|
|
return hsla(hsl);
|
2010-04-24 01:36:50 +08:00
|
|
|
},
|
|
|
|
darken: function (color, amount) {
|
|
|
|
var hsl = color.toHSL();
|
|
|
|
|
2010-06-16 08:13:19 +08:00
|
|
|
hsl.l -= amount.value / 100;
|
2010-04-24 01:36:50 +08:00
|
|
|
hsl.l = clamp(hsl.l);
|
2010-06-20 02:35:37 +08:00
|
|
|
return hsla(hsl);
|
2010-04-24 01:36:50 +08:00
|
|
|
},
|
2010-11-20 04:18:56 +08:00
|
|
|
fadein: function (color, amount) {
|
|
|
|
var hsl = color.toHSL();
|
|
|
|
|
|
|
|
hsl.a += amount.value / 100;
|
|
|
|
hsl.a = clamp(hsl.a);
|
|
|
|
return hsla(hsl);
|
|
|
|
},
|
|
|
|
fadeout: function (color, amount) {
|
|
|
|
var hsl = color.toHSL();
|
|
|
|
|
|
|
|
hsl.a -= amount.value / 100;
|
|
|
|
hsl.a = clamp(hsl.a);
|
|
|
|
return hsla(hsl);
|
|
|
|
},
|
2010-06-16 08:13:19 +08:00
|
|
|
spin: function (color, amount) {
|
|
|
|
var hsl = color.toHSL();
|
|
|
|
var hue = (hsl.h + amount.value) % 360;
|
|
|
|
|
|
|
|
hsl.h = hue < 0 ? 360 + hue : hue;
|
|
|
|
|
2010-06-20 02:35:37 +08:00
|
|
|
return hsla(hsl);
|
2010-06-16 08:13:19 +08:00
|
|
|
},
|
2010-11-28 21:13:13 +08:00
|
|
|
//
|
|
|
|
// Copyright (c) 2006-2009 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein
|
|
|
|
// http://sass-lang.com
|
|
|
|
//
|
2010-11-27 06:19:30 +08:00
|
|
|
mix: function (color1, color2, weight) {
|
|
|
|
var p = weight.value / 100.0;
|
|
|
|
var w = p * 2 - 1;
|
|
|
|
var a = color1.toHSL().a - color2.toHSL().a;
|
|
|
|
|
|
|
|
var w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2.0;
|
|
|
|
var w2 = 1 - w1;
|
|
|
|
|
|
|
|
var rgb = [color1.rgb[0] * w1 + color2.rgb[0] * w2,
|
|
|
|
color1.rgb[1] * w1 + color2.rgb[1] * w2,
|
|
|
|
color1.rgb[2] * w1 + color2.rgb[2] * w2];
|
|
|
|
|
|
|
|
var alpha = color1.alpha * p + color2.alpha * (1 - p);
|
|
|
|
|
2011-01-22 05:17:52 +08:00
|
|
|
return new tree.Color(rgb, alpha);
|
2010-11-27 06:19:30 +08:00
|
|
|
},
|
2010-06-16 08:13:19 +08:00
|
|
|
greyscale: function (color) {
|
2011-01-22 05:17:52 +08:00
|
|
|
return this.desaturate(color, new tree.Dimension(100));
|
2010-04-24 01:36:50 +08:00
|
|
|
},
|
2010-04-23 07:42:02 +08:00
|
|
|
e: function (str) {
|
2011-01-22 05:17:52 +08:00
|
|
|
return new tree.Anonymous(str instanceof tree.JavaScript ? str.evaluated : str);
|
2010-05-01 02:06:36 +08:00
|
|
|
},
|
|
|
|
'%': function (quoted /* arg, arg, ...*/) {
|
|
|
|
var args = Array.prototype.slice.call(arguments, 1),
|
2010-07-26 04:41:05 +08:00
|
|
|
str = quoted.value;
|
2010-05-01 02:06:36 +08:00
|
|
|
|
|
|
|
for (var i = 0; i < args.length; i++) {
|
2010-07-26 04:41:05 +08:00
|
|
|
str = str.replace(/%s/, args[i].value)
|
2011-01-22 07:28:54 +08:00
|
|
|
.replace(/%[da]/, args[i].toString());
|
2010-05-01 02:06:36 +08:00
|
|
|
}
|
|
|
|
str = str.replace(/%%/g, '%');
|
2011-01-22 05:17:52 +08:00
|
|
|
return new tree.Quoted('"' + str + '"', str);
|
2010-03-02 08:47:48 +08:00
|
|
|
}
|
|
|
|
};
|
2010-03-02 09:03:43 +08:00
|
|
|
|
2010-06-20 02:35:37 +08:00
|
|
|
function hsla(hsla) {
|
|
|
|
return tree.functions.hsla(hsla.h, hsla.s, hsla.l, hsla.a);
|
|
|
|
}
|
|
|
|
|
2010-03-02 10:48:19 +08:00
|
|
|
function number(n) {
|
|
|
|
if (n instanceof tree.Dimension) {
|
2010-04-24 01:35:36 +08:00
|
|
|
return parseFloat(n.unit == '%' ? n.value / 100 : n.value);
|
2010-03-02 10:48:19 +08:00
|
|
|
} else if (typeof(n) === 'number') {
|
|
|
|
return n;
|
|
|
|
} else {
|
|
|
|
throw {
|
|
|
|
error: "RuntimeError",
|
|
|
|
message: "color functions take numbers as parameters"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2010-04-24 01:36:50 +08:00
|
|
|
|
|
|
|
function clamp(val) {
|
|
|
|
return Math.min(1, Math.max(0, val));
|
|
|
|
}
|
2010-06-16 06:44:59 +08:00
|
|
|
|
2011-01-06 03:23:28 +08:00
|
|
|
})(require('mess/tree'));
|