-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
102 lines (87 loc) · 3.16 KB
/
script.js
File metadata and controls
102 lines (87 loc) · 3.16 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
document.addEventListener('DOMContentLoaded', function() {
const isMobile = window.innerWidth <= 700; // Adjust the width for mobile devices
const generateButton = document.querySelector('.generate-button');
const lines = document.querySelectorAll('.line');
// Initialize locked state for each line
const lockedLines = Array.from(lines).map(() => false);
// Set default colors and add lock icon on page load
setDefaultColors();
// Add event listener for space bar press on desktop
if (!isMobile) {
document.addEventListener('keydown', function(event) {
if (event.code === 'Space') {
generateColors();
}
});
} else {
// Show the generate button only on mobile
generateButton.style.display = 'flex';
generateButton.addEventListener('click', function() {
generateColors();
});
}
function setDefaultColors() {
lines.forEach((line, index) => {
line.style.backgroundColor = generateRandomColor();
addLockIcon(line, index);
});
}
function generateColors() {
lines.forEach((line, index) => {
// Check if the line is not locked before changing the color
if (!lockedLines[index]) {
line.style.backgroundColor = generateRandomColor();
}
});
}
function generateRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function addLockIcon(line, index) {
const lockIcon = document.createElement('i');
lockIcon.className = 'fa-solid fa-lock additional-icon';
lockIcon.onclick = function() {
toggleLock(index, lockIcon);
};
line.appendChild(lockIcon);
}
function toggleLock(index, lockIcon) {
lockedLines[index] = !lockedLines[index];
const newColor = lockedLines[index] ? 'red' : 'white';
lockIcon.style.color = newColor;
}
});
function copyColorCode(icon) {
const line = icon.parentElement;
const rgbColorCode = line.style.backgroundColor;
// Convert RGB to HEX
const hexColorCode = rgbToHex(rgbColorCode);
// Create a temporary input element to copy the color code
const tempInput = document.createElement('input');
document.body.appendChild(tempInput);
tempInput.value = hexColorCode;
tempInput.select();
document.execCommand('copy');
document.body.removeChild(tempInput);
// Provide visual feedback to the user (optional)
icon.style.color = 'red';
setTimeout(() => {
icon.style.color = 'white';
}, 1000);
}
// Function to convert RGB to HEX
function rgbToHex(rgb) {
// Extract the RGB values from the string
const rgbArray = rgb.match(/\d+/g);
// Convert each value to HEX and concatenate
const hexCode = rgbArray.map(value => {
const hexValue = parseInt(value).toString(16);
return hexValue.length === 1 ? "0" + hexValue : hexValue;
}).join('');
return "#" + hexCode.toUpperCase();
}