-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
184 lines (153 loc) · 6.61 KB
/
Copy pathscript.js
File metadata and controls
184 lines (153 loc) · 6.61 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var data = {
"mode": "transformer", // transformer, diffusion or random
"num_colors": 5, // max 12, min 2
"temperature": "1.2", // max 2.4, min 0
"num_results": 1, // max 50 for transformer, 5 for diffusion
"adjacency": [], // nxn adjacency matrix as a flat array of strings
"palette": ["-", "-", "-", "-", "-"]
}
generate();
function generate() {
let loader = $('.loader')[0];
loader.style.display = "block";
data.adjacency = randomAdjacency();
$.ajax({
type: "post",
url: "https://api.huemint.com/color",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (return_data) {
var colorarr = [];
loader.style.display = "none";
var palette = return_data.results[0].palette;
console.log(palette)
let container = document.getElementById("palette-container")
var colors = $(".color-container")
for (i in palette) {
if (data.palette[i] != "-") {
colorarr[i] = colors[i]
}
else {
if (colors[i]) {
colors[i].remove();
}
var colorcontainer = document.createElement("div");
colorcontainer.className = 'color-container';
let rgb = palette[i][0] + ', ' + palette[i][1] + ', ' + palette[i][2];
var color = document.createElement("input");
color.type = "color";
color.id = "bgcolor"
color.className = "color";
color.value = palette[i];
var colorinfo = document.createElement("div");
colorinfo.className = "color-info";
var colorrow1 = document.createElement("div");
colorrow1.className = "color-info-row";
var infokey1 = document.createElement("p");
infokey1.innerHTML = "RGB";
infokey1.className = "info-key";
var infovalue1 = document.createElement("p");
infovalue1.innerHTML = hexToRgb(palette[i]);
infovalue1.id = "rgb";
infovalue1.className = "info-value";
colorrow1.appendChild(infokey1);
colorrow1.appendChild(infovalue1);
var colorrow2 = document.createElement("div");
colorrow2.className = "color-info-row";
var infokey2 = document.createElement("p");
infokey2.innerHTML = "HEX";
infokey2.className = "info-key";
var infovalue2 = document.createElement("p");
infovalue2.innerHTML = palette[i];
infovalue2.className = "info-value";
infovalue2.id = "hex";
colorrow2.appendChild(infokey2);
colorrow2.appendChild(infovalue2);
colorinfo.appendChild(colorrow1);
colorinfo.appendChild(colorrow2);
var lockcontainer = document.createElement("div");
lockcontainer.className = "lock-container";
var locktext = document.createElement("p");
locktext.innerHTML = "Lock";
var lockbox = document.createElement("input");
lockbox.type = "checkbox";
lockbox.id = "lock";
lockcontainer.appendChild(locktext)
lockcontainer.appendChild(lockbox)
colorcontainer.appendChild(lockcontainer)
colorcontainer.appendChild(colorinfo);
colorcontainer.appendChild(color);
colorarr.push(colorcontainer)
// container.appendChild(colorcontainer);
color.addEventListener('input', function (e) {
// Traverse up the DOM to find the color container for this input
let curcontainer = this.parentElement;
while (curcontainer && curcontainer.className !== 'color-container') {
curcontainer = curcontainer.parentElement;
}
// Find the infovalue1 and infovalue2 elements within the container
let rgbvalue = curcontainer.querySelector('#rgb');
let hexvalue = curcontainer.querySelector('#hex');
// Update the infovalue1 and infovalue2 elements with the new RGB and hex values
rgbvalue.innerHTML = hexToRgb(this.value);
hexvalue.innerHTML = this.value;
})
}
// if(!container.children[0].querySelector("#lock").checked) {
}
for (i in colorarr) {
container.appendChild(colorarr[i])
}
// Run the code here that needs
// to access the data returned
console.log(data);
},
error: function () {
alert('Error occured');
}
});
}
function rgbToHex(r, g, b) {
r = Math.max(0, Math.min(255, r));
g = Math.max(0, Math.min(255, g));
b = Math.max(0, Math.min(255, b));
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
function hexToRgb(hex) {
// Remove the hash symbol from the beginning of the hex value (if present)
hex = hex.replace('#', '');
// Split the hex value into its three component parts (red, green, blue)
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);
// Return the RGB value as a string
return r + ', ' + g + ', ' + b;
}
function randomizePalette() {
var input = [];
var colors = $(".color-container")
for (i in colors) {
if (i >= 0) {
var lock = colors[i].querySelector("#lock");
if (lock.checked) {
var hexstring = colors[i].querySelector("#hex").innerHTML
input.push(hexstring)
}
else {
input.push("-")
// colors[i].remove();
}
}
}
data.palette = input;
// console.log(data)
generate();
}
function randomAdjacency() {
let output = []
for (let i = 0; i < 25; i++) {
output.push(Math.round(Math.random() * (50)).toString())
}
return output
}