stipple 0.0.2

Stipple: a cross-platform, self-drawn UI toolkit in Rust.
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! Design tokens and themes for Stipple's self-drawn widgets.
//!
//! Because every control is drawn by Stipple (not the OS), a single [`Theme`]
//! value defines the entire look: a semantic [`Palette`] (roles + interaction
//! states + status colors + overlays), a [`Typography`] scale, a [`Spacing`]
//! scale, and corner [`radius`](Theme::radius). Widgets read tokens from the
//! theme rather than hard-coding values, so re-skinning is swapping the theme —
//! and the [builder methods](Theme::with_accent) make a custom theme a
//! one-liner.

#![forbid(unsafe_code)]

use crate::render::Color;

/// Semantic colors. Names describe *role*, not hue, so light/dark/custom themes
/// can supply different values for the same role.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Palette {
    /// Window/background fill.
    pub background: Color,
    /// Raised surface (cards, panels).
    pub surface: Color,
    /// Primary accent (default button, selection).
    pub primary: Color,
    /// Primary under hover.
    pub primary_hover: Color,
    /// Primary while pressed/active.
    pub primary_active: Color,
    /// Content drawn on top of `primary`.
    pub on_primary: Color,
    /// Default text/icon color on `background`/`surface`.
    pub text: Color,
    /// De-emphasized text.
    pub text_muted: Color,
    /// Hairline borders and dividers.
    pub border: Color,
    /// Positive / success status.
    pub success: Color,
    /// Caution / warning status.
    pub warning: Color,
    /// Destructive / error status.
    pub danger: Color,
    /// Focus-ring color (the App draws it around the focused element).
    pub focus_ring: Color,
    /// Translucent overlay the App composites on a hovered element.
    pub hover_overlay: Color,
    /// Translucent highlight behind selected text.
    pub selection: Color,
}

/// A type scale, in logical pixels.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Typography {
    pub caption: f64,
    pub body: f64,
    pub title: f64,
    pub heading: f64,
}

impl Default for Typography {
    fn default() -> Self {
        Self {
            caption: 12.0,
            body: 14.0,
            title: 18.0,
            heading: 24.0,
        }
    }
}

/// A spacing scale in logical pixels. A small fixed scale keeps layouts
/// rhythmically consistent.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Spacing {
    pub xs: f64,
    pub sm: f64,
    pub md: f64,
    pub lg: f64,
    pub xl: f64,
}

impl Default for Spacing {
    fn default() -> Self {
        Self {
            xs: 4.0,
            sm: 8.0,
            md: 12.0,
            lg: 16.0,
            xl: 24.0,
        }
    }
}

/// A complete look: palette + typography + spacing + corner radius.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Theme {
    pub palette: Palette,
    pub typography: Typography,
    pub spacing: Spacing,
    /// Default corner radius for rounded surfaces, in logical pixels.
    pub radius: f64,
    /// Base font size in logical pixels (mirrors `typography.body`).
    pub font_size: f64,
}

/// Derive the hover/active/ring tints from a `primary` accent.
fn states(primary: Color, dark: bool) -> (Color, Color) {
    if dark {
        (primary.lighten(0.12), primary.darken(0.12))
    } else {
        (primary.darken(0.08), primary.darken(0.18))
    }
}

impl Theme {
    /// The default light theme.
    pub fn light() -> Self {
        let primary = Color::rgb(0x3b, 0x82, 0xf6);
        let (primary_hover, primary_active) = states(primary, false);
        Self {
            palette: Palette {
                background: Color::rgb(0xf7, 0xf7, 0xf8),
                surface: Color::WHITE,
                primary,
                primary_hover,
                primary_active,
                on_primary: Color::WHITE,
                text: Color::rgb(0x1a, 0x1a, 0x1e),
                text_muted: Color::rgb(0x6b, 0x70, 0x80),
                border: Color::rgb(0xe2, 0xe4, 0xe9),
                success: Color::rgb(0x22, 0xa5, 0x5a),
                warning: Color::rgb(0xe0, 0x8a, 0x00),
                danger: Color::rgb(0xdc, 0x3a, 0x3a),
                focus_ring: primary,
                hover_overlay: Color::rgba(0, 0, 0, 18),
                selection: Color::rgba(0x3b, 0x82, 0xf6, 60),
            },
            typography: Typography::default(),
            spacing: Spacing::default(),
            radius: 8.0,
            font_size: 14.0,
        }
    }

