-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
162 lines (145 loc) · 4.8 KB
/
script.js
File metadata and controls
162 lines (145 loc) · 4.8 KB
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
const hex_to_dec = {
'0':0,
'1':1,
'2':2,
'3':3,
'4':4,
'5':5,
'6':6,
'7':7,
'8':8,
'9':9,
'a':10,
'b':11,
'c':12,
'd':13,
'e':14,
'f':15,
};
const m_c = document.getElementById("manhattan-gradient-surface");
const e_c = document.getElementById("euclidean-gradient-surface");
let m_ctx = m_c.getContext("2d");
let e_ctx = e_c.getContext("2d");
const exponent_picker = document.getElementById("exponent-picker");
const exponent_value = document.getElementById("exponent-value");
const gradient_button = document.getElementById("set-gradient");
const top_left = document.getElementById("top-left-picker");
const top_right = document.getElementById("top-right-picker");
const bottom_left = document.getElementById("bottom-left-picker");
const bottom_right = document.getElementById("bottom-right-picker");
//make white
for (let x = 0; x < m_c.height; x++){
for (let y = 0; y < m_c.width; y++){
setPixel(x, y, [255, 255, 255], 0);
}
}
for (let x = 0; x < e_c.height; x++){
for (let y = 0; y < e_c.width; y++){
setPixel(x, y, [255, 255, 255], 1);
}
}
//listeners
exponent_picker.addEventListener('change', (event) => {
exponent_value.innerHTML = `Exponent Value: ${exponent_picker.value}`;
if (bottom_right){
gradient(
[[[0, 0], getRGB(top_left.value)],
[[m_c.width, 0], getRGB(top_right.value)],
[[0, m_c.height], getRGB(bottom_left.value)],
[[m_c.width, m_c.height], getRGB(bottom_right.value)]],
0
);
gradient(
[[[0, 0], getRGB(top_left.value)],
[[m_c.width, 0], getRGB(top_right.value)],
[[0, m_c.height], getRGB(bottom_left.value)],
[[m_c.width, m_c.height], getRGB(bottom_right.value)]],
1
);
}
}
);
gradient_button.addEventListener('click', (event) => {//colors = [Each color:[[position x,y], [r, g, b]]
gradient(
[[[0, 0], getRGB(top_left.value)],
[[m_c.width, 0], getRGB(top_right.value)],
[[0, m_c.height], getRGB(bottom_left.value)],
[[m_c.width, m_c.height], getRGB(bottom_right.value)]],
0
);
gradient(
[[[0, 0], getRGB(top_left.value)],
[[m_c.width, 0], getRGB(top_right.value)],
[[0, m_c.height], getRGB(bottom_left.value)],
[[m_c.width, m_c.height], getRGB(bottom_right.value)]],
1
);
console.log("gradiented");
});
function gradient(colors, grad_method) {
console.log(colors);
console.log(grad_method);
var height;
var width;
if (grad_method == 0){
height = m_c.height;
width = m_c.width;
} else if (grad_method == 1) {
height = e_c.height;
width = e_c.width;
} else {
console.log("0 or 1 for manhattan or euclidean respectively. This request did not go through");
return;
}
for (let x = 0; x < height; x++){
for (let y = 0; y < width ; y++){
var sum = 0;
for (const source of colors){
sum += get_distance(source[0][0], source[0][1], x, y, grad_method);
}
let avg = sum / colors.length;
var pixel_color = [0, 0, 0];
for (let idx = 0; idx < 3; idx++){
var color = 0;
for (const source of colors){
var distance = get_distance(source[0][0], source[0][1], x, y, grad_method);
var multiplier = ((avg-distance)*2 + distance) / sum;
color += source[1][idx] * multiplier;
}
pixel_color[idx] = color;
}
setPixel(x, y, pixel_color, grad_method)
}
}
}
function get_distance(x1, y1, x2, y2, grad_method){
var original_distance
if (grad_method == 0){
original_distance = Math.abs(x1-x2) + Math.abs(y1-y2);
} else if (grad_method == 1) {
original_distance = ((x1-x2)**2 + (y1-y2)**2)**0.5;
} else {
console.log("0 or 1 for manhattan or euclidean respectively. This request did not go through");
return;
}
return original_distance ** exponent_picker.value;
}
function getRGB(hex) {
var rgb = [
hex_to_dec[hex[1]]*16+hex_to_dec[hex[2]],
hex_to_dec[hex[3]]*16+hex_to_dec[hex[4]],
hex_to_dec[hex[5]]*16+hex_to_dec[hex[6]]
];
return rgb;
}
function setPixel(x, y, rgb, grad_method) {
if (grad_method == 0){
m_ctx.fillStyle = `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
m_ctx.fillRect(x, y, 1, 1)
} else if (grad_method == 1) {
e_ctx.fillStyle = `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
e_ctx.fillRect(x, y, 1, 1)
} else {
console.log("0 or 1 for manhattan or euclidean respectively. This request did not go through");
}
}