dotmax 0.1.9

High-performance terminal braille rendering for images, animations, and graphics
Documentation
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
//! Glitch progress bars — torn signals, RGB fringes, static, sync locks.
//!
//! Progress reads as *signal integrity*: bars heal their dropouts, static
//! condenses into clean fill, and rolling pictures lock as the number
//! climbs. Fringe colors are the classic chromatic-aberration pair —
//! magenta and cyan around a white core. Deterministic in `(progress, time)`.

use super::super::draw;
use super::super::{BarContext, ProgressStyle};
use crate::{BrailleGrid, Color, DotmaxError};
use std::f32::consts::TAU;

// ─── deterministic hash ─────────────────────────────────────────────────────

/// Fast integer hash → `[0, 1)`.
#[inline]
fn hash2(x: i32, y: i32) -> f32 {
    let mut h = (x
        .wrapping_mul(374_761_393)
        .wrapping_add(y.wrapping_mul(668_265_263))) as u32;
    h = (h ^ (h >> 13)).wrapping_mul(1_274_126_177);
    ((h ^ (h >> 16)) % 1000) as f32 / 1000.0
}

/// 3-D variant: hash `(x, y, z_int)` for time-slotted flicker.
#[inline]
fn hash3(x: i32, y: i32, z: i32) -> f32 {
    hash2(x ^ z.wrapping_mul(1_234_567), y ^ z.wrapping_mul(7_654_321))
}

// ─── theme colors — chromatic aberration ────────────────────────────────────

/// The clean white signal.
const SIGNAL: Color = Color::rgb(232, 236, 242);
/// Magenta fringe (left/low offset copy).
const FRINGE_MAGENTA: Color = Color::rgb(255, 63, 216);
/// Cyan fringe (right/high offset copy).
const FRINGE_CYAN: Color = Color::rgb(57, 230, 255);
/// Dim scan-noise gray.
const NOISE_GRAY: Color = Color::rgb(110, 116, 128);

/// Applies glitch coloring to every cell the inner style drew: a white core
/// with hash-timed magenta/cyan pops, like a failing video cable.
struct Tinted<S>(S);

impl<S: ProgressStyle> ProgressStyle for Tinted<S> {
    fn name(&self) -> &str {
        self.0.name()
    }
    fn theme(&self) -> &str {
        self.0.theme()
    }
    fn describe(&self) -> &str {
        self.0.describe()
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        self.0.render(grid, ctx)?;
        grid.enable_color_support();
        let (w, h) = grid.dimensions();
        let slot = (ctx.time * 6.0) as i32;
        for y in 0..h {
            for x in 0..w {
                let ch = grid.get_char(x, y);
                if ch != '\u{2800}' && ch != ' ' {
                    let roll = hash3(x as i32, y as i32, slot);
                    let color = if roll > 0.92 {
                        FRINGE_MAGENTA
                    } else if roll > 0.84 {
                        FRINGE_CYAN
                    } else {
                        SIGNAL
                    };
                    let _ = grid.set_cell_color(x, y, color);
                }
            }
        }
        Ok(())
    }
}

/// All styles in the `glitch` theme.
pub fn styles() -> Vec<Box<dyn ProgressStyle>> {
    vec![
        Box::new(Tinted(SignalRestore)),
        Box::new(Tinted(Datamosh)),
        Box::new(RgbSplit),
        Box::new(Tinted(StaticSweep)),
        Box::new(Tinted(Dropout)),
        Box::new(ScanlineRoll),
        Box::new(Tinted(Bitcrush)),
        Box::new(Hexfade),
        Box::new(Tinted(TearFill)),
        Box::new(SyncLock),
    ]
}

