-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
750 lines (636 loc) · 19.4 KB
/
main.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
let filled = [];
let usedColors = new Set();
let animationHandle = null;
var showColorspace = true;
let scene = null;
let glRenderer = null;
let camera = null;
let cameraControls = null;
let colorCube = null;
var buildingImage = false;
String.prototype.hashCode = function() {
var hash = 0, i, chr;
if (this.length === 0) return hash;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
var random = null;
function start() {
const randomSeedInput = $('random_seed_input').value;
var randomSeed = null;
if (randomSeedInput === '') {
const rand = new Random(Math.floor(Math.random() * 4294967296));
randomSeed = rand.mulberry32(Math.floor(Math.random() * 4294967296));
$('random_seed_input').placeholder = randomSeed;
} else {
randomSeed = randomSeedInput.hashCode();
}
random = new Random(randomSeed);
// Cancel prior execution
if (animationHandle !== null) {
window.cancelAnimationFrame(animationHandle);
}
$('progress_area').hidden = false
$('toggle_generation').value = 'Pause';
showColorspace = $('display_colorspace').checked;
const {canvas, _} = canvasContext();
const colors = buildColors(canvas.width, canvas.height);
filled = new Array(canvas.width);
for (let i = 0; i < canvas.width; i++) {
filled[i] = new Array(canvas.height);
for (let j = 0; j < canvas.height; j++) {
filled[i][j] = false;
}
}
colorCube = new ColorCube(colors);
init3js();
let seedColorString = $('seed_color_picker').value;
let startColor = colors.closest(colorStringToRgb(seedColorString));
let seedAnchor = new Anchor(new XY(Math.floor((canvas.width - 1) / 2), Math.floor(canvas.height - 1)), startColor);
setColor(seedAnchor.pos, seedAnchor.color);
const renderer = new Renderer(colors, seedAnchor);
buildingImage = true;
let pixelsDrawn = 1;
let elapsedTime = 0;
const numPixels = canvas.width * canvas.height;
const animationCallback = () => {
if (buildingImage && renderer.anchors.length > 0) {
const frameStart = Date.now();
const MAX_TIME_PER_ITERATION = 33; // millis
let newPixelsDrawn = 0;
while (buildingImage && renderer.anchors.length > 0 && (Date.now() - frameStart < MAX_TIME_PER_ITERATION)) {
renderer.renderPass();
newPixelsDrawn++;
pixelsDrawn++;
}
const frameTime = Date.now() - frameStart;
elapsedTime += frameTime;
const fillRate = Math.round(pixelsDrawn / (elapsedTime / 1000));
const progress = 100 * pixelsDrawn / numPixels;
$('elapsed_time_text').textContent = `in ${toHHMMSS(elapsedTime)}`;
$('fillrate_text').textContent = `(drawing ${fillRate} pixels per second)`;
$('progress_bar').value = progress;
$('progress_text').textContent = `${Math.floor(progress)}%`;
}
showColorspace = $('display_colorspace').checked;
$('colorcube_canvas').hidden = !showColorspace;
if (showColorspace) {
colorCube.render();
if (!scene) {
init3js();
}
cameraControls.update();
glRenderer.render(scene, camera);
} else {
scene = null;
}
animationHandle = window.requestAnimationFrame(animationCallback);
};
animationHandle = window.requestAnimationFrame(animationCallback);
}
function toggleGeneration() {
buildingImage = !buildingImage;
if (buildingImage) {
$('toggle_generation').value = 'Pause';
} else {
$('toggle_generation').value = 'Resume'
}
}
function colorToSigned24Bit(s) {
return (parseInt(s.substr(1), 16) << 8) / 256;
}
function colorStringToRgb(colorString) {
let colorVal = colorToSigned24Bit(colorString);
let r = (colorVal >> 16) & 0xff;
let g = (colorVal >> 8) & 0xff;
let b = colorVal & 0xff;
return new RGB(r, g, b);
}
function init3js() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(70, 1, 0.01, 256 * 4);
camera.position.z = 256 * 1.414;
while(scene.children.length > 0) {
scene.remove(scene.children[0]);
}
scene.add(colorCube.points);
const canvas = $('colorcube_canvas');
glRenderer = new THREE.WebGLRenderer({ antialias: true, canvas: canvas });
glRenderer.setSize(512, 512);
glRenderer.setPixelRatio(window.devicePixelRatio);
cameraControls = new THREE.TrackballControls(camera, canvas);
}
function dataURLtoBlob(dataurl) {
const arr = dataurl.split(',');
const mime = arr[0].match(/:(.*?);/)[1];
const bstr = atob(arr[1]);
let n = bstr.length;
const u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {type:mime});
}
function downloadImage(){
const canvas = $('main_canvas');
const link = document.createElement("a");
const imgData = canvas.toDataURL({format: 'png', multiplier: 4});
const strDataURI = imgData.substr(22, imgData.length);
const blob = dataURLtoBlob(imgData);
const objurl = URL.createObjectURL(blob);
link.download = `colorburst-${canvas.width}x${canvas.height}.png`;
link.href = objurl;
link.click();
}
function toHHMMSS(millis) {
let seconds = Math.floor(millis / 1000);
let minutes = Math.floor(seconds / 60);
let hours = Math.floor(minutes / 60);
seconds = Math.floor(seconds % 60);
minutes = Math.floor(minutes % 60);
hours = Math.floor(hours % 60);
if (hours > 0) {
return `${hours} hour${plural(hours)} ${minutes} minute${plural(minutes)} ${seconds} second${plural(seconds)}`
} else if (minutes > 0) {
return `${minutes} minute${plural(minutes)} ${seconds} second${plural(seconds)}`
} else {
return `${seconds} second${plural(seconds)}`
}
}
function plural(count) {
return count == 1 ? "" : "s";
}
function setColor(xy, rgb) {
const {_, context} = canvasContext();
context.fillStyle = rgb.toColorString();
context.fillRect(xy.x, xy.y, 1, 1);
// usedColors.add(rgb);
rgb.inUse = true;
filled[xy.x][xy.y] = true;
colorCube.addPoint(rgb);
}
function resetAndStart() {
const {canvas, context} = canvasContext();
context.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = $('width_field').value;
canvas.height = $('height_field').value;
start();
}
function buildColors(width, height) {
const colorDepth = Math.ceil(Math.pow(2, (Math.log2(width * height)) / 3));
const colorSkip = 256 / colorDepth;
const matrix = new ColorSpace(colorDepth, colorSkip);
matrix.build();
return matrix;
}
/**
* Pops a random element from an array and returns it.
* @param {Array} values
* @returns {null|*} a random element from the array, or null if the array is empty.
*/
function popRandom(values) {
if (values.length === 0) {
return null;
}
const i = Math.floor(random.nextRandom() * values.length);
const element = values[i];
values.splice(i, 1);
return element;
}
/**
* Gets a random element from an array and returns it.
* @param {Array} values
* @returns {null|*} a random element from the array, or null if the array is empty.
*/
function getRandom(values) {
if (values.length === 0) {
return null;
}
const i = Math.floor(random.nextRandom() * values.length);
return values[i];
}
function groupBy(extractor) {
return function(map, value) {
var key = extractor(value);
if (map.get(key) === undefined) {
map.set(key, []);
}
map.get(key).push(value);
return map;
};
}
/**
*
* @param xy an {@link XY}
* @returns {*}
*/
function isOpen(xy) {
if (xy.x >= filled.length) {
throw `IndexOutOfBounds: X ${xy.x} > ${filled.length - 1}`
}
if (xy.y >= filled[0].length) {
throw `IndexOutOfBounds: Y ${xy.y} > ${filled[0].length - 1}`
}
return filled[xy.x][xy.y] === false;
}
function canvasContext() {
let canvas = $('main_canvas');
let context = canvas.getContext('2d');
return {
canvas: canvas,
context: context
};
}
function $(id) {
return document.getElementById(id);
}
class Renderer {
constructor(colorSpace, seedAnchor) {
this.anchors = [seedAnchor];
this.colorSpace = colorSpace;
}
renderPass() {
if (this.anchors.length === 0) {
return;
}
const anchor = this.getNextAnchor();
if (anchor === null || anchor === undefined) {
this.anchors = [];
return;
}
const neighbors = anchor.getNeighbors();
const xy = popRandom(neighbors);
const color = this.colorSpace.closest(anchor.color);
if (xy !== null) {
const newAnchor = new Anchor(xy, color);
if (newAnchor.hasNeighbors()) {
this.anchors.push(newAnchor);
}
setColor(xy, color);
}
if (anchor.hasNeighbors()) {
// The anchor is still viable, push it back
this.anchors.push(anchor);
}
}
/**
*
* @returns {null|Anchor}
*/
getNextAnchor() {
let anchor = null;
let usable = false;
do {
if (this.anchors.length === 0) {
return null;
}
anchor = popRandom(this.anchors);
usable = anchor.hasNeighbors();
} while (!usable);
return anchor;
}
}
/**
*
* @param array {[]}
* @param element {*}
*/
function removeElement(array, element) {
let index = array.findIndex(e => e.sameAs(element));
if (index > -1) {
array.splice(index, 1);
}
}
class ColorSpace {
constructor(colorDepth, colorSkip) {
this.colorDepth = colorDepth;
this.colorSkip = colorSkip;
this.flat = [];
this.arr = new Array(colorDepth);
this.indexes = [];
for (let x = 0; x < colorDepth; x++) {
this.arr[x] = new Array(colorDepth);
for (let y = 0; y < colorDepth; y++) {
this.arr[x][y] = new Array(colorDepth);
for (let z = 0; z < colorDepth; z++) {
this.arr[x][y][z] = 0;
}
}
}
}
build() {
for (let i = 0; i < this.colorDepth; i++) {
this.indexes.push(Math.floor(i * this.colorSkip)); // i, j, k are all going to be in the same set of numbers
for (let j = 0; j < this.colorDepth; j++) {
for (let k = 0; k < this.colorDepth; k++) {
let rgb = new RGB(
Math.floor(i * this.colorSkip), // R
Math.floor(j * this.colorSkip), // G
Math.floor(k * this.colorSkip) // B
);
this.set(i, j, k, rgb);
}
}
}
}
/**
* Gets the closest available color to the given one.
* @param rgb
* @returns {RGB}
*/
closest(rgb) {
return this.fastSearch(rgb);
}
mapToIndex(channelValue) {
// +1, -2, +3, -4, +5, -6, +7, -8, +9, -10, ...
// checks offsets of +1, -1, +2, -2, etc
var offset = 1;
while(this.indexes.indexOf(channelValue) === -1) {
channelValue += offset;
offset = -1 * (offset + Math.sign(offset));
}
return this.indexes.indexOf(channelValue);
}
fastSearch(rgb) {
const dim = this.colorDepth;
const arr = this.arr;
const r = this.mapToIndex(rgb.r);
const g = this.mapToIndex(rgb.g);
const b = this.mapToIndex(rgb.b);
// Search the faces of a cube centered at point (R, G, B) with radius up to colorDepth to find any unused colors;
// out of those colors, find the one closest to (R, G, B) and return it. If multiple colors are the same distance,
// pick one of them at random.
for (let searchRadius = 0; searchRadius < this.colorDepth; searchRadius++) {
const availableOptions = [];
const xOffsets = [];
const yOffsets = [];
const zOffsets = [];
if (r - searchRadius >= 0) { xOffsets.push(r - searchRadius); }
if (r + searchRadius < dim) { xOffsets.push(r + searchRadius); }
if (g - searchRadius >= 0) { yOffsets.push(g - searchRadius); }
if (g + searchRadius < dim) { yOffsets.push(g + searchRadius); }
if (b - searchRadius >= 0) { zOffsets.push(b - searchRadius); }
if (b + searchRadius < dim) { zOffsets.push(b + searchRadius); }
// bottom red-green plane and top red-green plane (-b, +b)
for (let x = Math.max(0, r - searchRadius); x <= r + searchRadius && x < dim; x++) {
for (let y = Math.max(0, g - searchRadius); y <= g + searchRadius && y < dim; y++) {
for (const z of zOffsets) {
const color = arr[x][y][z];
// will add the same color twice in the case of searchRadius === 0,
// but that's the only color in the search space so that doesn't matter
if (color && color.inUse === false) {
availableOptions.push(color);
}
}
}
}
// left green-blue plane and right green-blue plane (-r, +r)
// z-index is pushed towards the center by one to avoid re-querying the edges on the R-G planes
for (let y = Math.max(0, g - searchRadius); y <= g + searchRadius && y < dim; y++) {
for (let z = Math.max(0, b - searchRadius + 1); z <= b + searchRadius - 1 && z < dim; z++) {
for (const x of xOffsets) {
const color = arr[x][y][z];
if (color && color.inUse === false) {
availableOptions.push(color);
}
}
}
}
// front red-blue plane and back red-blue plane
for (let x = Math.max(0, r - searchRadius + 1); x <= r + searchRadius - 1 && x < dim; x++) {
for (let z = Math.max(0, b - searchRadius + 1); z <= b + searchRadius - 1 && z < dim; z++) {
for (const y of yOffsets) {
const color = arr[x][y][z];
if (color && color.inUse === false) {
availableOptions.push(color);
}
}
}
}
if (availableOptions.length === 1) {
return availableOptions[0];
} else if (availableOptions.length > 1) {
let closestDistance = null;
let closestOptions = [];
for (const option of availableOptions) {
const distance = option.distance(rgb);
if (!closestDistance || distance < closestDistance) {
closestDistance = distance;
closestOptions = [];
closestOptions.push(option);
} else if (Math.abs(distance - closestDistance) < 0.01) { // epsilon comparison
closestOptions.push(option);
}
}
return getRandom(closestOptions);
}
}
// Should never get here - the search should exhaustively cover the entire color space
console.warn(`Search found no results for ${rgb}!`);
return new RGB(255, 255, 255);
}
/**
*
* @returns {[]}
*/
flatten() {
return this.flat;
}
get(x, y, z) {
return this.arr[x][y][z];
}
/**
*
* @param x
* @param y
* @param z
* @param value
*/
set(x, y, z, value) {
this.arr[x][y][z] = value;
this.flat.push(value);
}
}
class Equatable {
sameAs(other) {
return this === other;
}
}
class RGB extends Equatable {
constructor(r, g, b) {
super();
this.r = r;
this.g = g;
this.b = b;
this.inUse = false;
return this;
}
/**
*
*/
toString() {
return this.toColorString();
}
/**
*
* @returns {string}
*/
toColorString() {
return `rgba(${this.r}, ${this.g}, ${this.b}, 1)`;
}
/**
*
* @param other
* @returns {boolean}
*/
sameAs(other) {
return this.r === other.r
&& this.g === other.g
&& this.b === other.b;
}
/**
*
* @param other
* @returns {number}
*/
distance(other) {
return Math.sqrt(
Math.pow((this.r - other.r), 2) +
Math.pow((this.g - other.g), 2) +
Math.pow((this.b - other.b), 2)
);
// return deltaE(rgb2lab([this.r, this.g, this.b]), rgb2lab([other.r, other.g, other.b]));
}
}
class XY extends Equatable {
constructor(x, y) {
super();
this.x = x;
this.y = y;
}
/**
*
* @param other
* @returns {boolean}
*/
sameAs(other) {
return this.x === other.x && this.y === other.y;
}
toString() {
return `(${this.x}, ${this.y})`;
}
}
class Anchor extends Equatable {
constructor(pos, color) {
super();
this.pos = pos;
this.color = color;
}
toString() {
return `Anchor[${this.pos.toString()}, ${this.color.toString()}]`
}
/**
*
* @param other
* @returns {boolean}
*/
sameAs(other) {
return this.pos.sameAs(other.pos);
}
/**
* Checks if there are any available neighbors adjacent to this anchor.
* @returns {boolean}
*/
hasNeighbors() {
return this.getNeighbors().length > 0;
}
/**
* Gets available points adjacent to this anchor.
* @returns {Array}
*/
getNeighbors() {
const {canvas, _} = canvasContext();
const neighbors = [];
const cx = this.pos.x;
const cy = this.pos.y;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
if (i === 0 && j === 0) {
continue;
}
let x = cx + i;
let y = cy + j;
if (x < 0 || x >= canvas.width || y < 0 || y >= canvas.height) {
continue;
}
let pos = new XY(x, y);
if (isOpen(pos)) {
neighbors.push(pos);
}
}
}
return neighbors;
}
}
// RGB-LAB conversion
// SOURCE: https://github.com/antimatter15/rgb-lab/blob/master/color.js
// the following functions are based off of the pseudocode
// found on www.easyrgb.com
function lab2rgb(lab){
var y = (lab[0] + 16) / 116,
x = lab[1] / 500 + y,
z = y - lab[2] / 200,
r, g, b;
x = 0.95047 * ((x * x * x > 0.008856) ? x * x * x : (x - 16/116) / 7.787);
y = 1.00000 * ((y * y * y > 0.008856) ? y * y * y : (y - 16/116) / 7.787);
z = 1.08883 * ((z * z * z > 0.008856) ? z * z * z : (z - 16/116) / 7.787);
r = x * 3.2406 + y * -1.5372 + z * -0.4986;
g = x * -0.9689 + y * 1.8758 + z * 0.0415;
b = x * 0.0557 + y * -0.2040 + z * 1.0570;
r = (r > 0.0031308) ? (1.055 * Math.pow(r, 1/2.4) - 0.055) : 12.92 * r;
g = (g > 0.0031308) ? (1.055 * Math.pow(g, 1/2.4) - 0.055) : 12.92 * g;
b = (b > 0.0031308) ? (1.055 * Math.pow(b, 1/2.4) - 0.055) : 12.92 * b;
return [Math.max(0, Math.min(1, r)) * 255,
Math.max(0, Math.min(1, g)) * 255,
Math.max(0, Math.min(1, b)) * 255]
}
function rgb2lab(rgb){
var r = rgb[0] / 255,
g = rgb[1] / 255,
b = rgb[2] / 255,
x, y, z;
r = (r > 0.04045) ? Math.pow((r + 0.055) / 1.055, 2.4) : r / 12.92;
g = (g > 0.04045) ? Math.pow((g + 0.055) / 1.055, 2.4) : g / 12.92;
b = (b > 0.04045) ? Math.pow((b + 0.055) / 1.055, 2.4) : b / 12.92;
x = (r * 0.4124 + g * 0.3576 + b * 0.1805) / 0.95047;
y = (r * 0.2126 + g * 0.7152 + b * 0.0722) / 1.00000;
z = (r * 0.0193 + g * 0.1192 + b * 0.9505) / 1.08883;
x = (x > 0.008856) ? Math.pow(x, 1/3) : (7.787 * x) + 16/116;
y = (y > 0.008856) ? Math.pow(y, 1/3) : (7.787 * y) + 16/116;
z = (z > 0.008856) ? Math.pow(z, 1/3) : (7.787 * z) + 16/116;
return [(116 * y) - 16, 500 * (x - y), 200 * (y - z)]
}
// calculate the perceptual distance between colors in CIELAB
// https://github.com/THEjoezack/ColorMine/blob/master/ColorMine/ColorSpaces/Comparisons/Cie94Comparison.cs
function deltaE(labA, labB){
var deltaL = labA[0] - labB[0];
var deltaA = labA[1] - labB[1];
var deltaB = labA[2] - labB[2];
var c1 = Math.sqrt(labA[1] * labA[1] + labA[2] * labA[2]);
var c2 = Math.sqrt(labB[1] * labB[1] + labB[2] * labB[2]);
var deltaC = c1 - c2;
var deltaH = deltaA * deltaA + deltaB * deltaB - deltaC * deltaC;
deltaH = deltaH < 0 ? 0 : Math.sqrt(deltaH);
var sc = 1.0 + 0.045 * c1;
var sh = 1.0 + 0.015 * c1;
var deltaLKlsl = deltaL / (1.0);
var deltaCkcsc = deltaC / (sc);
var deltaHkhsh = deltaH / (sh);
var i = deltaLKlsl * deltaLKlsl + deltaCkcsc * deltaCkcsc + deltaHkhsh * deltaHkhsh;
return i < 0 ? 0 : Math.sqrt(i);
}
// end RGB-LAB conversion