-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathquote_generatoe.js
More file actions
116 lines (101 loc) · 2.96 KB
/
Copy pathquote_generatoe.js
File metadata and controls
116 lines (101 loc) · 2.96 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
/**
* Color Palette Generator
* Generates random color palettes and allows copying hex codes
*/
/**
* Generates a random hex color code
* @returns {string} Random hex color (e.g., "#A3B2C4")
*/
function generateRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
/**
* Generates a palette of 5 random colors
* Creates color boxes and adds them to the DOM
*/
function generatePalette() {
const paletteContainer = document.getElementById('paletteContainer');
paletteContainer.innerHTML = ''; // Clear existing palette
// Generate 5 color boxes
for (let i = 0; i < 5; i++) {
const color = generateRandomColor();
const colorBox = createColorBox(color);
paletteContainer.appendChild(colorBox);
}
}
/**
* Creates a color box element
* @param {string} color - Hex color code
* @returns {HTMLElement} Color box div element
*/
function createColorBox(color) {
const colorBox = document.createElement('div');
colorBox.className = 'color-box';
colorBox.style.backgroundColor = color;
colorBox.innerHTML = `
<div class="color-code">${color}</div>
`;
// Add click event to copy color code
colorBox.addEventListener('click', () => copyToClipboard(color));
return colorBox;
}
/**
* Copies text to clipboard and shows notification
* @param {string} text - Text to copy (hex color code)
*/
function copyToClipboard(text) {
// Use modern Clipboard API
navigator.clipboard.writeText(text).then(() => {
showNotification();
}).catch(err => {
console.error('Failed to copy: ', err);
// Fallback for older browsers
fallbackCopyToClipboard(text);
});
}
/**
* Fallback copy method for older browsers
* @param {string} text - Text to copy
*/
function fallbackCopyToClipboard(text) {
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.style.position = 'fixed';
textArea.style.left = '-9999px';
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand('copy');
showNotification();
} catch (err) {
console.error('Fallback copy failed: ', err);
}
document.body.removeChild(textArea);
}
/**
* Shows notification message temporarily
*/
function showNotification() {
const notification = document.getElementById('notification');
notification.style.display = 'block';
// Hide notification after 2 seconds
setTimeout(() => {
notification.style.display = 'none';
}, 2000);
}
/**
* Allows generating new palette with spacebar
*/
document.addEventListener('keydown', (e) => {
if (e.code === 'Space') {
e.preventDefault(); // Prevent page scroll
generatePalette();
}
});
// Generate initial palette on page load
window.addEventListener('load', generatePalette);