-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscripts.js
More file actions
170 lines (148 loc) Β· 4.69 KB
/
Copy pathscripts.js
File metadata and controls
170 lines (148 loc) Β· 4.69 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
// Data Model π
var hexNum = ['A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
var currentPalette;
var savedPalettes = [];
// Global DOM variables π
var square1 = document.getElementById('color1');
var square2 = document.getElementById('color2');
var square3 = document.getElementById('color3');
var square4 = document.getElementById('color4');
var square5 = document.getElementById('color5');
var hex1 = document.getElementById('hex1');
var hex2 = document.getElementById('hex2');
var hex3 = document.getElementById('hex3');
var hex4 = document.getElementById('hex4');
var hex5 = document.getElementById('hex5');
var unlockIcons = document.querySelectorAll('.js-unlock-icon');
var savedPalettesSection = document.getElementById('savedPalettes');
var paletteSection = document.getElementById('palette');
// Global DOM button variables π
var newBtn = document.getElementById('newPaletteBtn');
var saveBtn = document.getElementById('savePaletteBtn');
// Classes π
class Color {
constructor() {
this.hex = this.getHex();
this.locked = false;
}
getHex() {
var color = '#';
for (var i = 0; i < 6; i++) {
color += this.getRandNum(hexNum);
}
return color;
}
getRandNum(array) {
return array[Math.floor(Math.random() * array.length)];
}
lock() {
this.locked = true;
}
unlock() {
this.locked = false;
}
}
class Palette {
constructor() {
this.id = Date.now();
this.color1 = new Color;
this.color2 = new Color;
this.color3 = new Color;
this.color4 = new Color;
this.color5 = new Color;
}
changeColor() {
for (var i = 1; i < 6; i++) {
if (!this[`color${i}`].locked) {
this[`color${i}`] = new Color;
}
}
}
switchLocked(color) {
if (this[color].locked) {
this[color].unlock();
} else if (!this[color].locked) {
this[color].lock();
}
}
}
// Event Listeners π
window.addEventListener('load', loadPalette);
newBtn.addEventListener('click', function() {
generateColors();
displayColors();
});
saveBtn.addEventListener('click', savePalette);
paletteSection.addEventListener('click', function() {
toggleLock(event);
});
savedPalettesSection.addEventListener('click', function() {
deleteSavedPalette(event);
});
// Functions π
function displayColors() {
square1.style.backgroundColor = currentPalette.color1.hex;
square2.style.backgroundColor = currentPalette.color2.hex;
square3.style.backgroundColor = currentPalette.color3.hex;
square4.style.backgroundColor = currentPalette.color4.hex;
square5.style.backgroundColor = currentPalette.color5.hex;
hex1.innerText = `${currentPalette.color1.hex}`;
hex2.innerText = `${currentPalette.color2.hex}`;
hex3.innerText = `${currentPalette.color3.hex}`;
hex4.innerText = `${currentPalette.color4.hex}`;
hex5.innerText = `${currentPalette.color5.hex}`;
}
function generateColors() {
currentPalette.changeColor();
displayColors();
}
function loadPalette() {
currentPalette = new Palette;
displayColors();
for (var i = 0; i < unlockIcons.length; i++) {
unlockIcons[i].innerText = 'lock_open';
}
}
function toggleLock(event) {
var boxId = event.target.id;
var palKeys = Object.keys(currentPalette);
var targetLock = event.target.nextElementSibling.children[1];
for (var i = 1; i < palKeys.length; i++) {
if (palKeys[i] === boxId) {
changeIcon(targetLock);
currentPalette.switchLocked(palKeys[i]);
break;
}
}
}
function changeIcon(icon) {
if (icon.innerText === 'lock_open') {
icon.innerText = `lock`;
} else {
icon.innerText = 'lock_open';
}
}
function savePalette() {
savedPalettes.push(currentPalette);
createMiniPalette();
loadPalette();
}
function createMiniPalette() {
savedPalettesSection.innerHTML +=
`<section class="saved-mini-palette">
<section style="background-color: ${currentPalette.color1.hex};" class="mini-square"></section>
<section style="background-color: ${currentPalette.color2.hex};" class="mini-square"></section>
<section style="background-color: ${currentPalette.color3.hex};" class="mini-square"></section>
<section style="background-color: ${currentPalette.color4.hex};" class="mini-square"></section>
<section style="background-color: ${currentPalette.color5.hex};" class="mini-square"></section>
<span class="material-symbols-outlined trash" id="${currentPalette.id}">delete</span>
</section>`;
}
function deleteSavedPalette(event) {
for (var i = 0; i < savedPalettes.length; i++) {
if (event.target.id === savedPalettes[i].id.toString() && window.confirm('Are you sure you want to delete this palette?')) {
savedPalettes.splice(i, 1);
event.target.parentNode.remove();
}
}
}