|
| 1 | +const generateColorButton = document.getElementById("generate-color"); |
| 2 | +const hexValue = document.getElementById("hex-value"); |
| 3 | +const rgbValue = document.getElementById("rgb-value"); |
| 4 | +const hslValue = document.getElementById("hsl-value"); |
| 5 | +const copyButton = document.getElementById("copy-button"); |
| 6 | +const colorBox = document.querySelector(".color-box"); |
| 7 | +const colorHistory = document.querySelector(".color-history"); |
| 8 | + |
| 9 | +generateColorButton.addEventListener("click", generateRandomColor); |
| 10 | +copyButton.addEventListener("click", copyToClipboard); |
| 11 | + |
| 12 | +function generateRandomColor() { |
| 13 | + // Generate a random color |
| 14 | + const randomColor = '#' + Math.floor(Math.random() * 16777215).toString(16); |
| 15 | + |
| 16 | + // Update color values |
| 17 | + hexValue.textContent = randomColor; |
| 18 | + const rgbColor = hexToRgb(randomColor); |
| 19 | + rgbValue.textContent = `rgb(${rgbColor.r}, ${rgbColor.g}, ${rgbColor.b})`; |
| 20 | + const hslColor = rgbToHsl(rgbColor.r, rgbColor.g, rgbColor.b); |
| 21 | + hslValue.textContent = `hsl(${hslColor.h}, ${hslColor.s}%, ${hslColor.l}%)`; |
| 22 | + |
| 23 | + // Update color preview |
| 24 | + colorBox.style.backgroundColor = randomColor; |
| 25 | + |
| 26 | + // Add the generated color to the history panel |
| 27 | + const colorHistoryItem = document.createElement("div"); |
| 28 | + colorHistoryItem.classList.add("color-history-item"); |
| 29 | + colorHistoryItem.style.backgroundColor = randomColor; |
| 30 | + colorHistory.appendChild(colorHistoryItem); |
| 31 | +} |
| 32 | + |
| 33 | +function copyToClipboard() { |
| 34 | + const textToCopy = hexValue.textContent; |
| 35 | + |
| 36 | + // Create a text area element, set its value, and select it for copying |
| 37 | + const textArea = document.createElement("textarea"); |
| 38 | + textArea.value = textToCopy; |
| 39 | + document.body.appendChild(textArea); |
| 40 | + textArea.select(); |
| 41 | + document.execCommand("copy"); |
| 42 | + document.body.removeChild(textArea); |
| 43 | + |
| 44 | + // Provide user feedback (e.g., change the button text temporarily) |
| 45 | + copyButton.textContent = "Copied!"; |
| 46 | + setTimeout(() => { |
| 47 | + copyButton.textContent = "Copy to Clipboard"; |
| 48 | + }, 2000); |
| 49 | +} |
| 50 | + |
| 51 | +// Helper functions for color conversion |
| 52 | +function hexToRgb(hex) { |
| 53 | + const bigint = parseInt(hex.slice(1), 16); |
| 54 | + const r = (bigint >> 16) & 255; |
| 55 | + const g = (bigint >> 8) & 255; |
| 56 | + const b = bigint & 255; |
| 57 | + return { r, g, b }; |
| 58 | +} |
| 59 | + |
| 60 | +function rgbToHsl(r, g, b) { |
| 61 | + r /= 255; |
| 62 | + g /= 255; |
| 63 | + b /= 255; |
| 64 | + |
| 65 | + const max = Math.max(r, g, b); |
| 66 | + const min = Math.min(r, g, b); |
| 67 | + let h, s, l = (max + min) / 2; |
| 68 | + |
| 69 | + if (max === min) { |
| 70 | + h = s = 0; // grayscale |
| 71 | + } else { |
| 72 | + const d = max - min; |
| 73 | + s = l > 0.5 ? d / (2 - max - min) : d / (max + min); |
| 74 | + switch (max) { |
| 75 | + case r: |
| 76 | + h = (g - b) / d + (g < b ? 6 : 0); |
| 77 | + break; |
| 78 | + case g: |
| 79 | + h = (b - r) / d + 2; |
| 80 | + break; |
| 81 | + case b: |
| 82 | + h = (r - g) / d + 4; |
| 83 | + break; |
| 84 | + } |
| 85 | + h /= 6; |
| 86 | + } |
| 87 | + |
| 88 | + return { h: Math.round(h * 360), s: Math.round(s * 100), l: Math.round(l * 100) }; |
| 89 | +} |
0 commit comments