    /// The default dark theme.
    pub fn dark() -> Self {
        let primary = Color::rgb(0x60, 0x9c, 0xff);
        let (primary_hover, primary_active) = states(primary, true);
        Self {
            palette: Palette {
                background: Color::rgb(0x14, 0x15, 0x18),
                surface: Color::rgb(0x1e, 0x20, 0x25),
                primary,
                primary_hover,
                primary_active,
                on_primary: Color::rgb(0x0a, 0x0b, 0x0d),
                text: Color::rgb(0xec, 0xee, 0xf2),
                text_muted: Color::rgb(0x9a, 0xa0, 0xae),
                border: Color::rgb(0x2c, 0x2f, 0x36),
                success: Color::rgb(0x35, 0xc7, 0x76),
                warning: Color::rgb(0xf5, 0x9e, 0x0b),
                danger: Color::rgb(0xf2, 0x55, 0x55),
                focus_ring: primary,
                hover_overlay: Color::rgba(255, 255, 255, 28),
                selection: Color::rgba(0x60, 0x9c, 0xff, 80),
            },
            typography: Typography::default(),
            spacing: Spacing::default(),
            radius: 8.0,
            font_size: 14.0,
        }
    }

    /// Recolor the theme around a new `accent`: sets `primary`, derives its
    /// hover/active tints + focus ring, and picks a readable `on_primary`.
    pub fn with_accent(mut self, accent: Color) -> Self {
        let dark = self.palette.background.luminance() < 0.5;
        let (hover, active) = states(accent, dark);
        self.palette.primary = accent;
        self.palette.primary_hover = hover;
        self.palette.primary_active = active;
        self.palette.on_primary = accent.on_color();
        self.palette.focus_ring = accent;
        // Tint the text selection with the accent, keeping the prior opacity.
        let a = self.palette.selection.a;
        self.palette.selection = Color::rgba(accent.r, accent.g, accent.b, a);
        self
    }

    /// Set the default corner radius.
    pub fn with_radius(mut self, radius: f64) -> Self {
        self.radius = radius;
        self
    }

    /// Set the base font size (also updates `typography.body`).
    pub fn with_font_size(mut self, size: f64) -> Self {
        self.font_size = size;
        self.typography.body = size;
        self
    }

    /// A higher-contrast variant: pure black/white text on a near-pure
    /// background, square-ish corners, and bolder borders. Useful for
    /// accessibility-leaning skins.
    pub fn high_contrast(mut self) -> Self {
        let dark = self.palette.background.luminance() < 0.5;
        if dark {
            self.palette.background = Color::BLACK;
            self.palette.surface = Color::rgb(0x10, 0x10, 0x10);
            self.palette.text = Color::WHITE;
            self.palette.border = Color::rgb(0xc0, 0xc0, 0xc0);
        } else {
            self.palette.background = Color::WHITE;
            self.palette.surface = Color::WHITE;
            self.palette.text = Color::BLACK;
            self.palette.border = Color::BLACK;
        }
        self.palette.text_muted = self.palette.text;
        self.radius = 2.0;
        self
    }
}

impl Default for Theme {
    fn default() -> Self {
        Self::light()
    }
}

