germterm 0.4.0

A lightweight high-performance terminal graphics framework!
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
//! Color types and helpers.
//!
//! This module provides `u32`-packed [`Color`] and [`ColorRgb`] types, as well as
//! gradient support along with sampling and LUT-based linear interpolation.
//!
//!
//! ## Colors
//!
//! - [`Color`] stores RGBA in a single `u32` (`0xRRGGBBAA`).
//! - [`ColorRgb`] stores RGB without alpha (`0xRRGGBB00`).
//!
//! The library is built with alpha blending support as one of it's core features,
//! which is why [`Color`] sees considerably more use compared to [`ColorRgb`].
//!
//! ## Gradients
//!
//! - [`GradientStop`] represents a single stop in a color gradient.
//! - [`ColorGradient`] stores a sequence of stops and can be sampled along
//!   a normalized `0.0..=1.0` range using [`sample_gradient`].
//!
//! ## Interpolation
//!
//! - [`lerp`] allows fast linear interpolation between two [`Color`]s.

use std::sync::Arc;

pub static BLEND_ALPHA_MULT: [[u8; 256]; 256] = {
    let mut lut = [[0u8; 256]; 256];
    let mut ta = 0;
    while ta < 256 {
        let mut ba = 0;
        while ba < 256 {
            // ba * (1 - ta/255) rounded
            let ta_f = ta as f32 / 255.0;
            let ba_f = ba as f32;
            let result = ba_f * (1.0 - ta_f);
            lut[ta as usize][ba as usize] = (result + 0.5) as u8;
            ba += 1;
        }
        ta += 1;
    }
    lut
};

pub static MUL_DIV_255: [[u8; 256]; 256] = {
    let mut lut = [[0u8; 256]; 256];
    let mut a = 0;
    while a < 256 {
        let mut b = 0;
        while b < 256 {
            // (a * b) / 255 rounded
            let result = (a as f32 * b as f32) / 255.0;
            lut[a as usize][b as usize] = (result + 0.5) as u8;
            b += 1;
        }
        a += 1;
    }
    lut
};

pub static RECIPROCAL_255_OVER_X: [u16; 256] = {
    let mut lut = [0u16; 256];
    let mut x = 1;
    while x < 256 {
        let recip = 255.0 / x as f32;
        lut[x] = (recip * 256.0 + 0.5) as u16;
        x += 1;
    }
    lut
};

pub static LERP_LUT_A: [[u8; 256]; 256] = {
    let mut lut: [[u8; 256]; 256] = [[0u8; 256]; 256];
    let mut channel_value: usize = 0;
    while channel_value < 256 {
        let mut t_value: usize = 0;
        while t_value < 256 {
            let scaled: usize = channel_value * (255 - t_value);
            let rounded: usize = scaled + 128;
            let final_value: u8 = (rounded / 255) as u8;
            lut[channel_value][t_value] = final_value;
            t_value += 1;
        }
        channel_value += 1;
    }
    lut
};

pub static LERP_LUT_B: [[u8; 256]; 256] = {
    let mut lut: [[u8; 256]; 256] = [[0u8; 256]; 256];
    let mut channel_value: usize = 0;
    while channel_value < 256 {
        let mut t_value: usize = 0;
        while t_value < 256 {
            let scaled: usize = channel_value * t_value;
            let rounded: usize = scaled + 128;
            let final_value: u8 = (rounded / 255) as u8;
            lut[channel_value][t_value] = final_value;
            t_value += 1;
        }
        channel_value += 1;
    }
    lut
};

/// A packed RGBA color stored in an `u32`.
///
/// Layout: `0xRR_GG_BB_AA`
///
/// # Examples
///
/// ```rust
/// use germterm::color::Color;
///
/// let color = Color::new(255, 0, 0, 255);
/// assert_eq!(color, Color::RED);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Color(pub u32);

impl Color {
    pub const CLEAR: Self = Self(0x00_00_00_00);
    pub const WHITE: Self = Self(0xFF_FF_FF_FF);
    pub const DARK_GRAY: Self = Self(0xA9_A9_A9_FF);
    pub const LIGHT_GRAY: Self = Self(0xD3_D3_D3_FF);
    pub const BLACK: Self = Self(0x00_00_00_FF);
    pub const RED: Self = Self(0xFF_00_00_FF);
    pub const GREEN: Self = Self(0x00_FF_00_FF);
    pub const BLUE: Self = Self(0x00_00_FF_FF);
    pub const YELLOW: Self = Self(0xFF_FF_00_FF);
    pub const CYAN: Self = Self(0x00_FF_FF_FF);
    pub const TEAL: Self = Self(0x00_80_80_FF);
    pub const VIOLET: Self = Self(0x7F_00_FF_FF);
    pub const PINK: Self = Self(0xFF_C0_CB_FF);
    pub const ORANGE: Self = Self(0xFF_A5_00_FF);
    pub const DARK_GREEN: Self = Self(0x08_48_08_FF);

