-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchromacode-lab.html
More file actions
512 lines (449 loc) · 19.9 KB
/
Copy pathchromacode-lab.html
File metadata and controls
512 lines (449 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Chromacode Lab</title>
<link rel="stylesheet" href="styles.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.0/p5.min.js"></script>
<style>
#canvas-container {
margin-top: 20px;
border: 2px solid #4a4a4a;
background: #e8e8e8;
overflow: hidden;
position: relative;
}
#canvas-container canvas {
display: block;
}
.palette-grid {
display: flex;
gap: 10px;
flex-wrap: wrap;
margin-top: 10px;
}
.palette-btn {
display: flex;
flex-direction: column;
align-items: center;
gap: 6px;
border: 2px solid #4a4a4a;
background: #ffffff;
padding: 10px 14px;
cursor: pointer;
transition: all 0.2s ease;
font-size: 0.8em;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.5px;
color: #1a1a1a;
}
.palette-btn:hover {
border-color: #c41e3a;
}
.palette-btn.active {
border-color: #1a1a1a;
border-width: 3px;
}
.palette-swatches {
display: flex;
gap: 3px;
}
.swatch {
width: 14px;
height: 14px;
border: 1px solid rgba(0,0,0,0.15);
}
.controls-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-bottom: 20px;
}
@media (max-width: 768px) {
.controls-row { grid-template-columns: 1fr; }
}
.output-actions {
display: flex;
gap: 12px;
margin-top: 16px;
align-items: center;
flex-wrap: wrap;
}
#wordCount {
font-size: 0.85em;
color: #4a4a4a;
font-style: italic;
margin-left: auto;
}
#placeholder-msg {
padding: 60px 20px;
text-align: center;
color: #4a4a4a;
font-style: italic;
font-size: 1.05em;
}
.legend-row {
display: flex;
gap: 20px;
margin-top: 12px;
font-size: 0.8em;
color: #4a4a4a;
flex-wrap: wrap;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-swatch {
width: 24px;
height: 8px;
border: 1px solid rgba(0,0,0,0.2);
}
</style>
</head>
<body>
<nav class="main-nav">
<a href="index.html">Home</a>
<a href="text-reverse-lab.html">Text Reversal</a>
<a href="line-break-lab.html">Line Break Lab</a>
<a href="eraser-lab.html">Eraser Lab</a>
<a href="alphabetizer-lab.html">Alphabetizer Lab</a>
<a href="markov-mashup-lab.html">Markov Mashup Lab</a>
<a href="lipogram-lab.html">Lipogram Lab</a>
<a href="long-tail-lab.html">Long Tail Lab</a>
<a href="grid-it-lab.html">Grid-It Lab</a>
<a href="letter-replacement-lab.html">Letter Swap</a>
<a href="chromacode-lab.html" class="active">Chromacode</a>
<a href="about.html">About</a>
</nav>
<div class="container">
<div class="header">
<h1>Chromacode Lab</h1>
<p>Translate text into color and form — a visual encoding of your words</p>
</div>
<p style="margin-bottom: 30px; line-height: 1.8; color: #4a4a4a;">
Upload a text file and watch each word become a colored block along thin horizontal bands.
Words that repeat appear in the same color; word length determines block width.
The result is a visual fingerprint of your text — its rhythms, repetitions, and structure
rendered as an abstract score.
</p>
<div class="upload-area" id="uploadArea" style="margin-bottom: 20px;">
<div class="upload-icon">📄</div>
<div class="upload-text">Drop a .txt file here or click to browse</div>
<div style="font-size: 0.9em; color: #999; margin-top: 6px;">Handles up to ~8,000 words</div>
<input type="file" id="fileInput" accept=".txt">
</div>
<!-- Controls -->
<div class="controls-row">
<div class="control-group">
<h3>Color Palette</h3>
<div class="palette-grid" id="paletteGrid"></div>
</div>
<div class="control-group">
<h3>Display Options</h3>
<div class="slider-container">
<div class="slider-label">
<span>Word width scale</span>
<span id="scaleVal">5 px/char</span>
</div>
<input type="range" class="slider" id="scaleSlider" min="2" max="14" value="5">
</div>
<div class="slider-container" style="margin-top: 16px;">
<div class="slider-label">
<span>Bar height</span>
<span id="barVal">8 px</span>
</div>
<input type="range" class="slider" id="barSlider" min="3" max="24" value="8">
</div>
<div class="checkbox-container" style="margin-top: 12px;">
<input type="checkbox" id="guideLinesToggle" checked>
<label for="guideLinesToggle">Show guide lines</label>
</div>
<div class="checkbox-container">
<input type="checkbox" id="paraBreaksToggle" checked>
<label for="paraBreaksToggle">Mark paragraph breaks</label>
</div>
</div>
</div>
<!-- Canvas output -->
<div id="canvas-container">
<div id="placeholder-msg">Upload a text file above to generate your Chromacode visualization.</div>
</div>
<div class="output-actions">
<button class="control-btn" id="reRenderBtn" disabled>Re-render</button>
<button class="control-btn" id="downloadBtn" disabled>Download PNG</button>
<span id="wordCount"></span>
</div>
<div class="legend-row" id="legend" style="display:none;">
<div class="legend-item">
<div class="legend-swatch" style="background:#888;"></div>
<span>Each block = one word (width = character count)</span>
</div>
<div class="legend-item">
<div class="legend-swatch" style="background:#888; width:4px;"></div>
<span>Thin gap = line break</span>
</div>
<div class="legend-item">
<div class="legend-swatch" style="background: transparent; border: none; font-size:0.85em;">▮</div>
<span>Tall gap = paragraph break</span>
</div>
</div>
</div>
<script>
// ─── Palettes ────────────────────────────────────────────────────────────
const PALETTES = {
risograph: {
label: 'Risograph',
colors: ['#F4845F', '#F6AE2D', '#C3B1E1', '#2EC4B6', '#E8445A', '#3A5A40'],
bg: '#FFFBF0',
guideLine: '#D8C9A8'
},
signal: {
label: 'Signal',
colors: ['#FF006E', '#3A86FF', '#FFBE0B', '#8338EC', '#06D6A0', '#FB5607'],
bg: '#0D0D14',
guideLine: '#2A2A3A'
},
botanical: {
label: 'Botanical',
colors: ['#2D6A4F', '#52B788', '#8B5E3C', '#95D5B2', '#1B4332', '#D4845A'],
bg: '#F2F5EE',
guideLine: '#C8D8C0'
},
archive: {
label: 'Archive',
colors: ['#6B4C3B', '#A07850', '#C4956A', '#4A5568', '#8B7355', '#3D3228'],
bg: '#F5EDD8',
guideLine: '#DDD0B0'
},
mono: {
label: 'Mono',
colors: ['#1a1a1a', '#4a4a4a', '#808080', '#c41e3a', '#333333', '#aaaaaa'],
bg: '#EBEBEB',
guideLine: '#CCCCCC'
}
};
let currentPaletteKey = 'risograph';
let tokens = [];
let p5Instance = null;
let fileLoaded = false;
// ─── Hash a word to a palette color index ────────────────────────────────
function wordHash(str) {
let h = 0;
for (let i = 0; i < str.length; i++) {
h = (Math.imul(31, h) + str.charCodeAt(i)) | 0;
}
return Math.abs(h);
}
// ─── Process text into tokens ─────────────────────────────────────────────
function processText(text) {
const MAX_WORDS = 8000;
const result = [];
const lines = text.split('\n');
let wordCount = 0;
let truncated = false;
for (let li = 0; li < lines.length; li++) {
const line = lines[li];
if (line.trim() === '') {
result.push({ type: 'para' });
continue;
}
const words = line.match(/\S+/g) || [];
for (const w of words) {
if (wordCount >= MAX_WORDS) { truncated = true; break; }
const clean = w.replace(/[^a-zA-Z']/g, '').toLowerCase() || w.toLowerCase();
result.push({ type: 'word', raw: w, key: clean, len: w.length });
wordCount++;
}
if (truncated) break;
if (li < lines.length - 1) result.push({ type: 'line' });
}
return { tokens: result, wordCount, truncated };
}
// ─── Build / rebuild the p5 sketch ───────────────────────────────────────
function renderSketch() {
if (!fileLoaded || tokens.length === 0) return;
const palette = PALETTES[currentPaletteKey];
const scalePx = parseInt(document.getElementById('scaleSlider').value, 10);
const barH = parseInt(document.getElementById('barSlider').value, 10);
const showGuides = document.getElementById('guideLinesToggle').checked;
const showParaBreaks = document.getElementById('paraBreaksToggle').checked;
const container = document.getElementById('canvas-container');
const placeholder = document.getElementById('placeholder-msg');
if (placeholder) placeholder.style.display = 'none';
// Remove old p5 instance
if (p5Instance) {
p5Instance.remove();
p5Instance = null;
}
const canvasW = Math.max(container.offsetWidth || 700, 400);
const marginX = 24;
const marginY = 20;
const wordGap = 4;
const lineGap = barH + 16; // gap between rows
const paraExtra = barH + 12; // extra for paragraph breaks
// ── Pre-layout to compute total canvas height ──────────────────────
function layout() {
let x = marginX;
let y = marginY;
const maxX = canvasW - marginX;
const rows = [];
let currentRow = { y: y + barH, items: [] };
function nextRow(extra = 0) {
rows.push(currentRow);
y += lineGap + extra;
x = marginX;
currentRow = { y: y + barH, items: [] };
}
for (const tok of tokens) {
if (tok.type === 'para') {
if (showParaBreaks) {
nextRow(paraExtra);
}
continue;
}
if (tok.type === 'line') {
// insert a subtle tick at current x position, then a small gap
currentRow.items.push({ type: 'linemark', x });
x += wordGap * 2;
continue;
}
const ww = Math.max(tok.len * scalePx, 4);
if (x + ww > maxX) {
nextRow();
}
const ci = wordHash(tok.key) % palette.colors.length;
currentRow.items.push({ type: 'word', x, ww, color: palette.colors[ci] });
x += ww + wordGap;
}
rows.push(currentRow);
const totalH = y + lineGap + marginY;
return { rows, totalH };
}
const { rows, totalH } = layout();
const canvasH = Math.min(Math.max(totalH, 200), 6000);
// ── Create p5 sketch ───────────────────────────────────────────────
p5Instance = new p5(function(p) {
p.setup = function() {
const cnv = p.createCanvas(canvasW, canvasH);
cnv.parent('canvas-container');
p.noLoop();
};
p.draw = function() {
p.background(palette.bg);
p.noStroke();
for (const row of rows) {
const ry = row.y;
// Guide line
if (showGuides) {
p.stroke(palette.guideLine);
p.strokeWeight(1);
p.line(marginX, ry + 1, canvasW - marginX, ry + 1);
p.noStroke();
}
for (const item of row.items) {
if (item.type === 'word') {
p.fill(item.color);
p.noStroke();
p.rect(item.x, ry - barH, item.ww, barH);
} else if (item.type === 'linemark') {
// Subtle vertical tick between source lines
p.stroke(palette.guideLine);
p.strokeWeight(1);
p.line(item.x, ry - barH - 3, item.x, ry + 3);
p.noStroke();
}
}
}
};
});
document.getElementById('downloadBtn').disabled = false;
document.getElementById('reRenderBtn').disabled = false;
document.getElementById('legend').style.display = 'flex';
}
// ─── Palette buttons ──────────────────────────────────────────────────────
function buildPaletteUI() {
const grid = document.getElementById('paletteGrid');
grid.innerHTML = '';
for (const [key, pal] of Object.entries(PALETTES)) {
const btn = document.createElement('button');
btn.className = 'palette-btn' + (key === currentPaletteKey ? ' active' : '');
btn.dataset.key = key;
const swatches = document.createElement('div');
swatches.className = 'palette-swatches';
for (const c of pal.colors) {
const s = document.createElement('div');
s.className = 'swatch';
s.style.background = c;
swatches.appendChild(s);
}
btn.appendChild(swatches);
btn.appendChild(document.createTextNode(pal.label));
btn.addEventListener('click', () => {
currentPaletteKey = key;
document.querySelectorAll('.palette-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
if (fileLoaded) renderSketch();
});
grid.appendChild(btn);
}
}
// ─── File upload ──────────────────────────────────────────────────────────
const uploadArea = document.getElementById('uploadArea');
const fileInput = document.getElementById('fileInput');
uploadArea.addEventListener('click', () => fileInput.click());
uploadArea.addEventListener('dragover', e => {
e.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', () => uploadArea.classList.remove('dragover'));
uploadArea.addEventListener('drop', e => {
e.preventDefault();
uploadArea.classList.remove('dragover');
const file = e.dataTransfer.files[0];
if (file) loadFile(file);
});
fileInput.addEventListener('change', () => {
if (fileInput.files[0]) loadFile(fileInput.files[0]);
});
function loadFile(file) {
const reader = new FileReader();
reader.onload = function(e) {
const { tokens: toks, wordCount, truncated } = processText(e.target.result);
tokens = toks;
fileLoaded = true;
const wc = document.getElementById('wordCount');
wc.textContent = truncated
? `Showing first 8,000 of ${wordCount.toLocaleString()}+ words`
: `${wordCount.toLocaleString()} words`;
renderSketch();
};
reader.readAsText(file);
}
// ─── Slider live updates ──────────────────────────────────────────────────
document.getElementById('scaleSlider').addEventListener('input', function() {
document.getElementById('scaleVal').textContent = this.value + ' px/char';
if (fileLoaded) renderSketch();
});
document.getElementById('barSlider').addEventListener('input', function() {
document.getElementById('barVal').textContent = this.value + ' px';
if (fileLoaded) renderSketch();
});
document.getElementById('guideLinesToggle').addEventListener('change', () => {
if (fileLoaded) renderSketch();
});
document.getElementById('paraBreaksToggle').addEventListener('change', () => {
if (fileLoaded) renderSketch();
});
// ─── Action buttons ───────────────────────────────────────────────────────
document.getElementById('reRenderBtn').addEventListener('click', renderSketch);
document.getElementById('downloadBtn').addEventListener('click', () => {
if (p5Instance) p5Instance.saveCanvas('chromacode', 'png');
});
// ─── Init ────────────────────────────────────────────────────────────────
buildPaletteUI();
</script>
</body>
</html>