/// A bar whose dropout holes heal as the signal is restored.
struct SignalRestore;
impl ProgressStyle for SignalRestore {
    fn name(&self) -> &str {
        "signal-restore"
    }
    fn theme(&self) -> &str {
        "glitch"
    }
    fn describe(&self) -> &str {
        "Dropout holes healing as signal returns"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (w, h) = draw::dot_dims(grid);
        let filled = (ctx.eased * w as f32).round() as usize;
        let holes = 1.0 - ctx.progress;
        let slot = (ctx.time * 10.0) as i32;
        for y in 0..h {
            for x in 0..filled {
                // Holes drift each slot and shrink as progress rises.
                if hash3((x / 3) as i32, (y / 2) as i32, slot) < holes * 0.55 {
                    continue;
                }
                draw::dot(grid, x, y);
            }
        }
        // Faint static beyond the fill.
        for y in 0..h {
            for x in filled..w {
                if hash3(x as i32, y as i32, slot) < 0.03 {
                    draw::dot(grid, x, y);
                }
            }
        }
        Ok(())
    }
}

/// Row bands shear sideways like a moshed keyframe.
struct Datamosh;
impl ProgressStyle for Datamosh {
    fn name(&self) -> &str {
        "datamosh"
    }
    fn theme(&self) -> &str {
        "glitch"
    }
    fn describe(&self) -> &str {
        "Row bands shearing like moshed video"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (w, h) = draw::dot_dims(grid);
        let filled = ctx.eased * w as f32;
        let slot = (ctx.time * 4.0) as i32;
        for y in 0..h {
            let band = (y / 3) as i32;
            let mut shear = (hash3(band, 0, slot) - 0.5) * 6.0;
            // Occasionally a band tears hard.
            if hash3(band, 1, slot) > 0.85 {
                shear *= 3.0;
            }
            let end = (filled + shear).max(0.0) as usize;
            for x in 0..end.min(w) {
                draw::dot(grid, x, y);
            }
        }
        Ok(())
    }
}

/// The bar splits into offset magenta/cyan copies around a white core.
struct RgbSplit;
impl ProgressStyle for RgbSplit {
    fn name(&self) -> &str {
        "rgb-split"
    }
    fn theme(&self) -> &str {
        "glitch"
    }
    fn describe(&self) -> &str {
        "Chromatic-aberration fringes around a white bar"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (w, h) = draw::dot_dims(grid);
        grid.enable_color_support();
        let filled = (ctx.eased * w as f32).round() as usize;
        // Round (not truncate) so the fringe width actually wanders frame to
        // frame instead of parking at its floor for most of the loop.
        let jit = (2.0 + 2.2 * (ctx.time * TAU * 0.75).sin()).max(0.0).round() as usize;
        let slot = (ctx.time * 4.0) as i32;
        let y0 = h / 4;
        let y1 = h.saturating_sub(h / 4);
        // Magenta copy: shifted left/up.
        for y in y0.saturating_sub(1)..y1.saturating_sub(1) {
            for x in 0..filled.saturating_sub(jit) {
                draw::dot(grid, x, y);
                let _ = grid.set_cell_color(x / 2, y / 4, FRINGE_MAGENTA);
            }
        }
        // Cyan copy: shifted right/down.
        for y in (y0 + 1)..(y1 + 1).min(h) {
            for x in jit..(filled + jit).min(w) {
                draw::dot(grid, x, y);
                let _ = grid.set_cell_color(x / 2, y / 4, FRINGE_CYAN);
            }
        }
        // White core drawn last so overlap reads clean; the occasional
        // scanline pair tears sideways for a beat.
        for y in y0..y1 {
            let band = (y / 2) as i32;
            let tear = if hash3(band, 7, slot) < 0.12 {
                ((hash3(band, 9, slot) - 0.5) * 5.0) as i32
            } else {
                0
            };
            for x in 0..filled {
                draw::dot_i(grid, x as i32 + tear, y as i32);
                let _ = grid.set_cell_color(x / 2, y / 4, SIGNAL);
            }
        }
        Ok(())
    }
}

/// A band of television static sweeps ahead of the clean fill.
struct StaticSweep;
impl ProgressStyle for StaticSweep {
    fn name(&self) -> &str {
        "static-sweep"
    }
    fn theme(&self) -> &str {
        "glitch"
    }
    fn describe(&self) -> &str {
        "A static band sweeping ahead of clean fill"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (w, h) = draw::dot_dims(grid);
        let edge = ctx.eased * w as f32;
        let band = 10.0;
        let slot = (ctx.time * 12.0) as i32;
        for y in 0..h {
            for x in 0..w {
                let fx = x as f32;
                if fx < edge {
                    draw::dot(grid, x, y);
                } else if fx < edge + band {
                    // Fresh white noise re-rolled every frame.
                    let falloff = 1.0 - (fx - edge) / band;
                    if hash3(x as i32, y as i32, slot) < 0.55 * falloff {
                        draw::dot(grid, x, y);
                    }
                }
            }
        }
        Ok(())
    }
}

