1 | (function (tree) { |
---|
2 | |
---|
3 | tree.functions = { |
---|
4 | rgb: function (r, g, b) { |
---|
5 | return this.rgba(r, g, b, 1.0); |
---|
6 | }, |
---|
7 | rgba: function (r, g, b, a) { |
---|
8 | var rgb = [r, g, b].map(function (c) { return number(c) }), |
---|
9 | a = number(a); |
---|
10 | return new(tree.Color)(rgb, a); |
---|
11 | }, |
---|
12 | hsl: function (h, s, l) { |
---|
13 | return this.hsla(h, s, l, 1.0); |
---|
14 | }, |
---|
15 | hsla: function (h, s, l, a) { |
---|
16 | h = (number(h) % 360) / 360; |
---|
17 | s = number(s); l = number(l); a = number(a); |
---|
18 | |
---|
19 | var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s; |
---|
20 | var m1 = l * 2 - m2; |
---|
21 | |
---|
22 | return this.rgba(hue(h + 1/3) * 255, |
---|
23 | hue(h) * 255, |
---|
24 | hue(h - 1/3) * 255, |
---|
25 | a); |
---|
26 | |
---|
27 | function hue(h) { |
---|
28 | h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h); |
---|
29 | if (h * 6 < 1) return m1 + (m2 - m1) * h * 6; |
---|
30 | else if (h * 2 < 1) return m2; |
---|
31 | else if (h * 3 < 2) return m1 + (m2 - m1) * (2/3 - h) * 6; |
---|
32 | else return m1; |
---|
33 | } |
---|
34 | }, |
---|
35 | hue: function (color) { |
---|
36 | return new(tree.Dimension)(Math.round(color.toHSL().h)); |
---|
37 | }, |
---|
38 | saturation: function (color) { |
---|
39 | return new(tree.Dimension)(Math.round(color.toHSL().s * 100), '%'); |
---|
40 | }, |
---|
41 | lightness: function (color) { |
---|
42 | return new(tree.Dimension)(Math.round(color.toHSL().l * 100), '%'); |
---|
43 | }, |
---|
44 | alpha: function (color) { |
---|
45 | return new(tree.Dimension)(color.toHSL().a); |
---|
46 | }, |
---|
47 | saturate: function (color, amount) { |
---|
48 | var hsl = color.toHSL(); |
---|
49 | |
---|
50 | hsl.s += amount.value / 100; |
---|
51 | hsl.s = clamp(hsl.s); |
---|
52 | return hsla(hsl); |
---|
53 | }, |
---|
54 | desaturate: function (color, amount) { |
---|
55 | var hsl = color.toHSL(); |
---|
56 | |
---|
57 | hsl.s -= amount.value / 100; |
---|
58 | hsl.s = clamp(hsl.s); |
---|
59 | return hsla(hsl); |
---|
60 | }, |
---|
61 | lighten: function (color, amount) { |
---|
62 | var hsl = color.toHSL(); |
---|
63 | |
---|
64 | hsl.l += amount.value / 100; |
---|
65 | hsl.l = clamp(hsl.l); |
---|
66 | return hsla(hsl); |
---|
67 | }, |
---|
68 | darken: function (color, amount) { |
---|
69 | var hsl = color.toHSL(); |
---|
70 | |
---|
71 | hsl.l -= amount.value / 100; |
---|
72 | hsl.l = clamp(hsl.l); |
---|
73 | return hsla(hsl); |
---|
74 | }, |
---|
75 | fadein: function (color, amount) { |
---|
76 | var hsl = color.toHSL(); |
---|
77 | |
---|
78 | hsl.a += amount.value / 100; |
---|
79 | hsl.a = clamp(hsl.a); |
---|
80 | return hsla(hsl); |
---|
81 | }, |
---|
82 | fadeout: function (color, amount) { |
---|
83 | var hsl = color.toHSL(); |
---|
84 | |
---|
85 | hsl.a -= amount.value / 100; |
---|
86 | hsl.a = clamp(hsl.a); |
---|
87 | return hsla(hsl); |
---|
88 | }, |
---|
89 | spin: function (color, amount) { |
---|
90 | var hsl = color.toHSL(); |
---|
91 | var hue = (hsl.h + amount.value) % 360; |
---|
92 | |
---|
93 | hsl.h = hue < 0 ? 360 + hue : hue; |
---|
94 | |
---|
95 | return hsla(hsl); |
---|
96 | }, |
---|
97 | // |
---|
98 | // Copyright (c) 2006-2009 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein |
---|
99 | // http://sass-lang.com |
---|
100 | // |
---|
101 | mix: function (color1, color2, weight) { |
---|
102 | var p = weight.value / 100.0; |
---|
103 | var w = p * 2 - 1; |
---|
104 | var a = color1.toHSL().a - color2.toHSL().a; |
---|
105 | |
---|
106 | var w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2.0; |
---|
107 | var w2 = 1 - w1; |
---|
108 | |
---|
109 | var rgb = [color1.rgb[0] * w1 + color2.rgb[0] * w2, |
---|
110 | color1.rgb[1] * w1 + color2.rgb[1] * w2, |
---|
111 | color1.rgb[2] * w1 + color2.rgb[2] * w2]; |
---|
112 | |
---|
113 | var alpha = color1.alpha * p + color2.alpha * (1 - p); |
---|
114 | |
---|
115 | return new(tree.Color)(rgb, alpha); |
---|
116 | }, |
---|
117 | greyscale: function (color) { |
---|
118 | return this.desaturate(color, new(tree.Dimension)(100)); |
---|
119 | }, |
---|
120 | e: function (str) { |
---|
121 | return new(tree.Anonymous)(str instanceof tree.JavaScript ? str.evaluated : str); |
---|
122 | }, |
---|
123 | escape: function (str) { |
---|
124 | return new(tree.Anonymous)(encodeURI(str.value).replace(/=/g, "%3D").replace(/:/g, "%3A").replace(/#/g, "%23").replace(/;/g, "%3B").replace(/\(/g, "%28").replace(/\)/g, "%29")); |
---|
125 | }, |
---|
126 | '%': function (quoted /* arg, arg, ...*/) { |
---|
127 | var args = Array.prototype.slice.call(arguments, 1), |
---|
128 | str = quoted.value; |
---|
129 | |
---|
130 | for (var i = 0; i < args.length; i++) { |
---|
131 | str = str.replace(/%[sda]/i, function(token) { |
---|
132 | var value = token.match(/s/i) ? args[i].value : args[i].toCSS(); |
---|
133 | return token.match(/[A-Z]$/) ? encodeURIComponent(value) : value; |
---|
134 | }); |
---|
135 | } |
---|
136 | str = str.replace(/%%/g, '%'); |
---|
137 | return new(tree.Quoted)('"' + str + '"', str); |
---|
138 | }, |
---|
139 | round: function (n) { |
---|
140 | if (n instanceof tree.Dimension) { |
---|
141 | return new(tree.Dimension)(Math.round(number(n)), n.unit); |
---|
142 | } else if (typeof(n) === 'number') { |
---|
143 | return Math.round(n); |
---|
144 | } else { |
---|
145 | throw { |
---|
146 | error: "RuntimeError", |
---|
147 | message: "math functions take numbers as parameters" |
---|
148 | }; |
---|
149 | } |
---|
150 | } |
---|
151 | }; |
---|
152 | |
---|
153 | function hsla(hsla) { |
---|
154 | return tree.functions.hsla(hsla.h, hsla.s, hsla.l, hsla.a); |
---|
155 | } |
---|
156 | |
---|
157 | function number(n) { |
---|
158 | if (n instanceof tree.Dimension) { |
---|
159 | return parseFloat(n.unit == '%' ? n.value / 100 : n.value); |
---|
160 | } else if (typeof(n) === 'number') { |
---|
161 | return n; |
---|
162 | } else { |
---|
163 | throw { |
---|
164 | error: "RuntimeError", |
---|
165 | message: "color functions take numbers as parameters" |
---|
166 | }; |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | function clamp(val) { |
---|
171 | return Math.min(1, Math.max(0, val)); |
---|
172 | } |
---|
173 | |
---|
174 | })(require('less/tree')); |
---|