// ---- Material 3 (Material You) dynamic color --------------------------------
//
// Material 3 derives a whole color scheme from a single seed color via *tonal
// palettes*: for each key color (primary, neutral, neutral-variant, error) a
// ramp of tones 0..100 is generated, and each semantic role picks a fixed tone
// (e.g. light `primary` = tone 40, `onPrimary` = tone 100). A "tone" in M3 is
// defined as **CIELAB L\***, so we build the ramps with real Lab color math; the
// hue/chroma are M3's CAM16 quantities, which we approximate with Lab hue/chroma
// (close for UI seeds, and fully self-consistent). The result maps onto Stipple's
// existing [`Palette`] roles, so every widget themes correctly with no changes.

/// Material 3's default seed (`#6750A4`) — the baseline "Material You" purple.
const M3_SEED: Color = Color::rgb(0x67, 0x50, 0xa4);

fn srgb_to_linear(c: f64) -> f64 {
    if c <= 0.04045 {
        c / 12.92
    } else {
        ((c + 0.055) / 1.055).powf(2.4)
    }
}

fn linear_to_srgb(c: f64) -> f64 {
    if c <= 0.0031308 {
        c * 12.92
    } else {
        1.055 * c.powf(1.0 / 2.4) - 0.055
    }
}

// D65 reference white.
const XN: f64 = 0.95047;
const ZN: f64 = 1.08883;

fn lab_f(t: f64) -> f64 {
    if t > 0.008856 {
        t.cbrt()
    } else {
        7.787 * t + 16.0 / 116.0
    }
}

fn lab_f_inv(t: f64) -> f64 {
    let t3 = t * t * t;
    if t3 > 0.008856 {
        t3
    } else {
        (t - 16.0 / 116.0) / 7.787
    }
}

/// sRGB color → CIELAB `(L*, a*, b*)`.
fn color_to_lab(c: Color) -> (f64, f64, f64) {
    let r = srgb_to_linear(c.r as f64 / 255.0);
    let g = srgb_to_linear(c.g as f64 / 255.0);
    let b = srgb_to_linear(c.b as f64 / 255.0);
    let x = (0.4124 * r + 0.3576 * g + 0.1805 * b) / XN;
    let y = 0.2126 * r + 0.7152 * g + 0.0722 * b;
    let z = (0.0193 * r + 0.1192 * g + 0.9505 * b) / ZN;
    let (fx, fy, fz) = (lab_f(x), lab_f(y), lab_f(z));
    (116.0 * fy - 16.0, 500.0 * (fx - fy), 200.0 * (fy - fz))
}

/// CIELAB `(L*, a*, b*)` → sRGB color, clipping out-of-gamut channels.
fn lab_to_color(l: f64, a: f64, b: f64) -> Color {
    let fy = (l + 16.0) / 116.0;
    let fx = fy + a / 500.0;
    let fz = fy - b / 200.0;
    let x = XN * lab_f_inv(fx);
    let y = lab_f_inv(fy);
    let z = ZN * lab_f_inv(fz);
    let r = 3.2406 * x - 1.5372 * y - 0.4986 * z;
    let g = -0.9689 * x + 1.8758 * y + 0.0415 * z;
    let bl = 0.0557 * x - 0.2040 * y + 1.0570 * z;
    let to8 = |v: f64| (linear_to_srgb(v.clamp(0.0, 1.0)) * 255.0).round() as u8;
    Color::rgb(to8(r), to8(g), to8(bl))
}

/// A Material 3 tonal palette: a fixed hue + chroma, sampled at any tone (L\*).
#[derive(Clone, Copy)]
struct Tonal {
    hue: f64,
    chroma: f64,
}

impl Tonal {
    /// Take the hue from `seed`, with chroma at least `min_chroma` (M3 floors the
    /// key palettes' chroma so a desaturated seed still yields a usable accent).
    fn from_seed(seed: Color, min_chroma: f64) -> Self {
        let (_l, a, b) = color_to_lab(seed);
        Self {
            hue: b.atan2(a),
            chroma: (a * a + b * b).sqrt().max(min_chroma),
        }
    }

