-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
219 lines (185 loc) · 6.88 KB
/
script.js
File metadata and controls
219 lines (185 loc) · 6.88 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const colorPalette = document.getElementById('color-palette');
let colors = [];
let lockedColors = [];
function generateRandomColor() {
return {
h: Math.floor(Math.random() * 360),
s: Math.floor(Math.random() * 100),
l: Math.floor(Math.random() * 100)
};
}
function hslToHex({ h, s, l }) {
l /= 100;
const a = s * Math.min(l, 1 - l) / 100;
const f = n => {
const k = (n + h / 30) % 12;
const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
return Math.round(255 * color).toString(16).padStart(2, '0');
};
return `#${f(0)}${f(8)}${f(4)}`.toUpperCase();
}
function getContrastColor(h, s, l) {
return l > 50 ? '#000000' : '#ffffff';
}
function createColorBlock(color, index) {
const hexColor = hslToHex(color);
const contrastColor = getContrastColor(color.h, color.s, color.l);
const colorBlock = document.createElement('div');
colorBlock.className = 'color-block';
colorBlock.style.backgroundColor = hexColor;
colorBlock.setAttribute('data-index', index);
const lockIcon = lockedColors[index] ?
`<i class="fas fa-lock" style="color: ${contrastColor};"></i>` :
`<i class="fas fa-unlock" style="color: ${contrastColor};"></i>`;
colorBlock.innerHTML = `
<div class="color-content">
<div class="color-info" style="color: ${contrastColor}">
<div class="color-hex">${hexColor}</div>
</div>
<div class="button-group">
<button class="button copy-btn" style="color: ${contrastColor}">
<i class="fas fa-copy"></i>
</button>
<button class="button lock-btn" style="color: ${contrastColor}">
${lockIcon}
</button>
<button class="button delete-btn" style="color: ${contrastColor}">
<i class="fas fa-times"></i>
</button>
</div>
</div>
`;
colorBlock.querySelector('.delete-btn').addEventListener('click', () => deleteColor(index));
colorBlock.querySelector('.lock-btn').addEventListener('click', () => toggleLock(index));
colorBlock.querySelector('.copy-btn').addEventListener('click', () => copyToClipboard(hexColor));
return colorBlock;
}
function renderColors() {
colorPalette.innerHTML = '';
colors.forEach((color, index) => {
const colorBlock = createColorBlock(color, index);
colorPalette.appendChild(colorBlock);
});
handleResponsiveLayout(); // Add this line to ensure proper layout after rendering
}
function generateNewColors() {
colors = colors.map((color, index) => lockedColors[index] ? color : generateRandomColor());
renderColors();
}
function toggleLock(index) {
lockedColors[index] = !lockedColors[index];
renderColors();
}
function deleteColor(index) {
if (colors.length > 1) {
colors.splice(index, 1);
lockedColors.splice(index, 1);
renderColors();
}
}
for (let i = 0; i < 5; i++) {
colors.push(generateRandomColor());
lockedColors.push(false);
}
renderColors();
document.addEventListener('keydown', (event) => {
if (event.code === 'Space') {
event.preventDefault();
generateNewColors();
}
});
function copyToClipboard(hexColor) {
navigator.clipboard.writeText(hexColor);
}
document.getElementById('download-palette').addEventListener('click', openDownloadDialog);
document.querySelector('.close-button').addEventListener('click', closeDownloadDialog);
document.querySelectorAll('.download-option').forEach(button => {
button.addEventListener('click', function() {
const format = this.getAttribute('data-format');
closeDownloadDialog();
});
});
function openDownloadDialog() {
document.getElementById('download-dialog').style.display = 'block';
}
function closeDownloadDialog() {
document.getElementById('download-dialog').style.display = 'none';
}
function handleResponsiveLayout() {
const colorBlocks = document.querySelectorAll('.color-block');
if (window.innerWidth <= 1024) {
const headerHeight = document.querySelector('.header').offsetHeight;
const footerHeight = document.querySelector('.footer').offsetHeight;
const availableHeight = window.innerHeight - headerHeight - footerHeight;
const blockHeight = availableHeight / colors.length;
colorBlocks.forEach(block => {
block.style.height = `${blockHeight}px`;
});
} else {
colorBlocks.forEach(block => {
block.style.height = 'auto';
});
}
showGenerateButton();
}
window.addEventListener('resize', () => {
handleResponsiveLayout();
showGenerateButton();
});
function showGenerateButton() {
const footer = document.querySelector('.footer');
if (window.innerWidth <= 1024) {
footer.style.display = 'block';
} else {
footer.style.display = 'none';
}
}
showGenerateButton();
document.getElementById('generate-btn').addEventListener('click', generateNewColors);
// Add these functions to the existing JavaScript file
function openViewDialog() {
const viewDialog = document.getElementById('view-dialog');
const paletteDetails = document.getElementById('palette-details');
const colorPreview = document.getElementById('color-preview');
const colorDetails = document.getElementById('color-details');
paletteDetails.innerHTML = '';
colors.forEach((color, index) => {
const hexColor = hslToHex(color);
const colorDiv = document.createElement('div');
colorDiv.className = 'color-preview';
colorDiv.style.backgroundColor = hexColor;
colorDiv.addEventListener('click', () => {
colorPreview.style.backgroundColor = hexColor;
colorDetails.innerHTML = `
<strong>Color ${index + 1}</strong><br>
HEX: ${hexColor}<br>
HSL: ${color.h}, ${color.s}%, ${color.l}%
`;
});
paletteDetails.appendChild(colorDiv);
});
// Optionally, select the first color by default
if (colors.length > 0) {
const firstColorHex = hslToHex(colors[0]);
colorPreview.style.backgroundColor = firstColorHex;
colorDetails.innerHTML = `
<strong>Color 1</strong><br>
HEX: ${firstColorHex}<br>
HSL: ${colors[0].h}, ${colors[0].s}%, ${colors[0].l}%
`;
}
viewDialog.style.display = 'block';
}
function closeViewDialog() {
document.getElementById('view-dialog').style.display = 'none';
}
document.getElementById('view-palette').addEventListener('click', openViewDialog);
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', closeViewDialog);
});
window.addEventListener('click', function(event) {
if (event.target.classList.contains('modal')) {
closeViewDialog();
closeDownloadDialog();
}
});