@@ -8,8 +8,17 @@ var Dimension = require('../tree/dimension'),
8
8
function clamp ( val ) {
9
9
return Math . min ( 1 , Math . max ( 0 , val ) ) ;
10
10
}
11
- function hsla ( color ) {
12
- return colorFunctions . hsla ( color . h , color . s , color . l , color . a ) ;
11
+ function hsla ( origColor , hsl ) {
12
+ var color = colorFunctions . hsla ( hsl . h , hsl . s , hsl . l , hsl . a ) ;
13
+ if ( color ) {
14
+ if ( origColor . value &&
15
+ / ^ ( r g b | h s l ) / . test ( origColor . value ) ) {
16
+ color . value = origColor . value ;
17
+ } else {
18
+ color . value = 'rgb' ;
19
+ }
20
+ return color ;
21
+ }
13
22
}
14
23
function number ( n ) {
15
24
if ( n instanceof Dimension ) {
@@ -32,46 +41,79 @@ function scaled(n, size) {
32
41
}
33
42
colorFunctions = {
34
43
rgb : function ( r , g , b ) {
35
- return colorFunctions . rgba ( r , g , b , 1.0 ) ;
44
+ var color = colorFunctions . rgba ( r , g , b , 1.0 ) ;
45
+ if ( color ) {
46
+ color . value = 'rgb' ;
47
+ return color ;
48
+ }
36
49
} ,
37
50
rgba : function ( r , g , b , a ) {
38
- var rgb = [ r , g , b ] . map ( function ( c ) { return scaled ( c , 255 ) ; } ) ;
39
- a = number ( a ) ;
40
- return new Color ( rgb , a ) ;
51
+ try {
52
+ if ( r instanceof Color ) {
53
+ if ( g ) {
54
+ a = number ( g ) ;
55
+ } else {
56
+ a = r . alpha ;
57
+ }
58
+ return new Color ( r . rgb , a , 'rgba' ) ;
59
+ }
60
+ var rgb = [ r , g , b ] . map ( function ( c ) { return scaled ( c , 255 ) ; } ) ;
61
+ a = number ( a ) ;
62
+ return new Color ( rgb , a , 'rgba' ) ;
63
+ }
64
+ catch ( e ) { }
41
65
} ,
42
66
hsl : function ( h , s , l ) {
43
- return colorFunctions . hsla ( h , s , l , 1.0 ) ;
67
+ var color = colorFunctions . hsla ( h , s , l , 1.0 ) ;
68
+ if ( color ) {
69
+ color . value = 'hsl' ;
70
+ return color ;
71
+ }
44
72
} ,
45
73
hsla : function ( h , s , l , a ) {
74
+ try {
75
+ if ( h instanceof Color ) {
76
+ if ( s ) {
77
+ a = number ( s ) ;
78
+ } else {
79
+ a = h . alpha ;
80
+ }
81
+ return new Color ( h . rgb , a , 'hsla' ) ;
82
+ }
46
83
47
- var m1 , m2 ;
84
+ var m1 , m2 ;
48
85
49
- function hue ( h ) {
50
- h = h < 0 ? h + 1 : ( h > 1 ? h - 1 : h ) ;
51
- if ( h * 6 < 1 ) {
52
- return m1 + ( m2 - m1 ) * h * 6 ;
53
- }
54
- else if ( h * 2 < 1 ) {
55
- return m2 ;
56
- }
57
- else if ( h * 3 < 2 ) {
58
- return m1 + ( m2 - m1 ) * ( 2 / 3 - h ) * 6 ;
59
- }
60
- else {
61
- return m1 ;
86
+ function hue ( h ) {
87
+ h = h < 0 ? h + 1 : ( h > 1 ? h - 1 : h ) ;
88
+ if ( h * 6 < 1 ) {
89
+ return m1 + ( m2 - m1 ) * h * 6 ;
90
+ }
91
+ else if ( h * 2 < 1 ) {
92
+ return m2 ;
93
+ }
94
+ else if ( h * 3 < 2 ) {
95
+ return m1 + ( m2 - m1 ) * ( 2 / 3 - h ) * 6 ;
96
+ }
97
+ else {
98
+ return m1 ;
99
+ }
62
100
}
63
- }
64
101
65
- h = ( number ( h ) % 360 ) / 360 ;
66
- s = clamp ( number ( s ) ) ; l = clamp ( number ( l ) ) ; a = clamp ( number ( a ) ) ;
102
+ h = ( number ( h ) % 360 ) / 360 ;
103
+ s = clamp ( number ( s ) ) ; l = clamp ( number ( l ) ) ; a = clamp ( number ( a ) ) ;
67
104
68
- m2 = l <= 0.5 ? l * ( s + 1 ) : l + s - l * s ;
69
- m1 = l * 2 - m2 ;
105
+ m2 = l <= 0.5 ? l * ( s + 1 ) : l + s - l * s ;
106
+ m1 = l * 2 - m2 ;
70
107
71
- return colorFunctions . rgba ( hue ( h + 1 / 3 ) * 255 ,
72
- hue ( h ) * 255 ,
73
- hue ( h - 1 / 3 ) * 255 ,
74
- a ) ;
108
+ var rgb = [
109
+ hue ( h + 1 / 3 ) * 255 ,
110
+ hue ( h ) * 255 ,
111
+ hue ( h - 1 / 3 ) * 255
112
+ ] ;
113
+ a = number ( a ) ;
114
+ return new Color ( rgb , a , 'hsla' ) ;
115
+ }
116
+ catch ( e ) { }
75
117
} ,
76
118
77
119
hsv : function ( h , s , v ) {
@@ -159,7 +201,7 @@ colorFunctions = {
159
201
hsl . s += amount . value / 100 ;
160
202
}
161
203
hsl . s = clamp ( hsl . s ) ;
162
- return hsla ( hsl ) ;
204
+ return hsla ( color , hsl ) ;
163
205
} ,
164
206
desaturate : function ( color , amount , method ) {
165
207
var hsl = color . toHSL ( ) ;
@@ -171,7 +213,7 @@ colorFunctions = {
171
213
hsl . s -= amount . value / 100 ;
172
214
}
173
215
hsl . s = clamp ( hsl . s ) ;
174
- return hsla ( hsl ) ;
216
+ return hsla ( color , hsl ) ;
175
217
} ,
176
218
lighten : function ( color , amount , method ) {
177
219
var hsl = color . toHSL ( ) ;
@@ -183,7 +225,7 @@ colorFunctions = {
183
225
hsl . l += amount . value / 100 ;
184
226
}
185
227
hsl . l = clamp ( hsl . l ) ;
186
- return hsla ( hsl ) ;
228
+ return hsla ( color , hsl ) ;
187
229
} ,
188
230
darken : function ( color , amount , method ) {
189
231
var hsl = color . toHSL ( ) ;
@@ -195,7 +237,7 @@ colorFunctions = {
195
237
hsl . l -= amount . value / 100 ;
196
238
}
197
239
hsl . l = clamp ( hsl . l ) ;
198
- return hsla ( hsl ) ;
240
+ return hsla ( color , hsl ) ;
199
241
} ,
200
242
fadein : function ( color , amount , method ) {
201
243
var hsl = color . toHSL ( ) ;
@@ -207,7 +249,7 @@ colorFunctions = {
207
249
hsl . a += amount . value / 100 ;
208
250
}
209
251
hsl . a = clamp ( hsl . a ) ;
210
- return hsla ( hsl ) ;
252
+ return hsla ( color , hsl ) ;
211
253
} ,
212
254
fadeout : function ( color , amount , method ) {
213
255
var hsl = color . toHSL ( ) ;
@@ -219,22 +261,22 @@ colorFunctions = {
219
261
hsl . a -= amount . value / 100 ;
220
262
}
221
263
hsl . a = clamp ( hsl . a ) ;
222
- return hsla ( hsl ) ;
264
+ return hsla ( color , hsl ) ;
223
265
} ,
224
266
fade : function ( color , amount ) {
225
267
var hsl = color . toHSL ( ) ;
226
268
227
269
hsl . a = amount . value / 100 ;
228
270
hsl . a = clamp ( hsl . a ) ;
229
- return hsla ( hsl ) ;
271
+ return hsla ( color , hsl ) ;
230
272
} ,
231
273
spin : function ( color , amount ) {
232
274
var hsl = color . toHSL ( ) ;
233
275
var hue = ( hsl . h + amount . value ) % 360 ;
234
276
235
277
hsl . h = hue < 0 ? 360 + hue : hue ;
236
278
237
- return hsla ( hsl ) ;
279
+ return hsla ( color , hsl ) ;
238
280
} ,
239
281
//
240
282
// Copyright (c) 2006-2009 Hampton Catlin, Natalie Weizenbaum, and Chris Eppstein
@@ -338,16 +380,17 @@ colorFunctions = {
338
380
} ,
339
381
color : function ( c ) {
340
382
if ( ( c instanceof Quoted ) &&
341
- ( / ^ # ( [ a - f 0 - 9 ] { 6 } | [ a - f 0 - 9 ] { 3 } ) $ / i. test ( c . value ) ) ) {
342
- return new Color ( c . value . slice ( 1 ) ) ;
383
+ ( / ^ # ( [ A - F a - f 0 - 9 ] { 8 } | [ A - F a - f 0 - 9 ] { 6 } | [ A - F a - f 0 - 9 ] { 3 , 4 } ) $ / i. test ( c . value ) ) ) {
384
+ var val = c . value . slice ( 1 ) ;
385
+ return new Color ( val , undefined , '#' + val ) ;
343
386
}
344
387
if ( ( c instanceof Color ) || ( c = Color . fromKeyword ( c . value ) ) ) {
345
388
c . value = undefined ;
346
389
return c ;
347
390
}
348
391
throw {
349
392
type : 'Argument' ,
350
- message : 'argument must be a color keyword or 3/6 digit hex e.g. #FFF'
393
+ message : 'argument must be a color keyword or 3|4|6|8 digit hex e.g. #FFF'
351
394
} ;
352
395
} ,
353
396
tint : function ( color , amount ) {
0 commit comments