/// Whole scan bands black out for a beat, ghosting a shifted copy.
struct Dropout;
impl ProgressStyle for Dropout {
    fn name(&self) -> &str {
        "dropout"
    }
    fn theme(&self) -> &str {
        "glitch"
    }
    fn describe(&self) -> &str {
        "Scan bands blinking out and ghosting back"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (w, h) = draw::dot_dims(grid);
        let filled = (ctx.eased * w as f32).round() as usize;
        let slot = (ctx.time * 6.0) as i32;
        for y in 0..h {
            let band = (y / 2) as i32;
            let out = hash3(band, 40, slot) < 0.12;
            if out {
                // Ghost: a dim shifted stub where the band went missing.
                let shift = ((hash3(band, 41, slot) - 0.5) * 8.0) as i32;
                for x in (0..filled).step_by(2) {
                    draw::dot_i(grid, x as i32 + shift, y as i32);
                }
            } else {
                for x in 0..filled {
                    draw::dot(grid, x, y);
                }
            }
        }
        Ok(())
    }
}

/// A bright scanline rolls down the bar, bending dots as it passes.
struct ScanlineRoll;
impl ProgressStyle for ScanlineRoll {
    fn name(&self) -> &str {
        "scanline-roll"
    }
    fn theme(&self) -> &str {
        "glitch"
    }
    fn describe(&self) -> &str {
        "A rolling scanline warping the fill"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (w, h) = draw::dot_dims(grid);
        grid.enable_color_support();
        let filled = (ctx.eased * w as f32).round() as usize;
        let scan = ((ctx.time * 0.5).fract() * h as f32) as usize;
        for y in 0..h {
            let near = (y as i32 - scan as i32).abs();
            let warp = if near <= 1 { 2 } else { 0 };
            for x in 0..filled {
                draw::dot(grid, (x + warp).min(w.saturating_sub(1)), y);
            }
            // Interlace shading: alternate cell rows dimmer.
            let color = if near <= 1 {
                SIGNAL
            } else if (y / 2) % 2 == 0 {
                Color::rgb(200, 205, 214)
            } else {
                NOISE_GRAY
            };
            for cx in 0..(filled / 2 + 1).min(grid.dimensions().0) {
                let _ = grid.set_cell_color(cx, y / 4, color);
            }
        }
        Ok(())
    }
}

/// The fill edge snaps to chunky blocks that pop and flicker in.
struct Bitcrush;
impl ProgressStyle for Bitcrush {
    fn name(&self) -> &str {
        "bitcrush"
    }
    fn theme(&self) -> &str {
        "glitch"
    }
    fn describe(&self) -> &str {
        "A fill quantized into popping blocks"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (cw, ch) = grid.dimensions();
        let chunk = 3usize;
        let cols = (cw / chunk).max(1);
        let lit = ctx.eased * cols as f32;
        let slot = (ctx.time * 8.0) as i32;
        for col in 0..cols {
            let fc = col as f32;
            let state = if fc + 1.0 <= lit {
                2 // solid
            } else {
                i32::from(fc < lit) // 1 = popping in, 0 = empty
            };
            if state == 0 {
                continue;
            }
            for y in 0..ch {
                for k in 0..chunk {
                    let x = col * chunk + k;
                    if x >= cw {
                        break;
                    }
                    if state == 2 {
                        draw::glyph(grid, x, y, '');
                    } else if hash3(x as i32, y as i32, slot) < 0.6 {
                        draw::glyph(grid, x, y, '');
                    }
                }
            }
        }
        Ok(())
    }
}

