-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalette.js
289 lines (257 loc) · 7.27 KB
/
palette.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
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
export function getPalette(id) {
const palette = PALETTES.find(p => p.id === id)
if (palette) return palette
return ORIGINAL
}
export function initPallet(palette, density, rotate, exp, max_iter) {
const rgbaBuffer = new Uint8ClampedArray(max_iter * 4 + 20)
// 0 and 1 = transparent (skipped), 2 and 3 = black (in set)
// the first elements are doubled because we rotate the palette by one for smoothing
rgbaBuffer[11] = 255
rgbaBuffer[15] = 255
// Scale density exponentially
density = Math.pow(2, density / 10)
for (let i = 0; i <= max_iter; i++) {
const v = density * i // Math.pow(i*2, 0.9)
const [r, g, b] = palette.getColor(v, rotate)
rgbaBuffer[(i + 4) * 4] = r
rgbaBuffer[(i + 4) * 4 + 1] = g
rgbaBuffer[(i + 4) * 4 + 2] = b
rgbaBuffer[(i + 4) * 4 + 3] = 255
}
return rgbaBuffer
}
// not sure if this is correct, it does result in vibrant colors though
function toSRGB(r, g, b) {
function toSRGBComponent(c) {
return c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4)
}
return [toSRGBComponent(r), toSRGBComponent(g), toSRGBComponent(b)]
}
class OriginalPalette {
constructor() {
this.id = 'original'
this.name = "Original"
this.wavelengths = [80, 81, 85]
this.mirrorPosition = 565
}
getColor(v, rotate) {
let idx = (v * 2 + 590 + rotate / 180 * this.mirrorPosition) % (this.mirrorPosition * 2)
if (idx >= this.mirrorPosition) {
idx = this.mirrorPosition - (idx - this.mirrorPosition)
}
let r = Math.cos(idx / this.wavelengths[0] * Math.PI) * 0.5 + 0.5
let g = Math.cos(idx / this.wavelengths[1] * Math.PI) * 0.5 + 0.5
let b = Math.cos(idx / this.wavelengths[2] * Math.PI) * 0.5 + 0.5
const [rr, gg, bb] = toSRGB(r, g, b)
return [Math.round(rr * 255), Math.round(gg * 255), Math.round(bb * 255)]
}
}
class GrayScalePalette {
constructor(id, name, min, max) {
this.id = id
this.name = name
this.min = min
this.max = max
}
getColor(v, rotate) {
const idx = v * 1.6 + rotate / 180 * 80
const f = Math.sin(idx / 80 * Math.PI - Math.PI / 3) * 127 + 128
return [f, f, f]
}
}
class SingleColorPalette {
constructor(id, name, color) {
this.id = id
this.name = name
this.color = color
}
getColor(v, rotate) {
return this.color
}
}
class IndexedPalette {
constructor(id, name, colors, mirror, reverse) {
this.id = id
this.name = name
this.colors = []
this.colors = this.colors.concat(colors)
reverse && this.colors.reverse()
mirror && (this.colors = this.colors.concat(this.colors.slice(1, this.colors.length - 1).reverse()))
}
getColor(v, rotate) {
const palette = this.colors
const scaled = v * palette.length / 100 + rotate / 360 * palette.length
return this.getInterpolationFunctions().map(fn => Math.round(fn(scaled)))
}
getInterpolationFunctions() {
if (!this.interpolationFunctions) {
this.interpolationFunctions = [0, 1, 2].map(i => monotoneCubicInterpolationFN(this.colors.map(c => c[i])))
}
return this.interpolationFunctions
}
}
// Similar to that of Ultra Fractal, although these colors are equaly spaced
const MANDELBROT = new IndexedPalette("mandelbrot", "Mandelbrot", [
[0, 7, 100],
[32, 107, 203],
[237, 255, 255],
[255, 170, 0],
[0, 2, 0],
], false)
const LAVA = new IndexedPalette("lava", "Lava", [
[0, 0, 0],
[10, 0, 0],
[20, 0, 0], // [20, 0, 0],
[40, 0, 0],
[80, 0, 0],
[160, 10, 0],
[200, 40, 0],
[240, 90, 0],
[255, 160, 0],
[255, 220, 10],
[255, 255, 80],
[255, 255, 160],
[255, 255, 255],
], true)
const FALL = new IndexedPalette("fall", "Fall", [
[25, 25, 25],
[128, 0, 0],
[255, 69, 0],
[255, 140, 0],
[255, 215, 0],
[255, 239, 184],
], false)
const OCEAN = new IndexedPalette("ocean", "Ocean", [
[0, 0, 51],
[0, 0, 102],
[0, 0, 153],
[0, 51, 102],
[0, 102, 204],
[51, 153, 255],
[102, 178, 255],
[153, 204, 255],
[204, 229, 255],
[255, 255, 255]
], true)
const POP = new IndexedPalette("pop", "Pop", [
[255, 0, 0],
[255, 165, 0],
[255, 255, 0],
[0, 128, 0],
[0, 0, 255],
[128, 0, 128],
[255, 0, 255],
[255, 192, 203],
[255, 99, 71],
[0, 255, 255],
[0, 255, 0],
[255, 0, 128]
], false)
const SKY_WATER = new IndexedPalette("sky_water", "Sky & Water", [
[0, 0, 51],
[0, 51, 102],
[0, 102, 153],
[0, 153, 204],
[51, 153, 204],
[102, 178, 255],
[153, 204, 255],
[178, 223, 255],
[204, 238, 255],
[229, 255, 255],
[255, 255, 255],
[51, 153, 204],
[0, 102, 153]
], false)
const JEWELLERY = new IndexedPalette("jewellery", "Jewellery", [
[0, 0, 51],
[0, 0, 102],
[0, 0, 153],
[0, 102, 204],
[51, 153, 255],
[0, 102, 102],
[0, 128, 128],
[204, 204, 255],
[255, 204, 0],
[255, 0, 0],
[255, 0, 255],
[255, 255, 255],
[51, 153, 255],
[0, 0, 153]
], false)
export const PALETTES = [
new OriginalPalette(),
MANDELBROT,
LAVA,
FALL,
OCEAN,
SKY_WATER,
POP,
JEWELLERY,
new GrayScalePalette("gray_scale", "Gray Scale", 0, 255),
new SingleColorPalette("black_white", "Pure B/W", [255, 255, 255]),
]
function linearInterpolationFN(values) {
const N = values.length
return function (x) {
const t = x - Math.floor(x)
let k = Math.floor(x)
if (k < 0) k += N
const yk0 = values[k % N]
const yk1 = values[(k + 1) % N]
return yk0 * (1 - t) + yk1 * t
}
}
// https://en.wikipedia.org/wiki/Monotone_cubic_interpolation
function monotoneCubicInterpolationFN(values) {
const N = values.length;
const delta = []
for (let k = 0; k < N; k++) {
delta.push((values[(k + 1) % N] - values[k]))
}
const m = []
for (let k = 1; k <= N; k++) {
const dk = delta[k % N]
const dk1 = delta[(k + 1) % N]
m[(k + 1) % N] = dk * dk1 <= 0 ? 0 : (dk + dk1) / 2
// m[(k + 1) % N] = (dk + dk1) / 2
}
for (let k = 0; k < N; k++) {
if (delta[k] !== 0) {
const alpha = m[k] / delta[k]
const beta = m[(k + 1) % N] / delta[k]
if (alpha < 0) {
m[k] = 0
}
if (beta < 0) {
m[(k + 1) % N] = 0
}
const sqRadius = alpha * alpha + beta * beta
if (sqRadius > 9) {
const tau = 3 / Math.sqrt(sqRadius)
m[k] = tau * alpha * delta[k]
m[(k + 1) % N] = tau * beta * delta[k]
}
}
}
return function (x) {
const t = x - Math.floor(x)
let k = Math.floor(x)
if (k < 0) k += N
const yk0 = values[k % N]
const yk1 = values[(k + 1) % N]
return yk0 * h00(t) + m[k % N] * h10(t) + yk1 * h01(t) + m[(k + 1) % N] * h11(t)
}
}
function h00(t) {
return (1 + 2 * t) * Math.pow(1 - t, 2)
}
function h10(t) {
return t * Math.pow(1 - t, 2)
}
function h01(t) {
return t * t * (3 - 2 * t)
}
function h11(t) {
return t * t * (t - 1)
}