-
Notifications
You must be signed in to change notification settings - Fork 4
/
repalette.js
190 lines (147 loc) · 5.53 KB
/
repalette.js
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
const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d', { willReadFrequently: true })
const processButton = document.querySelector('#process-image')
const downloadButton = document.querySelector('#download-image')
const downloader = document.createElement('a')
downloader.download = 'output'
downloadButton.addEventListener('click', () => downloader.click())
function updateDownloadURL() {
canvas.toBlob(blob => {
if (downloader.href) URL.revokeObjectURL(downloader.href)
const blobURL = URL.createObjectURL(blob)
downloader.href = blobURL
downloadButton.disabled = false
})
}
let originalImageData = null
function displayImage(image) {
canvas.width = image.width
canvas.height = image.height
canvas.style.aspectRatio = image.width + '/' + image.height
ctx.drawImage(image, 0, 0)
originalImageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
processButton.disabled = false
downloadButton.disabled = true
updateDownloadURL()
}
const hexChars = new Set("0123456789abcdefABCDEF")
function isHex(str) {
if (str.length != 7) return false
if (str[0] != '#') return false
for (let i = 1; i < str.length; ++i) {
if (!hexChars.has(str[i])) return false
}
return true
}
function Hex2RGB(hex) {
return [
parseInt(hex.slice(1, 3), 16),
parseInt(hex.slice(3, 5), 16),
parseInt(hex.slice(5, 7), 16),
]
}
ctx.font = 'lighter 84px sans-serif'
ctx.textAlign = 'center'
ctx.fillStyle = '#ccc'
ctx.fillText('Paste image or click to upload', canvas.width / 2, canvas.height / 2)
document.querySelector('#file-input').addEventListener('input', e => {
const reader = new FileReader()
reader.addEventListener('load', event => {
const img = new Image()
img.addEventListener('load', () => displayImage(img))
img.src = event.target.result.toString()
})
reader.readAsDataURL(e.target.files[0])
})
document.addEventListener('paste', e => {
for (const item of e.clipboardData.items) {
if (item.kind != 'file') continue
const reader = new FileReader()
const file = item.getAsFile()
reader.addEventListener('load', event => {
const img = new Image()
img.addEventListener('load', () => displayImage(img))
img.src = event.target.result
})
reader.readAsDataURL(file)
break
}
})
const paletteContainer = document.querySelector('#palette')
const paletteAdd = document.querySelector('#color-new')
function addColor(hex) {
const color = document.createElement('label')
color.classList.add('color')
color.style.background = hex;
const colorInp = document.createElement('input')
colorInp.classList.add('color-input')
colorInp.type = 'color'
colorInp.value = hex
colorInp.addEventListener('change', () => color.style.background = colorInp.value)
const colorRemove = document.createElement('button')
colorRemove.textContent = "\u00D7"
colorRemove.classList.add('color-remove')
colorRemove.addEventListener('click', () => color.remove())
color.appendChild(colorInp)
color.appendChild(colorRemove)
paletteContainer.insertBefore(color, paletteAdd)
}
paletteAdd.addEventListener('click', () => addColor('#000000'))
const paletteExport = document.querySelector('#palette-export')
paletteExport.addEventListener('focus', e => {
e.target.value = ''
for (const color of document.querySelectorAll('.color-input')) {
e.target.value += color.value + '\n'
}
})
paletteExport.addEventListener('blur', e => {
for (const color of document.querySelectorAll('.color')) color.remove()
for (const line of e.target.value.split('\n')) if (isHex(line)) addColor(line)
})
const paletteSelector = document.querySelector('#palette-select')
const option = paletteSelector.querySelector('option')
fetch('https://raw.githubusercontent.com/Gogh-Co/Gogh/master/data/themes.json').then(async response => {
const data = await response.json()
option.textContent = 'Select palette'
const themes = new Array(data.length)
for (let i = 0; i < data.length; ++i) {
const {name} = data[i]
const palette = new Set()
for (const value of Object.values(data[i])) if (isHex(value)) palette.add(value)
themes[i] = { name, palette }
const option = document.createElement('option')
option.value = i
option.textContent = name
paletteSelector.appendChild(option)
}
paletteSelector.addEventListener('change', e => {
if (e.value == "-1") return
for (const color of document.querySelectorAll('.color')) color.remove()
for (const color of themes[e.target.value].palette) addColor(color)
})
}).catch(e => {
option.textContent = 'ERROR: Failed to fetch colors from Gosh'
throw e
})
WebAssembly.instantiateStreaming(fetch("./repalette.wasm")).then(wasm => {
const { exports } = wasm.instance
const ditherSelect = document.querySelector('#dither-select')
processButton.addEventListener('click', () => {
ctx.putImageData(originalImageData, 0, 0)
exports.palette_clear()
for (const color of document.querySelectorAll('.color-input')) {
const [r, g, b] = Hex2RGB(color.value)
exports.palette_add(r, g, b)
}
const { data } = ctx.getImageData(0, 0, canvas.width, canvas.height)
const ptr = exports.get_pixels(canvas.width, canvas.height)
const len = 4 * canvas.width * canvas.height
const buf = new Uint8ClampedArray(exports.memory.buffer, ptr, len)
const imgdata = new ImageData(buf, canvas.width)
buf.set(data)
exports.update_canvas(canvas.width, canvas.height, ditherSelect.value)
ctx.putImageData(imgdata, 0, 0)
downloadButton.disabled = true
updateDownloadURL()
})
})