Lachlan McMurtrie
create a page called color distributions
bd2a94b verified
Raw History Blame Contribute Delete
3.67 kB
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);
}
});