|
4 | 4 | <head> |
5 | 5 | <title></title> |
6 | 6 | <style> |
| 7 | + body { |
| 8 | + display: grid; |
| 9 | + grid-template-columns: auto auto; |
| 10 | + gap: 1px; |
| 11 | + } |
| 12 | + |
7 | 13 | #grid-container { |
8 | 14 | display: grid; |
9 | 15 | grid-template-columns: repeat(29, 32px); |
|
54 | 60 | .inactive { |
55 | 61 | opacity: 0.4; |
56 | 62 | } |
| 63 | + |
| 64 | + .active { |
| 65 | + /* margin: 4px; */ |
| 66 | + /* outline: 4px green */ |
| 67 | + } |
| 68 | + |
| 69 | + .palette-item { |
| 70 | + display: inline-block; |
| 71 | + vertical-align: top; |
| 72 | + margin: 5px; |
| 73 | + } |
| 74 | + |
| 75 | + .palette-item p { |
| 76 | + display: inline-block; |
| 77 | + vertical-align: top; |
| 78 | + margin: 0 0 0 10px; |
| 79 | + /* Add some space between the div and the text */ |
| 80 | + font-size: 12px; |
| 81 | + font-weight: bold; |
| 82 | + color: black; |
| 83 | + } |
57 | 84 | </style> |
58 | 85 | </head> |
59 | 86 |
|
60 | 87 | <body> |
61 | 88 | <div id="grid-container"> |
62 | 89 | <!-- Grid will be generated here --> |
63 | 90 | </div> |
| 91 | + <div id="palette-container"></div> |
64 | 92 |
|
65 | 93 | <script> |
| 94 | + function createPalette(colorData) { |
| 95 | + const container = document.getElementById('palette-container'); |
| 96 | + |
| 97 | + const allColors = colorData.flat() |
| 98 | + |
| 99 | + const allUniqueColors = [...new Set(allColors.map(pixel => pixel.color))]; |
| 100 | + const allUniquePixels = allUniqueColors.map(color => allColors.find(pixel => pixel.color === color)); |
| 101 | + |
| 102 | + allUniquePixels |
| 103 | + .sort((a, b) => a.number - b.number) |
| 104 | + .forEach(({ name, number, color }) => { |
| 105 | + const colorContainer = document.createElement('div'); |
| 106 | + colorContainer.style.display = 'flex' |
| 107 | + const colorDiv = document.createElement('div'); |
| 108 | + colorDiv.style.backgroundColor = color; |
| 109 | + colorDiv.style.width = '32px'; |
| 110 | + colorDiv.style.height = '32px'; |
| 111 | + colorDiv.style.border = '1px solid black'; |
| 112 | + colorDiv.className = 'palette-item'; |
| 113 | + const colorDivP = document.createElement('p'); |
| 114 | + colorDiv.append(colorDivP); |
| 115 | + colorDivP.textContent = number; |
| 116 | + |
| 117 | + const p = document.createElement('p'); |
| 118 | + p.textContent = `${number} ${name}`; |
| 119 | + |
| 120 | + colorContainer.appendChild(colorDiv); |
| 121 | + colorContainer.appendChild(p); |
| 122 | + container.appendChild(colorContainer); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
66 | 126 | function createGrid(colorData) { |
67 | 127 | const container = document.getElementById('grid-container'); |
68 | 128 | container.innerHTML = ''; // Clear existing grid |
|
71 | 131 | for (let col = 0; col < 29; col++) { |
72 | 132 | const div = document.createElement('div'); |
73 | 133 |
|
74 | | - const pixel = colorData[row][col] |
| 134 | + const pixel = colorData[row][col]; |
75 | 135 | div.style.backgroundColor = pixel.color; |
76 | 136 |
|
77 | 137 | const p = document.createElement('p'); |
| 138 | + p.style.display = 'none'; |
78 | 139 | p.textContent = pixel?.number; |
79 | 140 | div.appendChild(p); |
80 | 141 | container.appendChild(div); |
|
85 | 146 | } |
86 | 147 | } |
87 | 148 | } |
| 149 | + |
88 | 150 | // Function to move the selected class |
89 | 151 | function moveSelected(direction) { |
90 | 152 | const grid = document.getElementById('grid-container'); |
|
131 | 193 | } |
132 | 194 |
|
133 | 195 | // Function to read JSON file |
134 | | - async function readJSON(filename) { |
135 | | - } |
136 | | - |
| 196 | + async function readJSON(filename) { } |
137 | 197 |
|
138 | 198 | let selectedPValue = ''; |
139 | 199 | function handleSpacebarPress() { |
|
143 | 203 | const selectedColor = selectedDiv.style.backgroundColor; |
144 | 204 |
|
145 | 205 | // First make all divs active |
146 | | - Array.from(grid.children).forEach(div => div.classList.remove('inactive')); |
| 206 | + Array.from(grid.children).forEach(div => div.classList.remove('inactive', 'active')); |
147 | 207 |
|
148 | 208 | if (newSelectedPValue !== selectedPValue) { |
149 | 209 | // Add 'inactive' class to divs not matching the color |
150 | 210 | Array.from(grid.children).forEach(div => { |
151 | 211 | if (div.style.backgroundColor !== selectedColor) { |
152 | 212 | div.classList.add('inactive'); |
| 213 | + } else { |
| 214 | + div.classList.add('active'); |
| 215 | + |
153 | 216 | } |
154 | 217 | }); |
155 | 218 | selectedPValue = newSelectedPValue; |
156 | 219 | } else { |
157 | | - selectedPValue = '' |
| 220 | + |
| 221 | + selectedPValue = ''; |
158 | 222 | } |
159 | 223 | } |
160 | 224 |
|
161 | | - |
162 | 225 | // Event listener for arrow keys |
163 | 226 | document.addEventListener('keydown', function (event) { |
164 | 227 | if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(event.key)) { |
|
175 | 238 | const load = urlParams.get('t'); |
176 | 239 | url = `https://tinyurl.com/${load.replaceAll("_", "ZZ")}`; |
177 | 240 | } |
178 | | - |
| 241 | + |
179 | 242 | const imageData = await (await fetch(url)).json(); |
180 | 243 | createGrid(imageData); |
| 244 | + createPalette(imageData); |
181 | 245 | }); |
182 | 246 |
|
183 | 247 | document.addEventListener('keydown', function (event) { |
184 | 248 | if (event.key === ' ') { |
185 | 249 | handleSpacebarPress(); |
186 | 250 | } |
187 | 251 | }); |
188 | | - |
189 | 252 | </script> |
190 | 253 | </body> |
191 | 254 |
|
|
0 commit comments