    #[inline]
    pub fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
        Color(((r as u32) << 24) | ((g as u32) << 16) | ((b as u32) << 8) | (a as u32))
    }

    #[inline]
    pub fn r(&self) -> u8 {
        ((self.0 >> 24) & 0xFF) as u8
    }

    #[inline]
    pub fn g(&self) -> u8 {
        ((self.0 >> 16) & 0xFF) as u8
    }

    #[inline]
    pub fn b(&self) -> u8 {
        ((self.0 >> 8) & 0xFF) as u8
    }

    #[inline]
    pub fn a(&self) -> u8 {
        (self.0 & 0xFF) as u8
    }

    #[inline]
    pub fn rgb(&self) -> (u8, u8, u8) {
        (self.r(), self.g(), self.b())
    }

    #[inline]
    pub fn rgba(&self) -> (u8, u8, u8, u8) {
        (self.r(), self.g(), self.b(), self.a())
    }

    #[inline]
    pub fn with_alpha(&self, a: u8) -> Self {
        Color((self.0 & 0xFFFF_FF00) | a as u32)
    }

    #[inline]
    pub fn rgba_f32(&self) -> (f32, f32, f32, f32) {
        let r: f32 = ((self.0 >> 24) & 0xFF) as f32 / 255.0;
        let g: f32 = ((self.0 >> 16) & 0xFF) as f32 / 255.0;
        let b: f32 = ((self.0 >> 8) & 0xFF) as f32 / 255.0;
        let a: f32 = (self.0 & 0xFF) as f32 / 255.0;
        (r, g, b, a)
    }

    #[inline]
    pub fn from_f32(r: f32, g: f32, b: f32, a: f32) -> Self {
        Color::new(
            (r.clamp(0.0, 1.0) * 255.0) as u8,
            (g.clamp(0.0, 1.0) * 255.0) as u8,
            (b.clamp(0.0, 1.0) * 255.0) as u8,
            (a.clamp(0.0, 1.0) * 255.0) as u8,
        )
    }
}

/// A packed RGB color stored in an `u32`.
///
/// Layout: `0xRR_GG_BB_00`
///
/// This struct is intended to be used in cases
/// where the alpha channel is not applicable.
pub struct ColorRgb(u32);

impl ColorRgb {
    pub const WHITE: Self = Self(0xFF_FF_FF_FF);
    pub const DARK_GRAY: Self = Self(0xA9_A9_A9_FF);
    pub const LIGHT_GRAY: Self = Self(0xD3_D3_D3_FF);
    pub const BLACK: Self = Self(0x00_00_00_FF);
    pub const RED: Self = Self(0xFF_00_00_FF);
    pub const GREEN: Self = Self(0x00_FF_00_FF);
    pub const BLUE: Self = Self(0x00_00_FF_FF);
    pub const YELLOW: Self = Self(0xFF_FF_00_FF);
    pub const CYAN: Self = Self(0x00_FF_FF_FF);
    pub const TEAL: Self = Self(0x00_80_80_FF);
    pub const VIOLET: Self = Self(0x7F_00_FF_FF);
    pub const PINK: Self = Self(0xFF_C0_CB_FF);
    pub const ORANGE: Self = Self(0xFF_A5_00_FF);
    pub const DARK_GREEN: Self = Self(0x08_48_08_FF);

    #[inline]
    pub fn new(r: u8, g: u8, b: u8) -> Self {
        ColorRgb(((r as u32) << 24) | ((g as u32) << 16) | ((b as u32) << 8))
    }

    #[inline]
    pub fn r(&self) -> u8 {
        ((self.0 >> 24) & 0xFF) as u8
    }

    #[inline]
    pub fn g(&self) -> u8 {
        ((self.0 >> 16) & 0xFF) as u8
    }

    #[inline]
    pub fn b(&self) -> u8 {
        ((self.0 >> 8) & 0xFF) as u8
    }
}

impl From<ColorRgb> for Color {
    fn from(color: ColorRgb) -> Self {
        Color::new(color.r(), color.g(), color.b(), 255)
    }
}

/// A single stop in a [`ColorGradient`].
///
/// Each stop specifies a position `t` in the normalized range `0.0..=1.0`
/// and a [`Color`] at that position. Gradients are created by interpolating
/// between multiple stops.
#[derive(Clone)]
pub struct GradientStop {
    pub t: f32,
    pub color: Color,
}

impl GradientStop {
    /// Creates a new gradient stop.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use germterm::color::{GradientStop, Color};
    /// let stop = GradientStop::new(0.5, Color::RED);
    /// ```
    pub fn new(t: f32, color: Color) -> Self {
        GradientStop { t, color }
    }
}

/// A simple n-color gradient.
///
/// Stores a sequence of color stops [`GradientStop`] that can be sampled
/// along a normalized range `t` (0.0..=1.0) to produce interpolated colors.
///
/// Construct a `ColorGradient` via [`ColorGradient::new`] and sample colors
/// using [`sample_gradient`] or other helper functions.
///
/// The gradient is internally reference-counted [`Arc`] so it can be
/// cheaply cloned and shared.
#[derive(Clone)]
pub struct ColorGradient {
    pub stops: Arc<Vec<GradientStop>>,
}

