-
Notifications
You must be signed in to change notification settings - Fork 51
/
color_picker.module.coffee
351 lines (270 loc) · 7.4 KB
/
color_picker.module.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
Popup = require('./popup')
Color = require('app/models/properties/color')
class Canvas extends Spine.Controller
tag: 'canvas'
width: 100
height: 100
events:
'mousedown': 'drag'
constructor: ->
super
@el.attr(
width: @width,
height: @height
)
@ctx = @el[0].getContext('2d')
val: (x, y) ->
data = @ctx.getImageData(x, y, 1, 1).data
new Color(data[0], data[1], data[2])
drag: (e) ->
@el.mousemove(@over)
$(document).mouseup(@drop)
@over(e)
over: (e) =>
e.preventDefault()
offset = @el.offset()
x = e.pageX - offset.left
y = e.pageY - offset.top
@trigger('change', @val(x, y))
drop: =>
@el.unbind('mousemove', @over)
$(document).unbind('mouseup', @drop)
release: ->
super
@drop()
class Gradient extends Canvas
className: 'gradient'
width: 250
height: 250
constructor: ->
super
@color or= new Color.Black
@setColor(@color)
setColor: (@color) ->
@render()
colorWithAlpha: (a) ->
color = @color.clone()
color.a = a
color
renderGradient: (xy, colors...) ->
gradient = @ctx.createLinearGradient(0, 0, xy...)
gradient.addColorStop(0, colors.shift()?.toString())
for color, index in colors
gradient.addColorStop(index + 1 / colors.length, color.toString())
@ctx.fillStyle = gradient
@ctx.fillRect(0, 0, @width, @height)
render: ->
@ctx.clearRect(0, 0, @width, @height)
@renderGradient(
[@width, 0],
new Color(255, 255, 255),
new Color(255, 255, 255)
)
@renderGradient(
[@width, 0],
@colorWithAlpha(0),
@colorWithAlpha(1)
)
gradient = @ctx.createLinearGradient(0, 0, -6, @height)
gradient.addColorStop(0, new Color(0, 0, 0, 0).toString())
gradient.addColorStop(1, new Color(0, 0, 0, 1).toString())
@ctx.fillStyle = gradient
@ctx.fillRect(0, 0, @width, @height)
class Spectrum extends Canvas
className: 'spectrum'
width: 25
height: 250
constructor: ->
super
@color or= new Color(0, 0, 0)
@setColor(@color)
render: ->
@ctx.clearRect(0, 0, @width, @height)
gradient = @ctx.createLinearGradient(0, 0, 0, @height)
gradient.addColorStop(0, 'rgb(255, 0, 0)')
gradient.addColorStop(0.16, 'rgb(255, 0, 255)')
gradient.addColorStop(0.33, 'rgb(0, 0, 255)')
gradient.addColorStop(0.50, 'rgb(0, 255, 255)')
gradient.addColorStop(0.67, 'rgb(0, 255, 0)')
gradient.addColorStop(0.83, 'rgb(255, 255, 0)')
gradient.addColorStop(1, 'rgb(255, 0, 0)')
@ctx.fillStyle = gradient
@ctx.fillRect(0, 0, @width, @height)
setColor: (@color) ->
@render()
class Alpha extends Canvas
className: 'alpha'
width: 25
height: 250
constructor: ->
super
@color or= new Color(0, 0, 0)
@setColor(@color)
render: ->
@ctx.clearRect(0, 0, @width, @height)
gradient = @ctx.createLinearGradient(0, 0, 0, @height)
gradient.addColorStop(0, @color.clone().set(a: 0).toString())
gradient.addColorStop(0.9, @color.clone().set(a: 1).toString())
@ctx.fillStyle = gradient
@ctx.fillRect(0, 0, @width, @height)
setColor: (@color) ->
@render()
val: (x, y) ->
data = @ctx.getImageData(x, y, 1, 1).data
@color.set(a: Math.round((data[3] / 255) * 100) / 100)
class Display extends Spine.Controller
tag: 'article'
elements:
'input[name=hex]': '$hex'
'input[name=r]': '$r'
'input[name=g]': '$g'
'input[name=b]': '$b'
'input[name=a]': '$a'
'.preview .inner': '$preview'
'.preview .original': '$original'
events:
'change input:not([name=hex])': 'change'
'change input[name=hex]': 'changeHex'
constructor: ->
super
@color or= new Color(0, 0, 0)
@render()
@setColor(@color)
render: ->
@html JST['lib/views/color_picker'](this)
if @original
@$original.css(background: @original.toString())
setColor: (@color) ->
@$r.val @color.r
@$g.val @color.g
@$b.val @color.b
@$a.val Math.round(@color.a * 100)
@$hex.val @color.toHex()
@$preview.css(background: @color.toString())
change: (e) ->
e.preventDefault()
color = new Color(
@$r.val(),
@$g.val(),
@$b.val(),
parseFloat(@$a.val()) / 100
)
@trigger 'change', color
changeHex: (e) ->
e.preventDefault()
color = Color.fromHex(@$hex.val())
@trigger 'change', color
class ColorPicker extends Popup
className: 'colorPicker'
width: 425
events:
'click .save': 'save'
'click .cancel': 'cancel'
'form submit': 'save'
constructor: ->
super
@color or= new Color(255, 0, 0)
unless @color instanceof Color
@color = Color.fromString(@color)
@original = @color.clone()
@render()
render: ->
@el.empty()
@gradient = new Gradient(color: @color)
@spectrum = new Spectrum(color: @color)
@alpha = new Alpha(color: @color)
@display = new Display(color: @color, original: @original)
@gradient.bind 'change', (color) =>
@color.set(color.rgb())
@display.setColor(@color)
@alpha.setColor(@color)
@change()
@spectrum.bind 'change', (color) =>
@color.set(color.rgb())
@gradient.setColor(@color)
@display.setColor(@color)
@alpha.setColor(@color)
@change()
@alpha.bind 'change', (color) =>
@color.set(a: color.a)
@display.setColor(@color)
@change()
@display.bind 'change', (color) =>
@setColor(color)
@append(@gradient, @spectrum, @alpha, @display)
setColor: (@color) ->
@display.setColor(@color)
@gradient.setColor(@color)
@spectrum.setColor(@color)
@alpha.setColor(@color)
@change()
change: (color = @color) ->
@trigger 'change', color
save: (e) ->
e.preventDefault()
@trigger 'save', @color
@close()
cancel: (e) ->
e.preventDefault()
@trigger 'cancel'
@trigger 'change', @original
@close()
release: ->
super
@gradient.release()
@spectrum.release()
@display.release()
class Input extends Spine.Controller
className: 'colorInput'
events:
'click .preview': 'open'
'change input': 'change'
constructor: ->
super
@color or= new Color
@$preview = $('<div />').addClass('preview')
@$preview.css(background: @color.toString())
@$input = $('<input type=color>')
@$input.val @color.toString()
@el.append @$preview, @$input
open: =>
@picker = new ColorPicker(color: @color)
@picker.bind 'change', (color) =>
@$input.val color.toString()
@$input.change()
@picker.open(@el.offset())
change: =>
@color.set Color.fromString(@$input.val())
@$preview.css(background: @color.toString())
class Preview extends Spine.Controller
className: 'colorPreview'
events:
'click': 'open'
constructor: ->
super
@color or= new Color
@inner = $('<div />').addClass('inner')
@append @inner
@render()
render: ->
@inner.css(background: @color.toString())
open: =>
# Picker already open
if @picker and @picker.isOpen()
@picker.remove()
return
@picker = new ColorPicker(color: @color)
@picker.bind 'change', (color) =>
@color.set color
@trigger 'change', @color
@render()
@picker.open(@el.offset())
val: (color) ->
if color?
@color = color
@render()
@color
module.exports = ColorPicker
module.exports.Color = Color
module.exports.Input = Input
module.exports.Preview = Preview