source: trip-planner-front/node_modules/stylus/lib/nodes/rgba.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.7 KB
Line 
1
2/*!
3 * Stylus - RGBA
4 * Copyright (c) Automattic <developer.wordpress.com>
5 * MIT Licensed
6 */
7
8/**
9 * Module dependencies.
10 */
11
12var Node = require('./node')
13 , HSLA = require('./hsla')
14 , functions = require('../functions')
15 , adjust = functions.adjust
16 , nodes = require('./');
17
18/**
19 * Initialize a new `RGBA` with the given r,g,b,a component values.
20 *
21 * @param {Number} r
22 * @param {Number} g
23 * @param {Number} b
24 * @param {Number} a
25 * @api public
26 */
27
28var RGBA = exports = module.exports = function RGBA(r,g,b,a){
29 Node.call(this);
30 this.r = clamp(r);
31 this.g = clamp(g);
32 this.b = clamp(b);
33 this.a = clampAlpha(a);
34 this.name = '';
35 this.rgba = this;
36};
37
38/**
39 * Inherit from `Node.prototype`.
40 */
41
42RGBA.prototype.__proto__ = Node.prototype;
43
44/**
45 * Return an `RGBA` without clamping values.
46 *
47 * @param {Number} r
48 * @param {Number} g
49 * @param {Number} b
50 * @param {Number} a
51 * @return {RGBA}
52 * @api public
53 */
54
55RGBA.withoutClamping = function(r,g,b,a){
56 var rgba = new RGBA(0,0,0,0);
57 rgba.r = r;
58 rgba.g = g;
59 rgba.b = b;
60 rgba.a = a;
61 return rgba;
62};
63
64/**
65 * Return a clone of this node.
66 *
67 * @return {Node}
68 * @api public
69 */
70
71RGBA.prototype.clone = function(){
72 var clone = new RGBA(
73 this.r
74 , this.g
75 , this.b
76 , this.a);
77 clone.raw = this.raw;
78 clone.name = this.name;
79 clone.lineno = this.lineno;
80 clone.column = this.column;
81 clone.filename = this.filename;
82 return clone;
83};
84
85/**
86 * Return a JSON representation of this node.
87 *
88 * @return {Object}
89 * @api public
90 */
91
92RGBA.prototype.toJSON = function(){
93 return {
94 __type: 'RGBA',
95 r: this.r,
96 g: this.g,
97 b: this.b,
98 a: this.a,
99 raw: this.raw,
100 name: this.name,
101 lineno: this.lineno,
102 column: this.column,
103 filename: this.filename
104 };
105};
106
107/**
108 * Return true.
109 *
110 * @return {Boolean}
111 * @api public
112 */
113
114RGBA.prototype.toBoolean = function(){
115 return nodes.true;
116};
117
118/**
119 * Return `HSLA` representation.
120 *
121 * @return {HSLA}
122 * @api public
123 */
124
125RGBA.prototype.__defineGetter__('hsla', function(){
126 return HSLA.fromRGBA(this);
127});
128
129/**
130 * Return hash.
131 *
132 * @return {String}
133 * @api public
134 */
135
136RGBA.prototype.__defineGetter__('hash', function(){
137 return this.toString();
138});
139
140/**
141 * Add r,g,b,a to the current component values.
142 *
143 * @param {Number} r
144 * @param {Number} g
145 * @param {Number} b
146 * @param {Number} a
147 * @return {RGBA} new node
148 * @api public
149 */
150
151RGBA.prototype.add = function(r,g,b,a){
152 return new RGBA(
153 this.r + r
154 , this.g + g
155 , this.b + b
156 , this.a + a);
157};
158
159/**
160 * Subtract r,g,b,a from the current component values.
161 *
162 * @param {Number} r
163 * @param {Number} g
164 * @param {Number} b
165 * @param {Number} a
166 * @return {RGBA} new node
167 * @api public
168 */
169
170RGBA.prototype.sub = function(r,g,b,a){
171 return new RGBA(
172 this.r - r
173 , this.g - g
174 , this.b - b
175 , a == 1 ? this.a : this.a - a);
176};
177
178/**
179 * Multiply rgb components by `n`.
180 *
181 * @param {String} n
182 * @return {RGBA} new node
183 * @api public
184 */
185
186RGBA.prototype.multiply = function(n){
187 return new RGBA(
188 this.r * n
189 , this.g * n
190 , this.b * n
191 , this.a);
192};
193
194/**
195 * Divide rgb components by `n`.
196 *
197 * @param {String} n
198 * @return {RGBA} new node
199 * @api public
200 */
201
202RGBA.prototype.divide = function(n){
203 return new RGBA(
204 this.r / n
205 , this.g / n
206 , this.b / n
207 , this.a);
208};
209
210/**
211 * Operate on `right` with the given `op`.
212 *
213 * @param {String} op
214 * @param {Node} right
215 * @return {Node}
216 * @api public
217 */
218
219RGBA.prototype.operate = function(op, right){
220 if ('in' != op) right = right.first
221
222 switch (op) {
223 case 'is a':
224 if ('string' == right.nodeName && 'color' == right.string) {
225 return nodes.true;
226 }
227 break;
228 case '+':
229 switch (right.nodeName) {
230 case 'unit':
231 var n = right.val;
232 switch (right.type) {
233 case '%': return adjust(this, new nodes.String('lightness'), right);
234 case 'deg': return this.hsla.adjustHue(n).rgba;
235 default: return this.add(n,n,n,0);
236 }
237 case 'rgba':
238 return this.add(right.r, right.g, right.b, right.a);
239 case 'hsla':
240 return this.hsla.add(right.h, right.s, right.l);
241 }
242 break;
243 case '-':
244 switch (right.nodeName) {
245 case 'unit':
246 var n = right.val;
247 switch (right.type) {
248 case '%': return adjust(this, new nodes.String('lightness'), new nodes.Unit(-n, '%'));
249 case 'deg': return this.hsla.adjustHue(-n).rgba;
250 default: return this.sub(n,n,n,0);
251 }
252 case 'rgba':
253 return this.sub(right.r, right.g, right.b, right.a);
254 case 'hsla':
255 return this.hsla.sub(right.h, right.s, right.l);
256 }
257 break;
258 case '*':
259 switch (right.nodeName) {
260 case 'unit':
261 return this.multiply(right.val);
262 }
263 break;
264 case '/':
265 switch (right.nodeName) {
266 case 'unit':
267 return this.divide(right.val);
268 }
269 break;
270 }
271 return Node.prototype.operate.call(this, op, right);
272};
273
274/**
275 * Return #nnnnnn, #nnn, or rgba(n,n,n,n) string representation of the color.
276 *
277 * @return {String}
278 * @api public
279 */
280
281RGBA.prototype.toString = function(){
282 function pad(n) {
283 return n < 16
284 ? '0' + n.toString(16)
285 : n.toString(16);
286 }
287
288 // special case for transparent named color
289 if ('transparent' == this.name)
290 return this.name;
291
292 if (1 == this.a) {
293 var r = pad(this.r)
294 , g = pad(this.g)
295 , b = pad(this.b);
296
297 // Compress
298 if (r[0] == r[1] && g[0] == g[1] && b[0] == b[1]) {
299 return '#' + r[0] + g[0] + b[0];
300 } else {
301 return '#' + r + g + b;
302 }
303 } else {
304 return 'rgba('
305 + this.r + ','
306 + this.g + ','
307 + this.b + ','
308 + (+this.a.toFixed(3)) + ')';
309 }
310};
311
312/**
313 * Return a `RGBA` from the given `hsla`.
314 *
315 * @param {HSLA} hsla
316 * @return {RGBA}
317 * @api public
318 */
319
320exports.fromHSLA = function(hsla){
321 var h = hsla.h / 360
322 , s = hsla.s / 100
323 , l = hsla.l / 100
324 , a = hsla.a;
325
326 var m2 = l <= .5 ? l * (s + 1) : l + s - l * s
327 , m1 = l * 2 - m2;
328
329 var r = hue(h + 1/3) * 0xff
330 , g = hue(h) * 0xff
331 , b = hue(h - 1/3) * 0xff;
332
333 function hue(h) {
334 if (h < 0) ++h;
335 if (h > 1) --h;
336 if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
337 if (h * 2 < 1) return m2;
338 if (h * 3 < 2) return m1 + (m2 - m1) * (2/3 - h) * 6;
339 return m1;
340 }
341
342 return new RGBA(r,g,b,a);
343};
344
345/**
346 * Clamp `n` >= 0 and <= 255.
347 *
348 * @param {Number} n
349 * @return {Number}
350 * @api private
351 */
352
353function clamp(n) {
354 return Math.max(0, Math.min(n.toFixed(0), 255));
355}
356
357/**
358 * Clamp alpha `n` >= 0 and <= 1.
359 *
360 * @param {Number} n
361 * @return {Number}
362 * @api private
363 */
364
365function clampAlpha(n) {
366 return Math.max(0, Math.min(n, 1));
367}
Note: See TracBrowser for help on using the repository browser.