    /// `seed`'s hue at a fixed (usually low) chroma — for the neutral ramps.
    fn neutral_of(seed: Color, chroma: f64) -> Self {
        let (_l, a, b) = color_to_lab(seed);
        Self {
            hue: b.atan2(a),
            chroma,
        }
    }

    /// A fixed hue (degrees) + chroma — for the error/success/warning ramps that
    /// don't follow the seed.
    fn fixed(hue_deg: f64, chroma: f64) -> Self {
        Self {
            hue: hue_deg.to_radians(),
            chroma,
        }
    }

    /// The color at tone `t` (CIELAB L\*, 0..=100).
    fn tone(&self, t: f64) -> Color {
        lab_to_color(
            t,
            self.chroma * self.hue.cos(),
            self.chroma * self.hue.sin(),
        )
    }
}

impl Theme {
    /// A **Material 3** light theme from the default "Material You" seed.
    pub fn material3_light() -> Self {
        Self::material3_from_seed(M3_SEED, false)
    }

    /// A **Material 3** dark theme from the default "Material You" seed.
    pub fn material3_dark() -> Self {
        Self::material3_from_seed(M3_SEED, true)
    }

    /// Build a **Material 3 dynamic-color** theme from any `seed`, light or
    /// `dark`. Generates tonal palettes (primary, neutral, neutral-variant, plus
    /// fixed error/success/warning) and maps M3's role tones onto Stipple's
    /// [`Palette`] — so a single brand color reskins the whole UI, the way
    /// "Material You" recolors from the wallpaper. Tones are CIELAB L\* (M3's
    /// definition); hue/chroma approximate M3's CAM16 quantities.
    pub fn material3_from_seed(seed: Color, dark: bool) -> Self {
        let primary = Tonal::from_seed(seed, 48.0);
        let neutral = Tonal::neutral_of(seed, 4.0);
        let nv = Tonal::neutral_of(seed, 8.0);
        let error = Tonal::fixed(25.0, 84.0);
        let success = Tonal::fixed(145.0, 50.0);
        let warning = Tonal::fixed(75.0, 80.0);

        // Role tones per the M3 spec, light vs dark.
        let p = if dark {
            // (primary, on_primary, background, surface, text, muted, border,
            //  danger, success, warning)
            Palette {
                background: neutral.tone(6.0),
                surface: neutral.tone(17.0),
                primary: primary.tone(80.0),
                primary_hover: states(primary.tone(80.0), true).0,
                primary_active: states(primary.tone(80.0), true).1,
                on_primary: primary.tone(20.0),
                text: neutral.tone(90.0),
                text_muted: nv.tone(80.0),
                border: nv.tone(30.0),
                success: success.tone(70.0),
                warning: warning.tone(75.0),
                danger: error.tone(80.0),
                focus_ring: primary.tone(80.0),
                hover_overlay: with_alpha(neutral.tone(90.0), 22),
                selection: with_alpha(primary.tone(80.0), 80),
            }
        } else {
            Palette {
                background: neutral.tone(96.0),
                surface: neutral.tone(100.0),
                primary: primary.tone(40.0),
                primary_hover: states(primary.tone(40.0), false).0,
                primary_active: states(primary.tone(40.0), false).1,
                on_primary: primary.tone(100.0),
                text: neutral.tone(10.0),
                text_muted: nv.tone(30.0),
                border: nv.tone(80.0),
                success: success.tone(40.0),
                warning: warning.tone(45.0),
                danger: error.tone(40.0),
                focus_ring: primary.tone(40.0),
                hover_overlay: with_alpha(neutral.tone(10.0), 20),
                selection: with_alpha(primary.tone(40.0), 60),
            }
        };
        Self {
            palette: p,
            // M3 type scale (sp): body 14, title 16, headline 22, label 12.
            typography: Typography {
                caption: 12.0,
                body: 14.0,
                title: 16.0,
                heading: 22.0,
            },
            spacing: Spacing::default(),
            // M3 medium components (cards) use a 12dp corner radius.
            radius: 12.0,
            font_size: 14.0,
        }
    }
}

