Skip to content

Commit 5e95ff3

Browse files
First Commit
1 parent 45f99ed commit 5e95ff3

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<link rel="stylesheet" href="style.css">
7+
<title>Random Color Palette Generator</title>
8+
</head>
9+
<body>
10+
<div class="palette">
11+
<!-- Color cards will be added dynamically here -->
12+
</div>
13+
<button id="generateButton">Generate Colors</button>
14+
<script src="script.js"></script>
15+
</body>
16+
</html>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const palette = document.querySelector('.palette');
2+
const generateButton = document.getElementById('generateButton');
3+
4+
// Generate a random color in hex format (#RRGGBB)
5+
function getRandomColor() {
6+
const letters = '0123456789ABCDEF';
7+
let color = '#';
8+
for (let i = 0; i < 6; i++) {
9+
color += letters[Math.floor(Math.random() * 16)];
10+
}
11+
return color;
12+
}
13+
14+
// Create and append a color card to the palette
15+
function createColorCard() {
16+
const colorCard = document.createElement('div');
17+
colorCard.classList.add('color-card');
18+
const hexCode = getRandomColor();
19+
colorCard.style.backgroundColor = hexCode;
20+
colorCard.innerHTML = `
21+
<div class="hex-code">${hexCode}</div>
22+
`;
23+
palette.appendChild(colorCard);
24+
25+
// Add click event to copy hex code to clipboard
26+
colorCard.addEventListener('click', () => {
27+
const tempInput = document.createElement('input');
28+
tempInput.value = hexCode;
29+
document.body.appendChild(tempInput);
30+
tempInput.select();
31+
document.execCommand('copy');
32+
document.body.removeChild(tempInput);
33+
alert(`Hex code ${hexCode} copied to clipboard!`);
34+
});
35+
}
36+
37+
// Generate a random color palette with a specified number of colors
38+
function generatePalette(numColors) {
39+
palette.innerHTML = ''; // Clear existing colors
40+
for (let i = 0; i < numColors; i++) {
41+
createColorCard();
42+
}
43+
}
44+
45+
// Event listener for the "Generate Colors" button
46+
generateButton.addEventListener('click', () => {
47+
const numColors = Math.floor(Math.random() * 4 + 1) * 2 * 2; // 10, 12, 14, or 16 colors
48+
generatePalette(numColors);
49+
});
50+
51+
// Initial palette generation
52+
generatePalette(10); // You can start with any initial number of colors
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
body {
2+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
3+
text-align: center;
4+
display: flex;
5+
flex-direction: column;
6+
align-items: center;
7+
justify-content: center;
8+
height: 100vh;
9+
margin: 0;
10+
}
11+
12+
.palette {
13+
display: flex;
14+
flex-wrap: wrap;
15+
justify-content: center;
16+
gap: 10px;
17+
}
18+
19+
.color-card {
20+
width: 100px;
21+
height: 100px;
22+
border: 2px solid #000;
23+
text-align: center;
24+
display: flex;
25+
flex-direction: column;
26+
justify-content: center;
27+
}
28+
29+
.hex-code {
30+
font-size: 16px;
31+
}
32+
33+
#generateButton {
34+
margin-top: 20px;
35+
padding: 10px 20px;
36+
font-size: 18px;
37+
}

0 commit comments

Comments
 (0)