|
| 1 | +// Adds event listener to generate a random palette when the button is clicked |
| 2 | +document.getElementById('generate-random-btn').addEventListener('click', generateRandomPalette); |
| 3 | +// Adds event listener to generate a palette based on a selected feature |
| 4 | +document.getElementById('generate-by-feature-btn').addEventListener('click', generatePaletteByFeature); |
| 5 | + |
| 6 | +// Function to generate a palette with random colors |
| 7 | +function generateRandomPalette() { |
| 8 | + generatePalette(generateRandomColor); |
| 9 | +} |
| 10 | + |
| 11 | +// Function to generate a palette based on a selected feature |
| 12 | +function generatePaletteByFeature() { |
| 13 | + const featureSelect = document.getElementById('feature-select'); |
| 14 | + const selectedFeature = featureSelect.value; |
| 15 | + |
| 16 | + if (!selectedFeature) { |
| 17 | + alert('Please, choose a feature to generate the palette.'); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + let colorGenerator; |
| 22 | + switch (selectedFeature) { |
| 23 | + case 'random': |
| 24 | + colorGenerator = generateRandomColor; |
| 25 | + break; |
| 26 | + case 'red': |
| 27 | + colorGenerator = generateRedColor; |
| 28 | + break; |
| 29 | + case 'blue': |
| 30 | + colorGenerator = generateBlueColor; |
| 31 | + break; |
| 32 | + case 'orange': |
| 33 | + colorGenerator = generateOrangeColor; |
| 34 | + break; |
| 35 | + case 'yellow': |
| 36 | + colorGenerator = generateYellowColor; |
| 37 | + break; |
| 38 | + case 'pink': |
| 39 | + colorGenerator = generatePinkColor; |
| 40 | + break; |
| 41 | + case 'green': |
| 42 | + colorGenerator = generateGreenColor; |
| 43 | + break; |
| 44 | + default: |
| 45 | + alert('feature not found. Please, choose a valid feature.'); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + generatePalette(colorGenerator); |
| 50 | +} |
| 51 | + |
| 52 | +// Function to generate a palette based on the provided color generator function |
| 53 | +function generatePalette(colorGenerator) { |
| 54 | + const colorPalette = document.getElementById('color-palette'); |
| 55 | + colorPalette.innerHTML = ''; // Clears the existing palette |
| 56 | + const paletteColors = new Set(); // Set to store generated colors |
| 57 | + |
| 58 | + for (let i = 0; i < 5; i++) { |
| 59 | + let color = colorGenerator(); |
| 60 | + while (paletteColors.has(color)) { |
| 61 | + color = colorGenerator(); |
| 62 | + } |
| 63 | + paletteColors.add(color); |
| 64 | + |
| 65 | + const colorBox = document.createElement('div'); |
| 66 | + colorBox.classList.add('color-box'); |
| 67 | + colorBox.style.backgroundColor = color; |
| 68 | + colorBox.addEventListener('click', () => { |
| 69 | + const copyButton = colorBox.nextSibling; |
| 70 | + const oldColor = colorBox.style.backgroundColor; |
| 71 | + let newColor = colorGenerator(); |
| 72 | + while (paletteColors.has(newColor)) { |
| 73 | + newColor = colorGenerator(); |
| 74 | + } |
| 75 | + colorBox.style.backgroundColor = newColor; |
| 76 | + // Updates the displayed hex code |
| 77 | + copyButton.innerText = newColor; |
| 78 | + paletteColors.delete(oldColor); // Removes the previous color from the set |
| 79 | + paletteColors.add(newColor); // Adds the new color to the set |
| 80 | + }); |
| 81 | + |
| 82 | + // Adds a button to copy the hexadecimal code |
| 83 | + const copyButton = document.createElement('button'); |
| 84 | + copyButton.innerText = color; |
| 85 | + copyButton.addEventListener('click', () => { |
| 86 | + copyToClipboard(color); |
| 87 | + alert('The hex code has been copied. ' + color); |
| 88 | + }); |
| 89 | + |
| 90 | + const colorInfo = document.createElement('div'); |
| 91 | + colorInfo.appendChild(colorBox); |
| 92 | + colorInfo.appendChild(copyButton); |
| 93 | + colorPalette.appendChild(colorInfo); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// Function to generate a random hexadecimal color code |
| 98 | +function generateRandomColor() { |
| 99 | + return '#' + Math.floor(Math.random()*16777215).toString(16); |
| 100 | +} |
| 101 | + |
| 102 | +// Function to generate a random red color |
| 103 | +function generateRedColor() { |
| 104 | + // Generates a hexadecimal value for red between 128 (80 in hex) and 255 (FF in hex) |
| 105 | + const red = Math.floor(Math.random() * (255 - 128) + 128).toString(16); |
| 106 | + // Keeps green and blue at low values to maintain the color close to red |
| 107 | + const green = Math.floor(Math.random() * 56).toString(16); // up to 55 (37 in hex) |
| 108 | + const blue = Math.floor(Math.random() * 56).toString(16); // up to 55 (37 in hex) |
| 109 | + return `#${red}${green.padStart(2, '0')}${blue.padStart(2, '0')}`; |
| 110 | +} |
| 111 | + |
| 112 | +// Function to generate a random green color |
| 113 | +function generateGreenColor() { |
| 114 | + const red = Math.floor(Math.random() * 56).toString(16); |
| 115 | + const green = Math.floor(Math.random() * (255 - 128) + 128).toString(16); |
| 116 | + const blue = Math.floor(Math.random() * 56).toString(16); |
| 117 | + return `#${red.padStart(2, '0')}${green}${blue.padStart(2, '0')}`; |
| 118 | +} |
| 119 | + |
| 120 | +// Function to generate a random orange color |
| 121 | +function generateOrangeColor() { |
| 122 | + const red = Math.floor(Math.random() * (255 - 200) + 200).toString(16); |
| 123 | + const green = Math.floor(Math.random() * (165 - 100) + 100).toString(16); |
| 124 | + const blue = Math.floor(Math.random() * 56).toString(16); |
| 125 | + return `#${red}${green}${blue.padStart(2, '0')}`; |
| 126 | +} |
| 127 | + |
| 128 | +// Function to generate a random blue color |
| 129 | +function generateBlueColor() { |
| 130 | + const red = Math.floor(Math.random() * 56).toString(16); |
| 131 | + const green = Math.floor(Math.random() * 56).toString(16); |
| 132 | + const blue = Math.floor(Math.random() * (255 - 128) + 128).toString(16); |
| 133 | + return `#${red.padStart(2, '0')}${green.padStart(2, '0')}${blue}`; |
| 134 | +} |
| 135 | + |
| 136 | +// Function to generate a random pink color |
| 137 | +function generatePinkColor() { |
| 138 | + const red = Math.floor(Math.random() * (255 - 200) + 200).toString(16); |
| 139 | + const green = Math.floor(Math.random() * (160 - 100) + 100).toString(16); |
| 140 | + const blue = Math.floor(Math.random() * (160 - 100) + 100).toString(16); |
| 141 | + return `#${red}${green}${blue}`; |
| 142 | +} |
| 143 | + |
| 144 | +// Function to generate a random yellow color |
| 145 | +function generateYellowColor() { |
| 146 | + const red = Math.floor(Math.random() * (255 - 200) + 200).toString(16); |
| 147 | + const green = Math.floor(Math.random() * (255 - 200) + 200).toString(16); |
| 148 | + const blue = Math.floor(Math.random() * 56).toString(16); |
| 149 | + return `#${red}${green}${blue.padStart(2, '0')}`; |
| 150 | +} |
| 151 | + |
| 152 | +// Function to copy text to clipboard |
| 153 | +function copyToClipboard(text) { |
| 154 | + const tempInput = document.createElement('input'); |
| 155 | + tempInput.value = text; |
| 156 | + document.body.appendChild(tempInput); |
| 157 | + tempInput.select(); |
| 158 | + document.execCommand('copy'); |
| 159 | + document.body.removeChild(tempInput); |
| 160 | +} |
0 commit comments