/// A copy of `c` with its alpha replaced (Material 3 state layers + selection).
fn with_alpha(c: Color, a: u8) -> Color {
    Color::rgba(c.r, c.g, c.b, a)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn themes_differ_in_palette_not_metrics() {
        let (l, d) = (Theme::light(), Theme::dark());
        assert_ne!(l.palette.background, d.palette.background);
        assert_eq!(l.spacing, d.spacing);
        assert_eq!(l.radius, d.radius);
    }

    #[test]
    fn with_accent_recolors_and_picks_on_color() {
        let t = Theme::light().with_accent(Color::rgb(0xff, 0xd0, 0x00)); // bright yellow
        assert_eq!(t.palette.primary, Color::rgb(0xff, 0xd0, 0x00));
        assert_eq!(t.palette.focus_ring, t.palette.primary);
        // On a bright accent, content should be dark.
        assert_eq!(t.palette.on_primary, Color::BLACK);
        // Hover/active are derived (different from the base).
        assert_ne!(t.palette.primary_hover, t.palette.primary);
    }

    #[test]
    fn high_contrast_maximizes_text_contrast() {
        let t = Theme::light().high_contrast();
        assert_eq!(t.palette.text, Color::BLACK);
        assert_eq!(t.palette.background, Color::WHITE);
    }

    #[test]
    fn lab_tone_ramp_spans_black_to_white() {
        // A tonal palette's extremes are near-black and near-white, and tones
        // increase monotonically in luminance.
        let t = Tonal::from_seed(M3_SEED, 48.0);
        assert!(t.tone(0.0).luminance() < 0.02, "tone 0 ~ black");
        assert!(t.tone(100.0).luminance() > 0.95, "tone 100 ~ white");
        assert!(t.tone(40.0).luminance() < t.tone(80.0).luminance());
        // A round-trip through Lab is near-identity (within rounding).
        let c = Color::rgb(0x67, 0x50, 0xa4);
        let (l, a, b) = color_to_lab(c);
        let back = lab_to_color(l, a, b);
        assert!((back.r as i32 - c.r as i32).abs() <= 1);
        assert!((back.g as i32 - c.g as i32).abs() <= 1);
        assert!((back.b as i32 - c.b as i32).abs() <= 1);
    }

    #[test]
    fn material3_light_and_dark_are_distinct_and_readable() {
        let l = Theme::material3_light();
        let d = Theme::material3_dark();
        // Light has a bright background, dark a deep one.
        assert!(l.palette.background.luminance() > 0.8);
        assert!(d.palette.background.luminance() < 0.1);
        // M3 shape + type scale.
        assert_eq!(l.radius, 12.0);
        assert_eq!(l.typography.heading, 22.0);
        // The primary is a recognizable Material You purple (blue-dominant, low
        // green) in both modes.
        for t in [l, d] {
            let p = t.palette.primary;
            assert!(p.b > p.g, "primary is purple-leaning (b>g)");
        }
        // on_primary must contrast strongly with primary (a filled button reads).
        let contrast = (l.palette.primary.luminance() - l.palette.on_primary.luminance()).abs();
        assert!(contrast > 0.4, "onPrimary contrasts with primary");
    }

    #[test]
    fn material3_from_seed_recolors_the_whole_scheme() {
        // A red seed yields a red-dominant primary; a green seed a green one.
        let red = Theme::material3_from_seed(Color::rgb(0xb0, 0x00, 0x20), false);
        assert!(red.palette.primary.r > red.palette.primary.b);
        assert!(red.palette.primary.r > red.palette.primary.g);
        let green = Theme::material3_from_seed(Color::rgb(0x00, 0x80, 0x40), false);
        assert!(green.palette.primary.g > green.palette.primary.r);
        // The neutral background even picks up a faint tint of the seed hue.
        assert_ne!(red.palette.background, green.palette.background);
    }
}