1
1
const postcss = require ( 'postcss' ) ;
2
2
const valueParser = require ( 'postcss-value-parser' ) ;
3
- const tinyColor = require ( 'tinycolor2' ) ;
3
+ const Color = require ( 'color' ) ;
4
+ const round10 = require ( 'round10' ) . round10 ;
5
+ // Easing functions
4
6
const easeInSine = require ( 'eases/sine-in' ) ;
5
7
const easeOutSine = require ( 'eases/sine-out' ) ;
6
8
const easeInOutSine = require ( 'eases/sine-in-out' ) ;
@@ -64,14 +66,16 @@ module.exports = postcss.plugin('easing-gradient', function easingGradient() {
64
66
// them back to words.
65
67
if ( param . type === 'function' ) functionToWord ( param ) ;
66
68
67
- // If a param is a color and if so then add to colors array.
68
- //
69
- if ( tinyColor ( param . value ) . isValid ( ) ) {
70
- colors . push ( param . value ) ;
71
-
72
- // A gradient direction is written using words and spaces
73
- } else if ( param . type === 'word' || param . type === 'space' ) {
74
- direction += param . value ;
69
+ // If a param is a color and then add to colors array.
70
+ try {
71
+ Color ( param . value ) && colors . push ( param . value ) ;
72
+ }
73
+ catch ( error ) {
74
+ // Test if it's a word or space and assume that's part of the
75
+ // direction anotation — e.g. 'to'+' '+'top' or '45deg'
76
+ if ( param . type === 'word' || param . type === 'space' ) {
77
+ direction += param . value ;
78
+ }
75
79
}
76
80
}
77
81
@@ -117,6 +121,28 @@ function functionToWord(obj) {
117
121
return obj ;
118
122
}
119
123
124
+ /**
125
+ * Mix colors
126
+ */
127
+ function mixColors ( colorA , colorB , ammount ) {
128
+ return Color ( colorA ) . mix ( Color ( colorB ) , ammount ) . hsl ( ) . string ( ) ;
129
+ }
130
+
131
+ /**
132
+ * Round hue and alpha in hsl colors to 3 decimals
133
+ */
134
+ function roundHueAlpha ( color ) {
135
+ let prefix = color . substring ( 0 , color . indexOf ( '(' ) ) ;
136
+ let values = color
137
+ . substring ( color . indexOf ( '(' ) + 1 , color . indexOf ( ')' ) )
138
+ . split ( ',' )
139
+ . map ( string => string . indexOf ( '%' ) === - 1
140
+ ? round10 ( Number ( string ) , - 3 )
141
+ : string . trim ( )
142
+ ) ;
143
+ return `${ prefix } (${ values . join ( ', ' ) } )` ;
144
+ }
145
+
120
146
/**
121
147
* Calculate the color stops based on start+stopColor in an array and easingType
122
148
*/
@@ -125,12 +151,11 @@ function getColorStops(colors, easingType) {
125
151
let gradientCoordinates = getCoordinates ( easingType ) ;
126
152
let colorStops = '' ;
127
153
for ( let ammount in gradientCoordinates ) {
128
- let color = tinyColor
129
- . mix ( colors [ 0 ] , colors [ 1 ] , ammount * 100 )
130
- . toHslString ( ) ;
154
+ let color = mixColors ( colors [ 1 ] , colors [ 0 ] , ammount ) ;
155
+ color = roundHueAlpha ( color ) ;
131
156
colorStops += `, ${ color } ${ gradientCoordinates [ ammount ] } ` ;
132
157
}
133
- colorStops += `, ${ colors [ 1 ] } 100%` ;
158
+ colorStops += `, ${ roundHueAlpha ( colors [ 1 ] ) } 100%` ;
134
159
return colorStops ;
135
160
} ;
136
161
@@ -149,7 +174,7 @@ function transparentFix(colors) {
149
174
*/
150
175
function transparentToAlpha ( colors , color ) {
151
176
let otherColor = colors [ Math . abs ( colors . indexOf ( color ) - 1 ) ] ;
152
- let transparentOfOtherColor = tinyColor ( otherColor ) . setAlpha ( 0 ) . toHslString ( ) ;
177
+ let transparentOfOtherColor = Color ( otherColor ) . alpha ( 0 ) . hsl ( ) . string ( ) ;
153
178
return transparentOfOtherColor ;
154
179
}
155
180
0 commit comments