forked from stefnotch/Processing3DRaytracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pick-colors-to-blend.js
145 lines (122 loc) · 3.21 KB
/
pick-colors-to-blend.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
const ctx = canvas.getContext("2d");
function createColor() {
return {
color: 0,
depth: 0,
bitPattern: 0,
};
}
function drawColor(color) {
ctx.fillStyle = "black";
ctx.fillRect(color.color, 0 + color.depth * 20, 1, 350 - color.depth * 20);
}
function drawColors(colorsArray) {
for (let i = 0; i < colorsArray.length; i++) {
drawColor(colorsArray[i]);
}
}
function setBit(number, bitIndex, value) {
let bitValue = (number >> bitIndex) & 1;
if (value != bitValue) {
return number ^ (1 << bitIndex);
}
return number;
}
let colors = [];
let colorsLength = 0;
let depth = 0;
function constrain(val, min, max) {
return Math.min(Math.max(val, min), max);
}
function random(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function addIteration(newColor, t) {
newColor = constrain(newColor, 0, 255);
t = constrain(t, 0, 255) / 255;
depth++;
let len = colorsLength;
for (let i = 0; i < len; i++) {
let lerpedColor = Math.floor(colors[i].color * (1 - t)) + Math.floor(newColor * t);
if (colorsLength >= colors.length) colors.push(createColor());
colors[colorsLength].color = lerpedColor;
colors[colorsLength].depth = depth;
colors[colorsLength].bitPattern = setBit(colors[i].bitPattern, depth, 1);
colorsLength++;
}
}
function doRun(maxScore) {
colorsLength = 0;
depth = 0;
// Iteration background
if (colorsLength >= colors.length) colors.push(createColor());
colors[colorsLength].color = 0;
colors[colorsLength].depth = 0;
colors[colorsLength].bitPattern = 0;
colorsLength++;
let iterationValues = [
255,
255,
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
];
// Iteration 1
addIteration(iterationValues[0], iterationValues[1]);
// Iteration 2
addIteration(iterationValues[2], iterationValues[3]);
// Iteration 3
addIteration(iterationValues[4], iterationValues[5]);
// Iteration 4
addIteration(iterationValues[6], iterationValues[7]);
// Iteration 5
addIteration(iterationValues[8], iterationValues[9]);
colors.sort((a, b) => a.color - b.color);
let score = 0;
for (let i = 0; i < colorsLength - 1; i++) {
let diff = colors[i + 1].color - colors[i].color;
if (diff < 1) diff = 0;
score += Math.sqrt(diff);
}
if (score < maxScore) return;
return {
score: score,
colors: colors.map((c) => {
return { ...c };
}),
iterationValues: iterationValues,
};
}
let maxScore = 0;
let maxResult = undefined;
function draw() {
let hasNewMaxScore = false;
for (let i = 0; i < 5000; i++) {
let result = doRun(maxScore);
if (result && result.score > maxScore) {
maxScore = result.score;
maxResult = result;
console.log(maxResult);
hasNewMaxScore = true;
}
}
if (hasNewMaxScore) {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, 400, 400);
for (let i = 0; i < 255; i++) {
ctx.fillStyle = `rgb(${i},${i},${i})`;
ctx.fillRect(i, 350, 1, 50);
}
drawColors(maxResult.colors);
ctx.fillText(maxScore, 256, 10);
}
requestAnimationFrame(draw);
}
draw();