1
1
/*
2
2
TODO: write tests (lazy) like always right? <3
3
3
*/
4
- const parser = require ( " gradient-parser" ) ;
4
+ const parser = require ( ' gradient-parser' ) ;
5
5
6
6
const getColor = color => {
7
7
switch ( color . type ) {
8
- case " hex" :
8
+ case ' hex' :
9
9
return `#${ color . value } ` ;
10
- case " literal" :
10
+ case ' literal' :
11
11
return color . value ;
12
12
default :
13
- return `${ color . type } (${ color . value . join ( "," ) } )` ;
13
+ return `${ color . type } (${ color . value . join ( ',' ) } )` ;
14
14
}
15
15
} ;
16
16
@@ -21,12 +21,7 @@ const getColorsAndLocations = (colorStops, maxWidth) =>
21
21
22
22
// PX value for location will break!
23
23
// TODO Make it happen for px + repeat?
24
- const locationValue = getPixelsForColor (
25
- color ,
26
- colorStops . length ,
27
- index ,
28
- maxWidth
29
- ) ;
24
+ const locationValue = getPixelsForColor ( color , colorStops . length , index , maxWidth ) ;
30
25
acc [ "locations" ] = [ ...acc . locations , locationValue ] ;
31
26
32
27
return acc ;
@@ -37,87 +32,92 @@ const getColorsAndLocations = (colorStops, maxWidth) =>
37
32
const getPixelsForColor = ( color , colorsLength , index , maxWidth ) => {
38
33
const { length } = color ;
39
34
if ( ! length ) {
40
- return ( 1 / ( colorsLength - 1 ) ) * index ;
35
+ return ( 1 / ( colorsLength - 1 ) ) * index ;
41
36
}
42
37
if ( length . type === "px" ) {
43
- return parseFloat ( length . value ) ;
38
+ return parseFloat ( length . value ) ;
44
39
}
45
- if ( length . type === "%" ) {
46
- if ( maxWidth ) {
47
- return ( parseFloat ( length . value ) * maxWidth ) / 100 ;
48
- } else {
49
- return length . value / 100 ;
50
- }
51
- }
52
- } ;
53
- const getRepeatingColorsAndLocations = ( colorStops , sizes ) => {
54
- const { width : maxWidth , height : maxHeight } = sizes ;
55
-
56
- if ( ! maxWidth && ! maxHeight ) {
57
- throw new Error (
58
- "You have to define width and height for repeating gradient to work"
59
- ) ;
40
+ if ( length . type === "%" ) {
41
+ if ( maxWidth ) {
42
+ return parseFloat ( length . value ) * maxWidth / 100 ;
43
+ } else {
44
+ return length . value / 100 ;
45
+ }
60
46
}
47
+ }
61
48
62
- const {
63
- colors : initialColors ,
64
- locations : initialLocations
65
- } = getColorsAndLocations ( colorStops , maxWidth ) ;
49
+ const getRepeatingColorsAndLocations = ( colorStops , sizes ) => {
50
+ const { width : maxWidth , height : maxHeight } = sizes ;
51
+ const { colors : initialColors , locations : initialLocations } = getColorsAndLocations ( colorStops , maxWidth ) ;
66
52
const maxValue = parseFloat ( initialLocations . slice ( - 1 ) [ 0 ] ) ;
67
53
const increment = maxValue / maxWidth ;
68
- const maxChunks = Math . round ( maxWidth / maxValue ) + 1 ;
54
+ const maxChunks = Math . round ( ( maxWidth / maxValue ) ) ;
69
55
const locations = [ ...Array ( maxChunks ) . keys ( ) ] . reduce ( ( acc , i ) => {
70
- return [ ...acc , ...initialLocations . map ( j => j / maxWidth + increment * i ) ] ;
56
+ return [ ...acc , ...initialLocations . map ( j => {
57
+ console . log ( )
58
+ return j / maxWidth + increment * i
59
+ } ) ]
71
60
} , [ ] ) ;
72
- const colors = locations . map (
73
- ( _ , i ) => initialColors [ i % initialColors . length ]
74
- ) ;
61
+ const colors = locations . map ( ( _ , i ) => initialColors [ i % initialColors . length ] )
75
62
76
- return { locations , colors } ;
63
+ return { colors , locations } ;
77
64
} ;
65
+
78
66
const getVectorsByDirection = direction => {
79
67
switch ( direction ) {
80
- case " top" :
68
+ case ' top' :
81
69
return getVectorsByAngle ( 0 ) ;
82
- case " right" :
70
+ case ' right' :
83
71
return getVectorsByAngle ( 90 ) ;
84
- case " bottom" :
72
+ case ' bottom' :
85
73
return getVectorsByAngle ( 180 ) ;
86
- case " left" :
74
+ case ' left' :
87
75
return getVectorsByAngle ( 270 ) ;
88
- case " left top" :
76
+ case ' left top' :
89
77
return getVectorsByAngle ( 270 + 45 ) ;
90
- case " left bottom" :
78
+ case ' left bottom' :
91
79
return getVectorsByAngle ( 180 + 45 ) ;
92
- case " right top" :
80
+ case ' right top' :
93
81
return getVectorsByAngle ( 45 ) ;
94
- case " right bottom" :
82
+ case ' right bottom' :
95
83
return getVectorsByAngle ( 90 + 45 ) ;
96
84
}
97
85
} ;
98
- const round = number => Math . round ( number * 1000 ) / 1000 ;
99
- const degreesToRadians = function ( degrees ) {
100
- return ( degrees * Math . PI ) / 180 ;
86
+
87
+ const round = ( number ) => Math . round ( number * 10000 ) / 10000 ;
88
+ var pointFromAngleAndDistance = function ( origin , angle , distance ) {
89
+ angle = degreesToRadians ( angle ) ;
90
+ return {
91
+ x : distance * Math . cos ( angle ) + origin . x ,
92
+ y : distance * Math . sin ( angle ) + origin . y
93
+ } ;
101
94
} ;
95
+ var degreesToRadians = function ( degrees ) { return degrees * Math . PI / 180 ; } ;
96
+
102
97
const getVectorsByAngle = alfa => {
103
- const angle = degreesToRadians ( alfa ) ;
98
+ const angle = alfa * Math . PI / 180 ;
104
99
105
- let gradientLineLength = round (
106
- Math . abs ( Math . sin ( angle ) ) + Math . abs ( Math . cos ( angle ) )
107
- ) ;
100
+ let gradientLineLength =
101
+ round ( Math . abs ( Math . sin ( angle ) ) + Math . abs ( Math . cos ( angle ) ) ) ;
108
102
let center = { x : 0.5 , y : 0.5 } ;
109
103
110
104
let yDiff = ( Math . sin ( angle - Math . PI / 2 ) * gradientLineLength ) / 2 ;
111
105
let xDiff = ( Math . cos ( angle - Math . PI / 2 ) * gradientLineLength ) / 2 ;
112
106
113
107
return {
114
- start : [ center . x - xDiff , center . y - yDiff ] ,
115
- end : [ center . x + xDiff , center . y + yDiff ]
108
+ start : {
109
+ x : center . x - xDiff ,
110
+ y : center . y - yDiff
111
+ } ,
112
+ end : {
113
+ x : center . x + xDiff ,
114
+ y : center . y + yDiff ,
115
+ } ,
116
116
} ;
117
117
} ;
118
118
119
119
const getVectorsByOrientation = orientation => {
120
- return orientation . type === " directional"
120
+ return orientation . type === ' directional'
121
121
? getVectorsByDirection ( orientation . value )
122
122
: getVectorsByAngle ( orientation . value ) ;
123
123
} ;
@@ -128,10 +128,7 @@ const generateGradient = (gradient, sizes) => {
128
128
if ( type === "radial-gradient" ) {
129
129
return "Only linear-gradient type is supported for now" ;
130
130
}
131
- const colorsAndLocations =
132
- type === "linear-gradient"
133
- ? getColorsAndLocations ( colorStops )
134
- : getRepeatingColorsAndLocations ( colorStops , sizes ) ;
131
+ const colorsAndLocations = type === "linear-gradient" ? getColorsAndLocations ( colorStops ) : getRepeatingColorsAndLocations ( colorStops , sizes ) ;
135
132
136
133
return {
137
134
...colorsAndLocations ,
0 commit comments