-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.js
More file actions
200 lines (165 loc) · 7.05 KB
/
Copy pathui.js
File metadata and controls
200 lines (165 loc) · 7.05 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var data = {
"mode": "transformer", // transformer, diffusion or random
"num_colors": 4, // max 12, min 2
"temperature": "1.2", // max 2.4, min 0
"num_results": 1, // max 50 for transformer, 5 for diffusion
"adjacency": ["0", "90", "50", "50", "90", "0", "0", "0", "50", "0", "0", "35", "50", "0", "35", "0"], // 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 colorborder = document.createElement("div");
colorborder.className = "color-border";
var colorpickercontainer = document.createElement("div");
colorpickercontainer.className = "color-picker-container";
colorborder.appendChild(color);
colorpickercontainer.appendChild(colorborder)
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);
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(colorpickercontainer);
colorcontainer.appendChild(colorrow2);
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 hexvalue = curcontainer.querySelector('#hex');
// Update the infovalue1 and infovalue2 elements with the new RGB and hex values
hexvalue.innerHTML = this.value;
changeUI();
})
}
}
for (i in colorarr) {
container.appendChild(colorarr[i])
}
changeUI();
function changeUI() {
var colorschange = $(".color-container");
var r = document.querySelector(':root');
r.style.setProperty('--ui-main', colorschange[2].querySelector("#hex").innerHTML);
r.style.setProperty('--ui-dark', colorschange[0].querySelector("#hex").innerHTML);
r.style.setProperty('--ui-light', colorschange[1].querySelector("#hex").innerHTML);
r.style.setProperty('--ui-second', colorschange[3].querySelector("#hex").innerHTML);
r.style.setProperty('--ui-third', colorschange[2].querySelector("#hex").innerHTML);
}
},
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
}
function save() {
var colors = $(".color-container")
var palette_arr = []
for (i in colors) {
if (i >= 0) {
palette_arr.push(colors[i].querySelector("#hex").innerHTML);
}
}
var unique_id = generateUniqueId();
var json_palette = {
"id": unique_id,
"name": $("#palette-name")[0].value,
"palette": palette_arr
}
console.log(json_palette)
}