1
1
/**
2
2
* Require constants
3
3
*/
4
- const Color = require ( 'color' ) ;
5
- const round10 = require ( 'round10' ) . round10 ;
6
- const easeInSine = require ( 'eases/sine-in' ) ;
7
- const easeOutSine = require ( 'eases/sine-out' ) ;
4
+ const Color = require ( 'color' ) ;
5
+ const round10 = require ( 'round10' ) . round10 ;
6
+ const easeInSine = require ( 'eases/sine-in' ) ;
7
+ const easeOutSine = require ( 'eases/sine-out' ) ;
8
8
const easeInOutSine = require ( 'eases/sine-in-out' ) ;
9
- const easeInQuad = require ( 'eases/quad-in' ) ;
10
- const easeOutQuad = require ( 'eases/quad-out' ) ;
9
+ const easeInQuad = require ( 'eases/quad-in' ) ;
10
+ const easeOutQuad = require ( 'eases/quad-out' ) ;
11
11
const easeInOutQuad = require ( 'eases/quad-in-out' ) ;
12
12
13
13
/**
@@ -27,23 +27,16 @@ const scrimCoordinates = {
27
27
0.94 : '80.36%' ,
28
28
0.97 : '87.18%' ,
29
29
0.99 : '93.73%'
30
- }
31
-
32
- /**
33
- * Calculate the color stops based on start+stopColor in an array and easingType
34
- */
35
- exports . getColorStops = function ( colors , easingType , delta , alphaDecimals ) {
36
- colors = transparentFix ( colors ) ;
37
- let gradientCoordinates = getCoordinates ( easingType , delta ) ;
38
- let colorStops = '' ;
39
- for ( let ammount in gradientCoordinates ) {
40
- let color = Color ( colors [ 1 ] ) . mix ( Color ( colors [ 0 ] ) , ammount ) ;
41
- color = roundHslAlpha ( color . hsl ( ) . string ( ) , alphaDecimals ) ;
42
- colorStops += `, ${ color } ${ gradientCoordinates [ ammount ] } ` ;
43
- }
44
- colorStops += `, ${ colors [ 1 ] } 100%` ;
45
- return colorStops ;
46
30
} ;
31
+ const supportedGradients = [
32
+ 'ease-in-sine-gradient' ,
33
+ 'ease-out-sine-gradient' ,
34
+ 'ease-in-out-sine-gradient' ,
35
+ 'ease-in-quad-gradient' ,
36
+ 'ease-out-quad-gradient' ,
37
+ 'ease-in-out-quad-gradient' ,
38
+ 'scrim-gradient'
39
+ ] ;
47
40
48
41
/**
49
42
* If a color is transparent then convert it to a transparent of the sibling
@@ -53,13 +46,50 @@ function transparentFix(colors) {
53
46
? Color ( colors [ Math . abs ( i - 1 ) ] ) . alpha ( 0 ) . hsl ( ) . string ( )
54
47
: color
55
48
) ;
56
- } ;
49
+ }
50
+
51
+ /**
52
+ * Test if new coordinate is far enough away from old coordinate
53
+ */
54
+ function isFarEnough ( x , y , xOld , yOld , delta ) {
55
+ return Math . sqrt ( ( ( x - xOld ) ** 2 ) + ( ( y - yOld ) ** 2 ) ) > delta ;
56
+ }
57
+
58
+ /**
59
+ * Convert decimal number to percentage string
60
+ */
61
+ function getPercentage ( number ) {
62
+ return `${ round10 ( number * 100 , - 1 ) } %` ;
63
+ }
64
+
65
+ /**
66
+ * Easing functions switcheroo
67
+ */
68
+ function ease ( x , type ) {
69
+ switch ( type ) {
70
+ case 'ease-in-sine-gradient' :
71
+ return easeInSine ( x ) ;
72
+ case 'ease-out-sine-gradient' :
73
+ return easeOutSine ( x ) ;
74
+ case 'ease-in-out-sine-gradient' :
75
+ return easeInOutSine ( x ) ;
76
+ case 'ease-in-quad-gradient' :
77
+ return easeInQuad ( x ) ;
78
+ case 'ease-out-quad-gradient' :
79
+ return easeOutQuad ( x ) ;
80
+ case 'ease-in-out-quad-gradient' :
81
+ return easeInOutQuad ( x ) ;
82
+ default :
83
+ console . log ( `Sorry, easing gradient does not support ${ type } .` ) ;
84
+ return null ;
85
+ }
86
+ }
57
87
58
88
/**
59
89
* Get coordinates based on easing function.
60
90
* Delta checks ensures there's roughly the same distance between coordinates.
61
91
*/
62
- function getCoordinates ( easingFunction , delta ) {
92
+ function getCoordinates ( easingFunction , precision ) {
63
93
if ( easingFunction === 'scrim-gradient' ) return scrimCoordinates ;
64
94
65
95
const yIncrements = 0.001 ;
@@ -72,6 +102,7 @@ function getCoordinates(easingFunction, delta) {
72
102
let xOld = 0 ;
73
103
let yOld = 0 ;
74
104
let firstTime = true ;
105
+ let delta = precision ;
75
106
76
107
while ( firstTime || ! isFarEnough ( 1 , 1 , xOld , yOld , delta - deltaTolerance ) ) {
77
108
if ( firstTime ) {
@@ -81,7 +112,7 @@ function getCoordinates(easingFunction, delta) {
81
112
y = 0 ;
82
113
xOld = 0 ;
83
114
yOld = 0 ;
84
- delta = delta - deltaAdjust ;
115
+ delta -= deltaAdjust ;
85
116
coordinates = { } ;
86
117
}
87
118
while ( y <= 1 ) {
@@ -96,61 +127,76 @@ function getCoordinates(easingFunction, delta) {
96
127
}
97
128
}
98
129
return coordinates ;
99
- } ;
130
+ }
100
131
101
132
/**
102
- * Easing functions switcheroo
133
+ * Round alpha in hsl colors to alphaDecimals
103
134
*/
104
- function ease ( x , type ) {
105
- switch ( type ) {
106
- case 'ease-in-sine-gradient' :
107
- return easeInSine ( x ) ;
108
- break ;
109
- case 'ease-out-sine-gradient' :
110
- return easeOutSine ( x ) ;
111
- break ;
112
- case 'ease-in-out-sine-gradient' :
113
- return easeInOutSine ( x ) ;
114
- break ;
115
- case 'ease-in-quad-gradient' :
116
- return easeInQuad ( x ) ;
117
- break ;
118
- case 'ease-out-quad-gradient' :
119
- return easeOutQuad ( x ) ;
120
- break ;
121
- case 'ease-in-out-quad-gradient' :
122
- return easeInOutQuad ( x ) ;
123
- break ;
124
- default :
125
- console . log ( `Sorry, easing gradient does not support ${ type } .` ) ;
126
- }
135
+ function roundHslAlpha ( color , alphaDecimals ) {
136
+ const prefix = color . substring ( 0 , color . indexOf ( '(' ) ) ;
137
+ const values = color
138
+ . substring ( color . indexOf ( '(' ) + 1 , color . indexOf ( ')' ) )
139
+ . split ( ',' )
140
+ . map ( string => string . indexOf ( '%' ) === - 1
141
+ ? round10 ( Number ( string ) , - alphaDecimals )
142
+ : string . trim ( )
143
+ ) ;
144
+ return `${ prefix } (${ values . join ( ', ' ) } )` ;
145
+ }
146
+
147
+ /**
148
+ * Calculate the color stops based on start+stopColor in an array and easingType
149
+ */
150
+ exports . getColorStops = function getColorStops (
151
+ colors ,
152
+ easingType ,
153
+ delta ,
154
+ alphaDecimals
155
+ ) {
156
+ const fixedColors = transparentFix ( colors ) ;
157
+ const gradientCoordinates = getCoordinates ( easingType , delta ) ;
158
+ let colorStops = '' ;
159
+ Object . keys ( gradientCoordinates ) . forEach ( ( ammount ) => {
160
+ // https://github.com/Qix-/color
161
+ let color = Color ( fixedColors [ 1 ] ) . mix ( Color ( fixedColors [ 0 ] ) , ammount ) ;
162
+ color = color . hsl ( ) . string ( ) ;
163
+ color = roundHslAlpha ( color , alphaDecimals ) ;
164
+ colorStops += `${ color } ${ gradientCoordinates [ ammount ] } , ` ;
165
+ } ) ;
166
+ colorStops += `${ colors [ 1 ] } 100%` ;
167
+ return colorStops ;
127
168
} ;
128
169
170
+
129
171
/**
130
- * Convert decimal number to percentage string
172
+ * Check if a string matches one of the supported gradients
131
173
*/
132
- function getPercentage ( number ) {
133
- return round10 ( number * 100 , - 1 ) + '%' ;
174
+ exports . isEasingGradient = function isEasingGradient ( str ) {
175
+ return new RegExp ( supportedGradients . join ( '|' ) ) . test ( str ) ;
134
176
} ;
135
177
136
178
/**
137
- * Test if new coordinate is far enough away from old coordinate
179
+ * Convert a function object to a word object
138
180
*/
139
- function isFarEnough ( x , y , xOld , yOld , delta ) {
140
- return Math . sqrt ( ( x - xOld ) ** 2 + ( y - yOld ) ** 2 ) > delta ;
181
+ exports . functionToWord = function functionToWord ( obj ) {
182
+ const object = obj ;
183
+ const array = [ ] ;
184
+ // TODO: Make eslint happy
185
+ /* eslint-disable no-restricted-syntax */
186
+ for ( const objNode of object . nodes ) {
187
+ if ( objNode . type === 'word' ) {
188
+ array . push ( objNode . value ) ;
189
+ }
190
+ }
191
+ /* eslint-enable no-restricted-syntax */
192
+ object . value = `${ obj . value } (${ array . join ( ) } )` ;
193
+ object . type = 'word' ;
194
+ return object ;
141
195
} ;
142
196
143
197
/**
144
- * Round alpha in hsl colors to alphaDecimals
198
+ * Test if two colors are identical
145
199
*/
146
- function roundHslAlpha ( color , alphaDecimals ) {
147
- let prefix = color . substring ( 0 , color . indexOf ( '(' ) ) ;
148
- let values = color
149
- . substring ( color . indexOf ( '(' ) + 1 , color . indexOf ( ')' ) )
150
- . split ( ',' )
151
- . map ( string => string . indexOf ( '%' ) === - 1
152
- ? round10 ( Number ( string ) , - alphaDecimals )
153
- : string . trim ( )
154
- ) ;
155
- return `${ prefix } (${ values . join ( ', ' ) } )` ;
200
+ exports . isSameColor = function isSameColor ( colorA , colorB ) {
201
+ return Color ( colorA ) . hsl ( ) . string ( ) === Color ( colorB ) . hsl ( ) . string ( ) ;
156
202
} ;
0 commit comments