File size: 3,674 Bytes
bd2a94b | 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 | document.addEventListener('DOMContentLoaded', function() {
// Color copy functionality
document.querySelectorAll('[data-color]').forEach(element => {
element.addEventListener('click', function() {
const color = '#' + this.getAttribute('data-color');
navigator.clipboard.writeText(color).then(() => {
showToast();
});
});
});
// Random color generator
const randomColorsContainer = document.getElementById('random-colors');
const generateBtn = document.getElementById('generate-colors');
function generateRandomColors() {
randomColorsContainer.innerHTML = '';
for (let i = 0; i < 10; i++) {
const color = Math.floor(Math.random()*16777215).toString(16).padStart(6, '0');
const colorDiv = document.createElement('div');
colorDiv.className = 'h-12 rounded hover:opacity-80 cursor-pointer transition-all fade-in';
colorDiv.style.backgroundColor = `#${color}`;
colorDiv.setAttribute('data-color', color);
colorDiv.addEventListener('click', function() {
navigator.clipboard.writeText(`#${color}`).then(() => {
showToast();
});
});
randomColorsContainer.appendChild(colorDiv);
}
}
generateBtn.addEventListener('click', generateRandomColors);
generateRandomColors(); // Initial generation
// Gradient generator
const gradientStart = document.getElementById('gradient-start');
const gradientEnd = document.getElementById('gradient-end');
const gradientPreview = document.getElementById('gradient-preview');
const gradientSteps = document.getElementById('gradient-steps');
function updateGradient() {
const startColor = gradientStart.value;
const endColor = gradientEnd.value;
gradientPreview.style.background = `linear-gradient(90deg, ${startColor}, ${endColor})`;
gradientSteps.innerHTML = '';
for (let i = 0; i < 10; i++) {
const ratio = i / 9;
const r = Math.ceil(parseInt(startColor.substring(1, 3), 16) * (1 - ratio) + parseInt(endColor.substring(1, 3), 16) * ratio);
const g = Math.ceil(parseInt(startColor.substring(3, 5), 16) * (1 - ratio) + parseInt(endColor.substring(3, 5), 16) * ratio);
const b = Math.ceil(parseInt(startColor.substring(5, 7), 16) * (1 - ratio) + parseInt(endColor.substring(5, 7), 16) * ratio);
const color = `#${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)}`;
const step = document.createElement('div');
step.className = 'hover:opacity-80 cursor-pointer transition-all';
step.style.backgroundColor = color;
step.setAttribute('data-color', color.substring(1));
step.addEventListener('click', function() {
navigator.clipboard.writeText(color).then(() => {
showToast();
});
});
gradientSteps.appendChild(step);
}
}
gradientStart.addEventListener('input', updateGradient);
gradientEnd.addEventListener('input', updateGradient);
updateGradient(); // Initial update
// Toast notification
function showToast() {
const toast = document.getElementById('toast');
toast.classList.remove('opacity-0');
toast.classList.add('opacity-100');
setTimeout(() => {
toast.classList.remove('opacity-100');
toast.classList.add('opacity-0');
}, 2000);
}
}); |