-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
394 lines (345 loc) · 11.1 KB
/
app.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
//selectors
const colorDivs = document.querySelectorAll(".color");
const generateBtn = document.querySelector(".generate");
const sliders = document.querySelectorAll("input[type='range']");
const currentHexes = document.querySelectorAll(".color h2");
const adjustButton = document.querySelectorAll(".adjust");
const lockButton = document.querySelectorAll(".lock");
const closeAdjustments = document.querySelectorAll(".close-adjustment");
const sliderContainers = document.querySelectorAll(".sliders");
const popup = document.querySelector(".copy-container");
let initialColors;
//local storage
let savedPalettes = [];
//Event listeners
sliders.forEach((slider) => {
slider.addEventListener("input", hslControls);
});
colorDivs.forEach((div, index) => {
div.addEventListener("change", () => {
/* callback function */
updateTextUI(index);
});
});
currentHexes.forEach((hex) => {
hex.addEventListener("click", () => {
copyToClipboard(hex);
});
});
popup.addEventListener("transitionend", () => {
const popupBox = popup.children[0];
popup.classList.remove("active");
popupBox.classList.remove("active");
});
adjustButton.forEach((button, index) => {
button.addEventListener("click", () => {
toggleAdjustmentPanel(index);
});
});
closeAdjustments.forEach((button, index) => {
button.addEventListener("click", () => {
closeAdjustmentPanel(index);
});
});
generateBtn.addEventListener("click", randomizeColor);
lockButton.forEach((button) => {
button.addEventListener("click", () => {
button.classList.toggle("locked");
if (button.classList.contains("locked")) {
button.innerHTML = "<i class='fas fa-lock'></i>";
} else {
button.innerHTML = "<i class='fas fa-lock-open'></i>";
}
});
});
//functions
function generateHex() {
/*
const letters = "#012356789ABCDEF";
let hash = "#";
for (let i = 0; i < 6; i++) {
hash += letters[Math.floor(Math.random() * 16)];
}
return hash;
*/
const hexColor = chroma.random();
return hexColor;
}
function randomizeColor() {
initialColors = [];
colorDivs.forEach((div, index) => {
const hexText = div.children[0];
const randomColor = generateHex();
if (div.children[1].children[1].classList.contains("locked")) {
initialColors.push(hexText.innerText);
return;
} else {
initialColors.push(chroma(randomColor).hex());
}
div.style.backgroundColor = randomColor;
hexText.innerText = randomColor;
/*contrast check*/
getTextContrast(randomColor, hexText);
const color = chroma(randomColor);
const sliders = div.querySelectorAll(".sliders input");
const hue = sliders[0];
const brightness = sliders[1];
const saturation = sliders[2];
colorizeSliders(color, hue, brightness, saturation);
});
resetInputs();
//button contrasts
adjustButton.forEach((button, index) => {
getTextContrast(initialColors[index], button);
getTextContrast(initialColors[index], lockButton[index]);
});
}
function getTextContrast(color, text) {
const luminance = chroma(color).luminance();
if (luminance > 0.5) {
text.style.color = "#333";
} else {
text.style.color = "#fff";
}
}
function colorizeSliders(color, hue, brightness, saturation) {
/* saturation */
const noSat = color.set("hsl.s", 0);
const fullSat = color.set("hsl.s", 1);
const scaleSat = chroma.scale([noSat, color, fullSat]);
/* brightness */
const midBright = color.set("hsl.l", 0.5);
const scaleBright = chroma.scale(["black", midBright, "white"]);
/* brightness */
/*update input*/
saturation.style.backgroundImage = `linear-gradient(to right, ${scaleSat(
0
)}, ${scaleSat(1)})`;
brightness.style.backgroundImage = `linear-gradient(to right, ${scaleBright(
0
)},${scaleBright(0.5)}, ${scaleBright(1)})`;
hue.style.backgroundImage = `linear-gradient(to right,
rgb(204,75,75),
rgb(204,204,75),
rgb(75,204,75),
rgb(75,204,204),
rgb(75,75,204),
rgb(204,75,204),
rgb(204,75,75))`;
}
function hslControls(e) {
const index =
e.target.getAttribute("data-bright") ||
e.target.getAttribute("data-sat") ||
e.target.getAttribute("data-hue");
let sliders = e.target.parentElement.querySelectorAll("input[type='range']");
const hue = sliders[0];
const brightness = sliders[1];
const saturation = sliders[2];
const bgColor = initialColors[index];
let color = chroma(bgColor)
.set("hsl.s", saturation.value)
.set("hsl.l", brightness.value)
.set("hsl.h", hue.value);
colorDivs[index].style.backgroundColor = color;
//colorize sliders
colorizeSliders(color, hue, saturation, brightness);
}
function updateTextUI(index) {
const activeDiv = colorDivs[index];
const color = chroma(activeDiv.style.backgroundColor);
const textHex = activeDiv.querySelector("h2");
const icons = activeDiv.querySelectorAll(".controls button");
textHex.innerText = color.hex();
//check text contrast
getTextContrast(color, textHex);
for (icon of icons) {
getTextContrast(color, icon);
}
}
function resetInputs() {
const sliders = document.querySelectorAll(".sliders input");
sliders.forEach((slider) => {
if (slider.name === "hue") {
const hueColor = initialColors[slider.getAttribute("data-hue")];
const hueValue = chroma(hueColor).hsl()[0];
slider.value = Math.floor(hueValue);
}
if (slider.name === "brightness") {
const brightColor = initialColors[slider.getAttribute("data-bright")];
const brightValue = chroma(brightColor).hsl()[2];
slider.value = Math.floor(brightValue * 100) / 100;
}
if (slider.name === "saturation") {
const satColor = initialColors[slider.getAttribute("data-sat")];
const satValue = chroma(satColor).hsl()[1];
slider.value = Math.floor(satValue * 100) / 100;
}
});
}
function copyToClipboard(hex) {
const el = document.createElement("textarea");
el.value = hex.innerText;
document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);
/* animation */
const popupBox = popup.children[0];
popupBox.classList.add("active");
popup.classList.add("active");
}
function toggleAdjustmentPanel(index) {
sliderContainers[index].classList.toggle("active");
}
function closeAdjustmentPanel(index) {
sliderContainers[index].classList.remove("active");
}
/* palette work */
//variables
const saveBtn = document.querySelector(".save");
const submitSave = document.querySelector(".submit-save");
const closeSave = document.querySelector(".close-save");
const saveContainer = document.querySelector(".save-container");
const saveInput = document.querySelector(".save-container input");
const libaryContainer = document.querySelector(".libary-container");
const libaryBtn = document.querySelector(".libary");
const closeLibaryBtn = document.querySelector(".close-libary");
//Event listeners
saveBtn.addEventListener("click", openPalette);
closeSave.addEventListener("click", closePalette);
submitSave.addEventListener("click", savePalette);
libaryBtn.addEventListener("click", openLibary);
closeLibaryBtn.addEventListener("click", closeLibary);
//Functions
function openPalette(e) {
const popup = saveContainer.children[0];
saveContainer.classList.add("active");
}
function closePalette(e) {
const popup = saveContainer.children[0];
saveContainer.classList.remove("active");
}
function savePalette(e) {
saveContainer.classList.remove("active");
popup.classList.remove("active");
const name = saveInput.value;
const colors = [];
currentHexes.forEach((hex) => {
colors.push(hex.innerText);
});
/*local storage */
let paletteNr;
paletteObjects = JSON.parse(localStorage.getItem("palettes"));
if (paletteObjects) {
paletteNr = paletteObjects.length;
} else {
paletteNr = savedPalettes.length;
}
const paletteObj = { name, colors, nr: paletteNr };
savedPalettes.push(paletteObj);
savetoLocal(paletteObj);
saveInput.value = "";
/*generate for libary */
createPalettes(paletteObj, savedPalettes, "create");
}
function savetoLocal(obj) {
let localPalettes;
if (localStorage.getItem("palettes") === null) {
localPalettes = [];
} else {
localPalettes = JSON.parse(localStorage.getItem("palettes"));
}
localPalettes.push(obj);
localStorage.setItem("palettes", JSON.stringify(localPalettes));
}
function openLibary() {
const popup = libaryContainer.children[0];
libaryContainer.classList.add("active");
popup.classList.add("active");
}
function closeLibary() {
const popup = libaryContainer.children[0];
libaryContainer.classList.remove("active");
popup.classList.remove("active");
}
function createPalettes(objOne, objTwo, todo) {
const palette = document.createElement("div");
const title = document.createElement("h4");
const preview = document.createElement("div");
palette.classList.add("custom-palette");
title.innerText = objOne.name;
preview.classList.add("small-preview");
objOne.colors.forEach((color) => {
const smallDiv = document.createElement("div");
smallDiv.style.background = color;
preview.appendChild(smallDiv);
});
const paletteBtn = document.createElement("button");
const paletteDeleteBtn = document.createElement("button");
paletteBtn.classList.add("pick-palette-btn");
paletteBtn.classList.add(objOne.nr);
paletteBtn.innerText = "select";
paletteDeleteBtn.classList.add("delete-palette");
paletteDeleteBtn.classList.add(objOne.nr);
paletteDeleteBtn.innerHTML = "<i class='fas fa-trash'></i>";
//attatch event to button
paletteBtn.addEventListener("click", (e) => {
closeLibary();
let i = -1;
let paletteIndex;
objTwo.forEach((x) => {
i++;
if (e.target.classList[1] == x.nr) {
paletteIndex = i;
}
});
initialColors = [];
objTwo[paletteIndex].colors.forEach((color, index) => {
initialColors.push(color);
colorDivs[index].style.background = color;
const text = colorDivs[index].children[0];
getTextContrast(color, text);
updateTextUI(index);
});
if (todo === "retrieve") {
resetInputs();
}
});
paletteDeleteBtn.addEventListener("click", (e) => {
const index = e.target.classList[1];
deletePalette(e, index);
});
//append to libary
palette.appendChild(title);
palette.appendChild(preview);
palette.appendChild(paletteBtn);
palette.appendChild(paletteDeleteBtn);
libaryContainer.children[0].appendChild(palette);
}
function getLocal() {
if (localStorage.getItem("palettes") === null) {
localPalettes = [];
} else {
const paletteObjects = JSON.parse(localStorage.getItem("palettes"));
paletteObjects.forEach((paletteObj) => {
createPalettes(paletteObj, paletteObjects, "retrieve");
});
}
}
function deletePalette(e, index) {
const paletteObjects = JSON.parse(localStorage.getItem("palettes"));
let i = 0;
paletteObjects.forEach((x) => {
if (x.name === e.target.parentElement.children[0].innerText) {
paletteObjects.splice(i, 1);
console.log(`removed ${i}: @${x.name}`);
}
});
updatedPalettes = JSON.stringify(paletteObjects);
localStorage.setItem("palettes", updatedPalettes);
e.target.parentElement.remove();
}
/* END palette work */
getLocal();
randomizeColor();