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
|
var PhotoView = (function(){
var hue = 0, sat = 0, lum = 0
var nn, invert, width
var timeout
var PhotoView = View.extend({
el: "#photo",
events: {
"change #width_el": "updateWidth",
"change #ratio_el": "updateRatio",
"change #hue_el": "updateHue",
"change #saturation_el": "updateSat",
"change #luminance_el": "updateLum",
"change #invert_el": "updateInvert",
"change #palette_el": "updatePalette",
"change #nn_el": "updateNN",
"click .post-btn": "post",
"click .upload-btn": "upload",
"click .close-btn": "hide",
},
initialize: function(){
Photo.set_recolor_fn(function(rgb){
if (invert) {
rgb[0] = 255 - rgb[0]
rgb[1] = 255 - rgb[1]
rgb[2] = 255 - rgb[2]
}
var hsl = rgb2hsl(rgb)
hsl[0] = mod(hsl[0] + hue, 1.0)
hsl[1] = clamp(hsl[1] + sat, 0.0, 1.0)
hsl[2] = clamp(hsl[2] + lum, 0.0, 1.0)
return hsl2rgb.apply(this, hsl)
})
this.$uploadBtn = this.$(".upload-btn")
this.$postBtn = this.$(".post-btn")
this.$closeBtn = this.$(".close-btn")
this.$width = this.$("#width_el")
this.$widthSpan = this.$("#width_span")
this.$heightSpan = this.$("#height_span")
this.$ratio = this.$("#ratio_el")
this.$nn = this.$("#nn_el")
this.$invert = this.$("#invert_el")
this.$palette = this.$("#palette_el")
this.$canvasRapper = this.$("#canvas_rapper")
this.camera = new UploadView({ el: this.$(".camera-btn") })
this.canvas = document.createElement("canvas")
this.ctx = this.canvas.getContext('2d')
this.$canvasRapper.append(this.canvas)
width = parseInt( this.$width.val() )
ratio = parseFloat( this.$ratio.val() )
nn = this.$nn.prop('checked')
invert = this.$invert.prop('checked')
this.$widthSpan.html(width)
this.img = new Image ()
this.img.onload = function(){
this.$ratio.val()
this.updateRatio()
}.bind(this)
},
load: function(f){
var url
if (typeof f == "string") {
url = f
this.$uploadBtn.hide()
}
else {
this.file = file
url = URL.createObjectURL(file)
this.$uploadBtn.show()
}
this.img.src = url
console.log("load", url)
this.show()
},
show: function(){
$("body").addClass("photo")
},
hide: function(){
$("body").removeClass("photo")
},
update: function(){
Photo.fromCanvas(this.img, this.toCanvas.bind(this), { width: width, ratio: ratio, neighbor: nn })
},
updateWidth: function(){
width = parseInt( this.$width.val() )
this.$widthSpan.html( width )
this.$heightSpan.html( "..." )
this.deferUpdate()
},
updateRatio: function(){
ratio = parseFloat( this.$ratio.val() )
if (ratio < 0.03) return
this.$widthSpan.html( width )
this.$heightSpan.html( "..." )
this.deferUpdate()
},
deferUpdate: function(){
clearTimeout( timeout )
timeout = setTimeout(this.update.bind(this), 50)
},
updateHue: function(){
},
updateSat: function(){
},
updateLum: function(){
},
updateInvert: function(){
invert = this.$invert.prop('checked')
this.update.bind(this)
},
updateNN: function(){
nn = this.$nn.prop('checked')
this.update.bind(this)
},
updatePalette: function(){
var palette = this.$palette.val()
Photo.set_colors( Photo[palette] )
this.update.bind(this)
},
toCanvas: function(rows){
this.rows = rows
var wpx = 6, hpx = 12
var rgb_colors = Photo.colors.map(function(c){ return "rgb(" + c + ")" })
this.canvas.width = rows[0].length * wpx
this.canvas.height = rows.length * hpx
var ctx = this.ctx
rows.forEach(function(row, j){
row.forEach(function(lex, i){
ctx.fillStyle = rgb_colors[lex]
ctx.fillRect(i*wpx,j*hpx,wpx,hpx)
})
})
this.$heightSpan.html( rows.length )
//
},
post: function(){
if (! this.rows) return
var color_code = Photo.mirc(this.rows)
// var fd = new FormData()
// fd.append('image', color_code)
var _csrf = $("meta[name='_csrf']").attr('content')
var request = $.ajax({
url: "/_irc/post",
type: "post",
data: { image: color_code, _csrf: _csrf },
// processData: false,
// contentType: false,
})
request.done(this.success.bind(this))
},
success: function(){
console.log("posted colorcode")
this.hide()
},
upload: function(){
this.camera.upload(this.file)
},
})
return PhotoView
})()
|