1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var Node = require('./node')
|
13 | , nodes = require('./');
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | var HSLA = exports = module.exports = function HSLA(h,s,l,a){
|
26 | Node.call(this);
|
27 | this.h = clampDegrees(h);
|
28 | this.s = clampPercentage(s);
|
29 | this.l = clampPercentage(l);
|
30 | this.a = clampAlpha(a);
|
31 | this.hsla = this;
|
32 | };
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | HSLA.prototype.__proto__ = Node.prototype;
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | HSLA.prototype.toString = function(){
|
48 | return 'hsla('
|
49 | + this.h + ','
|
50 | + this.s.toFixed(0) + '%,'
|
51 | + this.l.toFixed(0) + '%,'
|
52 | + this.a + ')';
|
53 | };
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | HSLA.prototype.clone = function(parent){
|
63 | var clone = new HSLA(
|
64 | this.h
|
65 | , this.s
|
66 | , this.l
|
67 | , this.a);
|
68 | clone.lineno = this.lineno;
|
69 | clone.column = this.column;
|
70 | clone.filename = this.filename;
|
71 | return clone;
|
72 | };
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | HSLA.prototype.toJSON = function(){
|
82 | return {
|
83 | __type: 'HSLA',
|
84 | h: this.h,
|
85 | s: this.s,
|
86 | l: this.l,
|
87 | a: this.a,
|
88 | lineno: this.lineno,
|
89 | column: this.column,
|
90 | filename: this.filename
|
91 | };
|
92 | };
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | HSLA.prototype.__defineGetter__('rgba', function(){
|
102 | return nodes.RGBA.fromHSLA(this);
|
103 | });
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | HSLA.prototype.__defineGetter__('hash', function(){
|
113 | return this.rgba.toString();
|
114 | });
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | HSLA.prototype.add = function(h,s,l){
|
127 | return new HSLA(
|
128 | this.h + h
|
129 | , this.s + s
|
130 | , this.l + l
|
131 | , this.a);
|
132 | };
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | HSLA.prototype.sub = function(h,s,l){
|
145 | return this.add(-h, -s, -l);
|
146 | };
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | HSLA.prototype.operate = function(op, right){
|
158 | switch (op) {
|
159 | case '==':
|
160 | case '!=':
|
161 | case '<=':
|
162 | case '>=':
|
163 | case '<':
|
164 | case '>':
|
165 | case 'is a':
|
166 | case '||':
|
167 | case '&&':
|
168 | return this.rgba.operate(op, right);
|
169 | default:
|
170 | return this.rgba.operate(op, right).hsla;
|
171 | }
|
172 | };
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | exports.fromRGBA = function(rgba){
|
183 | var r = rgba.r / 255
|
184 | , g = rgba.g / 255
|
185 | , b = rgba.b / 255
|
186 | , a = rgba.a;
|
187 |
|
188 | var min = Math.min(r,g,b)
|
189 | , max = Math.max(r,g,b)
|
190 | , l = (max + min) / 2
|
191 | , d = max - min
|
192 | , h, s;
|
193 |
|
194 | switch (max) {
|
195 | case min: h = 0; break;
|
196 | case r: h = 60 * (g-b) / d; break;
|
197 | case g: h = 60 * (b-r) / d + 120; break;
|
198 | case b: h = 60 * (r-g) / d + 240; break;
|
199 | }
|
200 |
|
201 | if (max == min) {
|
202 | s = 0;
|
203 | } else if (l < .5) {
|
204 | s = d / (2 * l);
|
205 | } else {
|
206 | s = d / (2 - 2 * l);
|
207 | }
|
208 |
|
209 | h %= 360;
|
210 | s *= 100;
|
211 | l *= 100;
|
212 |
|
213 | return new HSLA(h,s,l,a);
|
214 | };
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | HSLA.prototype.adjustLightness = function(percent){
|
225 | this.l = clampPercentage(this.l + this.l * (percent / 100));
|
226 | return this;
|
227 | };
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | HSLA.prototype.adjustHue = function(deg){
|
238 | this.h = clampDegrees(this.h + deg);
|
239 | return this;
|
240 | };
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | function clampDegrees(n) {
|
251 | n = n % 360;
|
252 | return n >= 0 ? n : 360 + n;
|
253 | }
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | function clampPercentage(n) {
|
264 | return Math.max(0, Math.min(n, 100));
|
265 | }
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 | function clampAlpha(n) {
|
276 | return Math.max(0, Math.min(n, 1));
|
277 | }
|