1
1
var MersenneTwister = require ( 'mersenne-twister' ) ;
2
2
var paperGen = require ( './paper' )
3
+ var Color = require ( 'color' )
3
4
var colors = require ( './colors' )
4
5
var shapeCount = 4
5
6
var svgns = 'http://www.w3.org/2000/svg'
@@ -69,94 +70,9 @@ function genColor(colors) {
69
70
var wobble = 30
70
71
function hueShift ( colors , generator ) {
71
72
var amount = ( generator . random ( ) * 30 ) - ( wobble / 2 )
72
- var rotate = ( hex ) => colorRotate ( hex , amount )
73
- return colors . map ( rotate )
73
+ return colors . map ( function ( hex ) {
74
+ var color = Color ( hex )
75
+ color . rotate ( amount )
76
+ return color . hexString ( )
77
+ } )
74
78
}
75
-
76
- function colorRotate ( hex , degrees ) {
77
- var hsl = hexToHSL ( hex )
78
- var hue = hsl . h
79
- hue = ( hue + degrees ) % 360 ;
80
- hue = hue < 0 ? 360 + hue : hue ;
81
- hsl . h = hue ;
82
- return HSLToHex ( hsl ) ;
83
- }
84
-
85
- function hexToHSL ( hex ) {
86
- // Convert hex to RGB first
87
- var r = "0x" + hex [ 1 ] + hex [ 2 ] ;
88
- var g = "0x" + hex [ 3 ] + hex [ 4 ] ;
89
- var b = "0x" + hex [ 5 ] + hex [ 6 ] ;
90
- // Then to HSL
91
- r /= 255 ;
92
- g /= 255 ;
93
- b /= 255 ;
94
- var cmin = Math . min ( r , g , b ) ,
95
- cmax = Math . max ( r , g , b ) ,
96
- delta = cmax - cmin ,
97
- h = 0 ,
98
- s = 0 ,
99
- l = 0 ;
100
-
101
- if ( delta == 0 )
102
- h = 0 ;
103
- else if ( cmax == r )
104
- h = ( ( g - b ) / delta ) % 6 ;
105
- else if ( cmax == g )
106
- h = ( b - r ) / delta + 2 ;
107
- else
108
- h = ( r - g ) / delta + 4 ;
109
-
110
- h = Math . round ( h * 60 ) ;
111
-
112
- if ( h < 0 )
113
- h += 360 ;
114
-
115
- l = ( cmax + cmin ) / 2 ;
116
- s = delta == 0 ? 0 : delta / ( 1 - Math . abs ( 2 * l - 1 ) ) ;
117
- s = + ( s * 100 ) . toFixed ( 1 ) ;
118
- l = + ( l * 100 ) . toFixed ( 1 ) ;
119
-
120
- return { h, s, l}
121
- }
122
-
123
- function HSLToHex ( hsl ) {
124
- var { h, s, l} = hsl
125
- s /= 100 ;
126
- l /= 100 ;
127
-
128
- let c = ( 1 - Math . abs ( 2 * l - 1 ) ) * s ,
129
- x = c * ( 1 - Math . abs ( ( h / 60 ) % 2 - 1 ) ) ,
130
- m = l - c / 2 ,
131
- r = 0 ,
132
- g = 0 ,
133
- b = 0 ;
134
-
135
- if ( 0 <= h && h < 60 ) {
136
- r = c ; g = x ; b = 0 ;
137
- } else if ( 60 <= h && h < 120 ) {
138
- r = x ; g = c ; b = 0 ;
139
- } else if ( 120 <= h && h < 180 ) {
140
- r = 0 ; g = c ; b = x ;
141
- } else if ( 180 <= h && h < 240 ) {
142
- r = 0 ; g = x ; b = c ;
143
- } else if ( 240 <= h && h < 300 ) {
144
- r = x ; g = 0 ; b = c ;
145
- } else if ( 300 <= h && h < 360 ) {
146
- r = c ; g = 0 ; b = x ;
147
- }
148
- // Having obtained RGB, convert channels to hex
149
- r = Math . round ( ( r + m ) * 255 ) . toString ( 16 ) ;
150
- g = Math . round ( ( g + m ) * 255 ) . toString ( 16 ) ;
151
- b = Math . round ( ( b + m ) * 255 ) . toString ( 16 ) ;
152
-
153
- // Prepend 0s, if necessary
154
- if ( r . length == 1 )
155
- r = "0" + r ;
156
- if ( g . length == 1 )
157
- g = "0" + g ;
158
- if ( b . length == 1 )
159
- b = "0" + b ;
160
-
161
- return "#" + r + g + b ;
162
- }
0 commit comments