-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
400 lines (353 loc) · 12.3 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
395
396
397
398
399
400
// Global parameters and variables
const colorDivs = document.querySelectorAll(".color");
const generateBtn = document.querySelector(".generate");
const sliders = document.querySelectorAll("input[type='range']");
const currentHexes = document.querySelectorAll(".color h2");
const popup = document.querySelector(".copy-container");
const adjustButtons = document.querySelectorAll(".adjust");
const lockButton = document.querySelectorAll(".lock");
const closeAdjustments = document.querySelectorAll(".close-adjust");
const sliderContainers = document.querySelectorAll(".sliders");
let initialColors;
//This is for local storage
let savedPalettes = [];
//EVENT LISTNERS
generateBtn.addEventListener("click", randomColors);
sliders.forEach((slider) => {
slider.addEventListener("input", hslControls);
});
colorDivs.forEach((div, index) => {
div.addEventListener("change", () => {
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");
});
adjustButtons.forEach((button, index) => {
button.addEventListener("click", () => {
openAdjustmentPanel(index);
});
});
closeAdjustments.forEach((button, index) => {
button.addEventListener("click", () => {
closeAdjustmentPanel(index);
});
});
lockButton.forEach((button, index) => {
button.addEventListener("click", (e) => {
lockLayer(e, index);
});
});
// Functions
function generateHex() {
const hexColor = chroma.random();
return hexColor;
}
function randomColors() {
initialColors = [];
colorDivs.forEach((div, index) => {
//RESETS THE INITIALS COLORS EVERY TIME OF GENERATING THESE
const hexText = div.children[0];
const randomColor = generateHex();
//add it to the array
if (div.classList.contains("locked")) {
initialColors.push(hexText.innerText);
return;
} else {
initialColors.push(chroma(randomColor).hex());
}
// add the color to the background
div.style.backgroundColor = randomColor;
hexText.innerText = randomColor;
// check the contrast
checkTextContrast(randomColor, hexText);
//Initailize color sliders
const color = chroma(randomColor);
const slider = div.querySelectorAll(".sliders input");
const hue = slider[0];
const brightness = slider[1];
const saturation = slider[2];
colorizeSliders(color, hue, brightness, saturation);
});
//RESET THE INPUTS
resetInputs();
//Check For Buttonsn Contrast
adjustButtons.forEach((button, index) => {
checkTextContrast(initialColors[index], button);
checkTextContrast(initialColors[index], lockButton[index]);
});
}
function checkTextContrast(color, text) {
const luminance = chroma(color).luminance();
if (luminance > 0.5) {
text.style.color = "black";
} else {
text.style.color = "white";
}
}
function colorizeSliders(color, hue, brightness, saturation) {
//Scalize the Saturation
const noSat = color.set("hsl.s", 0);
const fullSat = color.set("hsl.s", 1);
const scaleSat = chroma.scale([noSat, color, fullSat]);
//Scalize the brightness
const midBright = color.set("hsl.l", 0.5);
const scaleBright = chroma.scale(["black", midBright, "white"]);
// update input colors
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("hue-data") ||
e.target.getAttribute("bright-data") ||
e.target.getAttribute("sat-data");
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.h", hue.value)
.set("hsl.l", brightness.value)
.set("hsl.s", saturation.value);
colorDivs[index].style.backgroundColor = color;
//Colorize the input
colorizeSliders(color, hue, brightness, saturation);
}
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();
checkTextContrast(color, textHex);
for (icon of icons) {
checkTextContrast(color, icon);
}
}
function resetInputs() {
const sliders = document.querySelectorAll(".sliders input");
sliders.forEach((slider) => {
if (slider.name === "hue") {
const hueColor = initialColors[slider.getAttribute("hue-data")];
const hueValue = chroma(hueColor).hsl()[0];
slider.value = Math.floor(hueValue);
}
if (slider.name === "brightness") {
const brightColor = initialColors[slider.getAttribute("bright-data")];
const brightValue = chroma(brightColor).hsl()[2];
slider.value = Math.floor(brightValue * 100) / 100;
}
if (slider.name === "saturation") {
const satColor = initialColors[slider.getAttribute("sat-data")];
const satValue = chroma(satColor).hsl()[2];
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);
//popup animation
const popupBox = popup.children[0];
popupBox.classList.add("active");
popup.classList.add("active");
}
function openAdjustmentPanel(index) {
sliderContainers[index].classList.toggle("active");
}
function closeAdjustmentPanel(index) {
sliderContainers[index].classList.remove("active");
}
function lockLayer(e, index) {
const lockSVG = e.target.children[0];
const activeBg = colorDivs[index];
activeBg.classList.toggle("locked");
if (lockSVG.classList.contains("fa-lock-open")) {
e.target.innerHTML = `<i class="fas fa-lock"></i>`;
} else {
e.target.innerHTML = `<i class="fas fa-lock-open"></i>`;
}
}
//lets implement some library and local storage stuff
const saveBtn = document.querySelector(".save");
const saveContainer = document.querySelector(".save-container");
const submitSave = document.querySelector(".submit-save");
const closeSave = document.querySelector(".close-save");
const saveInput = document.querySelector(".save-container input");
const libraryContainer = document.querySelector(".library-container");
const libraryBtn = document.querySelector(".library");
const closeLibraryBtn = document.querySelector(".close-library");
//EVENT LISTNERS
saveBtn.addEventListener("click", openPalette);
closeSave.addEventListener("click", closePalette);
submitSave.addEventListener("click", savePalettes);
libraryBtn.addEventListener("click", openLibrary);
closeLibraryBtn.addEventListener("click", closeLibrary);
//functions
function openPalette(e) {
const popup = saveContainer.children[0];
saveContainer.classList.add("active");
popup.classList.add("active");
}
function closePalette(e) {
const popup = saveContainer.children[0];
saveContainer.classList.remove("active");
popup.classList.remove("active");
}
function savePalettes() {
saveContainer.classList.remove("active");
popup.classList.remove("active");
const name = saveInput.value;
const colors = [];
currentHexes.forEach((hex) => {
colors.push(hex.innerText);
});
//create a palette OBJ
let paletteNr;
const paletteObjects = JSON.parse(localStorage.getItem("palettes"));
if (paletteObjects) {
paletteNr = paletteObjects.length;
} else {
paletteNr = savedPalettes.length;
}
//creating Palette Object
const paletteObj = { name, colors, nr: paletteNr };
savedPalettes.push(paletteObj);
// save to local storage
savetoLocal(paletteObj);
saveInput.value = "";
//ADDING PALETTES TO LIBRARY
const palette = document.createElement("div");
palette.classList.add("custom-palette");
const title = document.createElement("h4");
title.innerText = paletteObj.name;
const preview = document.createElement("div");
preview.classList.add("small-preview");
paletteObj.colors.forEach((smallColor) => {
const smallDiv = document.createElement("div");
smallDiv.style.backgroundColor = smallColor;
preview.appendChild(smallDiv);
});
const paletteBtn = document.createElement("button");
paletteBtn.classList.add("pick-palette-btn");
paletteBtn.classList.add(paletteObj.nr);
paletteBtn.innerText = "Select";
//EVENT LISTNER
paletteBtn.addEventListener("click", (e) => {
closeLibrary();
const paletteIndex = e.target.classList[1];
initialColors = [];
savedPalettes[paletteIndex].colors.forEach((color, index) => {
initialColors.push(color);
colorDivs[index].style.backgroundColor = color;
//colorize the sliders again
const colorForSlider = chroma(color);
const slider = colorDivs[index].querySelectorAll('input[type="range"]');
const hue = slider[0];
const brightness = slider[1];
const saturation = slider[2];
colorizeSliders(colorForSlider, hue, brightness, saturation);
updateTextUI(index);
});
resetInputs();
});
//APPENDING TO LIBRARY
palette.appendChild(title);
palette.appendChild(preview);
palette.appendChild(paletteBtn);
libraryContainer.children[0].appendChild(palette);
}
function savetoLocal(paletteObj) {
let localPalettes;
if (localStorage.getItem("palettes") === null) {
localPalettes = [];
} else {
//getting back the palettes from localstorage for appending to it
localPalettes = JSON.parse(localStorage.getItem("palettes"));
}
localPalettes.push(paletteObj);
localStorage.setItem("palettes", JSON.stringify(localPalettes));
}
function openLibrary() {
const popup = libraryContainer.children[0];
libraryContainer.classList.add("active");
popup.classList.add("active");
}
function closeLibrary() {
libraryContainer.classList.remove("active");
popup.classList.remove("active");
}
function getLocal() {
if (localStorage.getItem("palettes") === null) {
localPalettes = [];
} else {
paletteObjects = JSON.parse(localStorage.getItem("palettes"));
savedPalettes = [...paletteObjects];
paletteObjects.forEach((paletteObj) => {
//ADDING PALETTES TO LIBRARY
const palette = document.createElement("div");
palette.classList.add("custom-palette");
const title = document.createElement("h4");
title.innerText = paletteObj.name;
const preview = document.createElement("div");
preview.classList.add("small-preview");
paletteObj.colors.forEach((smallColor) => {
const smallDiv = document.createElement("div");
smallDiv.style.backgroundColor = smallColor;
preview.appendChild(smallDiv);
});
const paletteBtn = document.createElement("button");
paletteBtn.classList.add("pick-palette-btn");
paletteBtn.classList.add(paletteObj.nr);
paletteBtn.innerText = "Select";
//EVENT LISTNER
paletteBtn.addEventListener("click", (e) => {
closeLibrary();
const paletteIndex = e.target.classList[1];
initialColors = [];
paletteObjects[paletteIndex].colors.forEach((color, index) => {
initialColors.push(color);
//again colorize the slider
//colorize the sliders again
const colorForSlider = chroma(color);
const slider = colorDivs[index].querySelectorAll(
'input[type="range"]'
);
const hue = slider[0];
const brightness = slider[1];
const saturation = slider[2];
colorizeSliders(colorForSlider, hue, brightness, saturation);
colorDivs[index].style.backgroundColor = color;
updateTextUI(index);
});
resetInputs();
});
//APPENDING TO LIBRARY
palette.appendChild(title);
palette.appendChild(preview);
palette.appendChild(paletteBtn);
libraryContainer.children[0].appendChild(palette);
});
}
}
getLocal();
randomColors();