impl ColorGradient {
    /// Creates a new color gradient from a vec or slice of [`GradientStop`]s.
    ///
    /// # Panics
    /// - If `stops` is empty.
    ///
    /// # Notes
    /// - `stops` should be in their intended visual order for the gradient to behave as expected.
    /// - When evaluating the gradient, `t` values are expected to be within `0.0..=1.0`.
    pub fn new(stops: Vec<GradientStop>) -> Self {
        assert!(!stops.is_empty(), "Gradient must have at least 1 stop");

        ColorGradient {
            stops: Arc::new(stops),
        }
    }
}

/// Samples a color from a `ColorGradient` at a normalized position `t`.
///
/// `t` should be in the range `0.0..=1.0`. Values outside this range are clamped.
///
/// # Example
///
/// ```rust,no_run
/// # use germterm::color::{ColorGradient, GradientStop, Color, sample_gradient};
/// let gradient = ColorGradient::new(vec![
///     GradientStop::new(0.0, Color::RED),
///     GradientStop::new(1.0, Color::BLUE),
/// ]);
/// let color = sample_gradient(&gradient, 0.75);
/// ```
#[inline]
pub fn sample_gradient(gradient: &ColorGradient, t: f32) -> Color {
    let t = t.clamp(0.0, 1.0);

    if gradient.stops.len() == 1 {
        return gradient.stops[0].color;
    }

    for window in gradient.stops.windows(2) {
        let a = &window[0];
        let b = &window[1];

        if t >= a.t && t <= b.t {
            let local_t = (t - a.t) / (b.t - a.t);
            return lerp(a.color, b.color, local_t);
        }
    }

    gradient.stops.last().unwrap().color
}

/// Linearly interpolates between two [`Color`]s.
///
/// Computes a color between `a` and `b` using the parameter `t`,
/// where `t = 0.0` returns `a` and `t = 1.0` returns `b`.
///
/// Values outside `0.0..=1.0` are clamped to this range.
///
/// # Example
///
/// ```rust,no_run
/// # use germterm::color::{Color, lerp};
/// let purple = lerp(Color::RED, Color::BLUE, 0.5);
/// ```
pub fn lerp(a: Color, b: Color, t: f32) -> Color {
    let clamped_t: f32 = t.clamp(0.0, 1.0);
    let t_scaled: u8 = (clamped_t * 255.0).round() as u8;

    let (a_r, a_g, a_b, a_a) = a.rgba();
    let (b_r, b_g, b_b, b_a) = b.rgba();

    let out_r: u8 =
        LERP_LUT_A[a_r as usize][t_scaled as usize] + LERP_LUT_B[b_r as usize][t_scaled as usize];
    let out_g: u8 =
        LERP_LUT_A[a_g as usize][t_scaled as usize] + LERP_LUT_B[b_g as usize][t_scaled as usize];
    let out_b: u8 =
        LERP_LUT_A[a_b as usize][t_scaled as usize] + LERP_LUT_B[b_b as usize][t_scaled as usize];
    let out_a: u8 =
        LERP_LUT_A[a_a as usize][t_scaled as usize] + LERP_LUT_B[b_a as usize][t_scaled as usize];

    Color::new(out_r, out_g, out_b, out_a)
}

#[inline]
pub(crate) fn blend_source_over(bottom: Color, top: Color) -> Color {
    let (tr, tg, tb, ta) = top.rgba();
    let (br, bg, bb, ba) = bottom.rgba();

    let alpha_mult = BLEND_ALPHA_MULT[ta as usize][ba as usize] as u16;
    let out_a = ta as u16 + alpha_mult;

    if out_a == 0 {
        return Color::CLEAR;
    }

    #[inline]
    fn compute_channel(tc: u8, bc: u8, ta: u8, alpha_mult: u16, out_a: u16) -> u8 {
        // numerator = tc * ta + bc * alpha_mult
        let tc_ta = MUL_DIV_255[tc as usize][ta as usize] as u16 * 255;
        let bc_alpha = MUL_DIV_255[bc as usize][alpha_mult as usize] as u16 * 255;
        let numerator = tc_ta + bc_alpha;

        // out_c = (numerator * 255) / out_a
        let recip = RECIPROCAL_255_OVER_X[out_a as usize] as u32;
        let result = ((numerator as u32 * recip) + (1 << 7)) >> 8;

        (result >> 8) as u8
    }

    let out_r = compute_channel(tr, br, ta, alpha_mult, out_a);
    let out_g = compute_channel(tg, bg, ta, alpha_mult, out_a);
    let out_b = compute_channel(tb, bb, ta, alpha_mult, out_a);

    Color::new(out_r, out_g, out_b, out_a as u8)
}