Skip to content

Commit 4b8f9de

Browse files
committed
beads.html with palette separated
1 parent e93e0e6 commit 4b8f9de

File tree

1 file changed

+72
-9
lines changed

1 file changed

+72
-9
lines changed

beads.html

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
<head>
55
<title></title>
66
<style>
7+
body {
8+
display: grid;
9+
grid-template-columns: auto auto;
10+
gap: 1px;
11+
}
12+
713
#grid-container {
814
display: grid;
915
grid-template-columns: repeat(29, 32px);
@@ -54,15 +60,69 @@
5460
.inactive {
5561
opacity: 0.4;
5662
}
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+
}
5784
</style>
5885
</head>
5986

6087
<body>
6188
<div id="grid-container">
6289
<!-- Grid will be generated here -->
6390
</div>
91+
<div id="palette-container"></div>
6492

6593
<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+
66126
function createGrid(colorData) {
67127
const container = document.getElementById('grid-container');
68128
container.innerHTML = ''; // Clear existing grid
@@ -71,10 +131,11 @@
71131
for (let col = 0; col < 29; col++) {
72132
const div = document.createElement('div');
73133

74-
const pixel = colorData[row][col]
134+
const pixel = colorData[row][col];
75135
div.style.backgroundColor = pixel.color;
76136

77137
const p = document.createElement('p');
138+
p.style.display = 'none';
78139
p.textContent = pixel?.number;
79140
div.appendChild(p);
80141
container.appendChild(div);
@@ -85,6 +146,7 @@
85146
}
86147
}
87148
}
149+
88150
// Function to move the selected class
89151
function moveSelected(direction) {
90152
const grid = document.getElementById('grid-container');
@@ -131,9 +193,7 @@
131193
}
132194

133195
// Function to read JSON file
134-
async function readJSON(filename) {
135-
}
136-
196+
async function readJSON(filename) { }
137197

138198
let selectedPValue = '';
139199
function handleSpacebarPress() {
@@ -143,22 +203,25 @@
143203
const selectedColor = selectedDiv.style.backgroundColor;
144204

145205
// 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'));
147207

148208
if (newSelectedPValue !== selectedPValue) {
149209
// Add 'inactive' class to divs not matching the color
150210
Array.from(grid.children).forEach(div => {
151211
if (div.style.backgroundColor !== selectedColor) {
152212
div.classList.add('inactive');
213+
} else {
214+
div.classList.add('active');
215+
153216
}
154217
});
155218
selectedPValue = newSelectedPValue;
156219
} else {
157-
selectedPValue = ''
220+
221+
selectedPValue = '';
158222
}
159223
}
160224

161-
162225
// Event listener for arrow keys
163226
document.addEventListener('keydown', function (event) {
164227
if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(event.key)) {
@@ -175,17 +238,17 @@
175238
const load = urlParams.get('t');
176239
url = `https://tinyurl.com/${load.replaceAll("_", "ZZ")}`;
177240
}
178-
241+
179242
const imageData = await (await fetch(url)).json();
180243
createGrid(imageData);
244+
createPalette(imageData);
181245
});
182246

183247
document.addEventListener('keydown', function (event) {
184248
if (event.key === ' ') {
185249
handleSpacebarPress();
186250
}
187251
});
188-
189252
</script>
190253
</body>
191254

0 commit comments

Comments
 (0)