/// A hex readout brightens into place as the dump completes.
struct Hexfade;
impl ProgressStyle for Hexfade {
    fn name(&self) -> &str {
        "hexfade"
    }
    fn theme(&self) -> &str {
        "glitch"
    }
    fn describe(&self) -> &str {
        "A hex dump resolving byte by byte"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        const HEX: [char; 16] = [
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
        ];
        let (cw, ch) = grid.dimensions();
        grid.enable_color_support();
        let total = cw * ch;
        let solved = (ctx.eased * total as f32).round() as usize;
        let slot = (ctx.time * 10.0) as i32;
        for y in 0..ch {
            for x in 0..cw {
                let idx = y * cw + x;
                if (x + 1) % 3 == 0 {
                    continue; // byte spacing
                }
                if idx < solved {
                    let pick = (hash2(x as i32, y as i32) * 16.0) as usize;
                    draw::glyph(grid, x, y, HEX[pick.min(15)]);
                    let _ = grid.set_cell_color(x, y, SIGNAL);
                } else if idx < solved + cw / 2 {
                    // The churn zone: digits still flipping.
                    let pick = (hash3(x as i32, y as i32, slot) * 16.0) as usize;
                    draw::glyph(grid, x, y, HEX[pick.min(15)]);
                    let _ = grid.set_cell_color(x, y, FRINGE_CYAN);
                } else {
                    draw::glyph(grid, x, y, '·');
                    let _ = grid.set_cell_color(x, y, NOISE_GRAY);
                }
            }
        }
        Ok(())
    }
}

/// The fill edge is a jagged, flickering tear.
struct TearFill;
impl ProgressStyle for TearFill {
    fn name(&self) -> &str {
        "tear-fill"
    }
    fn theme(&self) -> &str {
        "glitch"
    }
    fn describe(&self) -> &str {
        "A fill with a jagged flickering tear edge"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (w, h) = draw::dot_dims(grid);
        let filled = ctx.eased * w as f32;
        let slot = (ctx.time * 8.0) as i32;
        for y in 0..h {
            let jag = (hash3(y as i32, 0, slot) - 0.5) * 8.0;
            let end = (filled + jag).max(0.0) as usize;
            for x in 0..end.min(w) {
                draw::dot(grid, x, y);
            }
            // A stray shard past the tear.
            if hash3(y as i32, 1, slot) > 0.8 {
                let sx = (filled + jag + 4.0).max(0.0) as usize;
                for k in 0..3 {
                    draw::dot(grid, (sx + k).min(w.saturating_sub(1)), y);
                }
            }
        }
        Ok(())
    }
}

/// A rolling, noisy picture locks steady as sync is regained.
struct SyncLock;
impl ProgressStyle for SyncLock {
    fn name(&self) -> &str {
        "sync-lock"
    }
    fn theme(&self) -> &str {
        "glitch"
    }
    fn describe(&self) -> &str {
        "A rolling picture locking into sync"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (w, h) = draw::dot_dims(grid);
        grid.enable_color_support();
        let chaos = (1.0 - ctx.progress).powf(1.5);
        // Vertical hold: the whole picture rolls until sync locks.
        let roll = (chaos * (ctx.time * 1.0).fract() * h as f32) as usize;
        let bar_h = (h / 3).max(1);
        let y_top = h / 3;
        let slot = (ctx.time * 12.0) as i32;
        for y in 0..h {
            let src_y = (y + roll) % h;
            let in_bar = src_y >= y_top && src_y < y_top + bar_h;
            for x in 0..w {
                let noise = hash3(x as i32, y as i32, slot) < chaos * 0.35;
                if in_bar != noise {
                    draw::dot(grid, x, y);
                    let color = if noise { NOISE_GRAY } else { SIGNAL };
                    let _ = grid.set_cell_color(x / 2, y / 4, color);
                }
            }
        }
        // Percent readout locks in with the signal.
        if let Some(label) = &ctx.label {
            let (cw, ch_cells) = grid.dimensions();
            let chars: Vec<char> = label.chars().collect();
            if ctx.progress > 0.5 && cw > chars.len() + 1 && ch_cells > 0 {
                let x0 = (cw - chars.len()) / 2;
                let y0 = ch_cells / 2;
                for (i, c) in chars.iter().enumerate() {
                    draw::glyph(grid, x0 + i, y0, *c);
                    let _ = grid.set_cell_color(x0 + i, y0, FRINGE_CYAN);
                }
            }
        }
        Ok(())
    }
}