tizen beta release
[platform/framework/web/web-ui-fw.git] / build-tools / lib / less / functions.js
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     fade: function (color, amount) {
90         var hsl = color.toHSL();
91
92         hsl.a = amount.value / 100;
93         hsl.a = clamp(hsl.a);
94         return hsla(hsl);
95     },
96     spin: function (color, amount) {
97         var hsl = color.toHSL();
98         var hue = (hsl.h + amount.value) % 360;
99
100         hsl.h = hue < 0 ? 360 + hue : hue;
101
102         return hsla(hsl);
103     },
104     //
105     // Copyright (c) 2006-2009 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein
106     // http://sass-lang.com
107     //
108     mix: function (color1, color2, weight) {
109         var p = weight.value / 100.0;
110         var w = p * 2 - 1;
111         var a = color1.toHSL().a - color2.toHSL().a;
112
113         var w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2.0;
114         var w2 = 1 - w1;
115
116         var rgb = [color1.rgb[0] * w1 + color2.rgb[0] * w2,
117                    color1.rgb[1] * w1 + color2.rgb[1] * w2,
118                    color1.rgb[2] * w1 + color2.rgb[2] * w2];
119
120         var alpha = color1.alpha * p + color2.alpha * (1 - p);
121
122         return new(tree.Color)(rgb, alpha);
123     },
124     greyscale: function (color) {
125         return this.desaturate(color, new(tree.Dimension)(100));
126     },
127     e: function (str) {
128         return new(tree.Anonymous)(str instanceof tree.JavaScript ? str.evaluated : str);
129     },
130     escape: function (str) {
131         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"));
132     },
133     '%': function (quoted /* arg, arg, ...*/) {
134         var args = Array.prototype.slice.call(arguments, 1),
135             str = quoted.value;
136
137         for (var i = 0; i < args.length; i++) {
138             str = str.replace(/%[sda]/i, function(token) {
139                 var value = token.match(/s/i) ? args[i].value : args[i].toCSS();
140                 return token.match(/[A-Z]$/) ? encodeURIComponent(value) : value;
141             });
142         }
143         str = str.replace(/%%/g, '%');
144         return new(tree.Quoted)('"' + str + '"', str);
145     },
146     round: function (n) {
147         if (n instanceof tree.Dimension) {
148             return new(tree.Dimension)(Math.round(number(n)), n.unit);
149         } else if (typeof(n) === 'number') {
150             return Math.round(n);
151         } else {
152             throw {
153                 error: "RuntimeError",
154                 message: "math functions take numbers as parameters"
155             };
156         }
157     },
158     argb: function (color) {
159         return new(tree.Anonymous)(color.toARGB());
160
161     }
162 };
163
164 function hsla(hsla) {
165     return tree.functions.hsla(hsla.h, hsla.s, hsla.l, hsla.a);
166 }
167
168 function number(n) {
169     if (n instanceof tree.Dimension) {
170         return parseFloat(n.unit == '%' ? n.value / 100 : n.value);
171     } else if (typeof(n) === 'number') {
172         return n;
173     } else {
174         throw {
175             error: "RuntimeError",
176             message: "color functions take numbers as parameters"
177         };
178     }
179 }
180
181 function clamp(val) {
182     return Math.min(1, Math.max(0, val));
183 }
184
185 })(require('less/tree'));