gpui-material-theme 0.4.2

Material 3 Expressive tokens and themes for GPUI Material
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
use crate::{ColorRole, generated::*};

/// A packed 8-bit sRGB color in alpha, red, green, blue order.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct Argb(u32);

impl Argb {
    /// Creates an [`Argb`] from a packed `0xAARRGGBB` integer.
    pub const fn from_u32(argb: u32) -> Self {
        Self(argb)
    }

    /// Creates an opaque (`alpha = 255`) sRGB color.
    pub const fn from_rgb(red: u8, green: u8, blue: u8) -> Self {
        Self(u32::from_be_bytes([255, red, green, blue]))
    }

    /// Creates an sRGB color from explicit red, green, blue, and alpha channels.
    pub const fn from_rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
        Self(u32::from_be_bytes([alpha, red, green, blue]))
    }

    /// Returns a copy of this color with its alpha channel replaced.
    pub const fn with_alpha(self, alpha: u8) -> Self {
        let [_, red, green, blue] = self.channels();
        Self::from_rgba(red, green, blue, alpha)
    }

    /// Returns the packed `0xAARRGGBB` representation.
    pub const fn to_u32(self) -> u32 {
        self.0
    }

    /// Returns the color channels in `[alpha, red, green, blue]` order.
    pub const fn channels(self) -> [u8; 4] {
        self.0.to_be_bytes()
    }

    /// Returns the 8-bit alpha channel.
    pub const fn alpha(self) -> u8 {
        self.channels()[0]
    }

    /// Returns the 8-bit red channel.
    pub const fn red(self) -> u8 {
        self.channels()[1]
    }

    /// Returns the 8-bit green channel.
    pub const fn green(self) -> u8 {
        self.channels()[2]
    }

    /// Returns the 8-bit blue channel.
    pub const fn blue(self) -> u8 {
        self.channels()[3]
    }
}

impl ColorRole {
    /// Returns the standard Material foreground role paired with this semantic
    /// background role, or `None` when this role is not a paired background.
    pub const fn on_role(self) -> Option<Self> {
        use ColorRole::*;

        Some(match self {
            Background => OnBackground,
            Error => OnError,
            ErrorContainer => OnErrorContainer,
            Info => OnInfo,
            InfoContainer => OnInfoContainer,
            InverseSurface => InverseOnSurface,
            Primary => OnPrimary,
            PrimaryContainer => OnPrimaryContainer,
            PrimaryFixed | PrimaryFixedDim => OnPrimaryFixed,
            Secondary => OnSecondary,
            SecondaryContainer => OnSecondaryContainer,
            SecondaryFixed | SecondaryFixedDim => OnSecondaryFixed,
            Success => OnSuccess,
            SuccessContainer => OnSuccessContainer,
            Surface
            | SurfaceBright
            | SurfaceDim
            | SurfaceContainerLowest
            | SurfaceContainerLow
            | SurfaceContainer
            | SurfaceContainerHigh
            | SurfaceContainerHighest => OnSurface,
            SurfaceVariant => OnSurfaceVariant,
            Tertiary => OnTertiary,
            TertiaryContainer => OnTertiaryContainer,
            TertiaryFixed | TertiaryFixedDim => OnTertiaryFixed,
            Warning => OnWarning,
            WarningContainer => OnWarningContainer,
            _ => return None,
        })
    }
}

/// The selected appearance. The host resolves any system preference.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Appearance {
    /// Light mode (default).
    #[default]
    Light,
    /// Dark mode.
    Dark,
}

/// How an application selects between light, dark, and OS-provided appearance.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AppearanceMode {
    /// Always use light appearance.
    Light,
    /// Always use dark appearance.
    Dark,
    /// Follow the operating system or host window appearance (default).
    #[default]
    System,
}

impl AppearanceMode {
    /// Resolves this mode into a concrete [`Appearance`], falling back to `system`
    /// when `self` is [`Self::System`].
    pub const fn resolve(self, system: Appearance) -> Appearance {
        match self {
            Self::Light => Appearance::Light,
            Self::Dark => Appearance::Dark,
            Self::System => system,
        }
    }
}

impl From<Appearance> for AppearanceMode {
    fn from(appearance: Appearance) -> Self {
        match appearance {
            Appearance::Light => Self::Light,
            Appearance::Dark => Self::Dark,
        }
    }
}

/// A semantic Material [`ColorRole`] or an explicit literal [`Argb`] color.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ColorSpec {
    /// Resolve dynamically against the active [`ColorScheme`].
    Role(ColorRole),
    /// Use an explicit packed sRGB color.
    Custom(Argb),
}

impl ColorSpec {
    /// Resolves this color specification against `scheme`.
    pub const fn resolve(self, scheme: &ColorScheme) -> Argb {
        match self {
            Self::Role(role) => scheme.color(role),
            Self::Custom(argb) => argb,
        }
    }
}

impl From<ColorRole> for ColorSpec {
    fn from(role: ColorRole) -> Self {
        Self::Role(role)
    }
}

impl From<Argb> for ColorSpec {
    fn from(argb: Argb) -> Self {
        Self::Custom(argb)
    }
}

/// Resolved foreground and background colors for a Material pairing.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ColorPair {
    /// Foreground content color (text, icons, indicators).
    pub foreground: Argb,
    /// Background container or surface color.
    pub background: Argb,
}

/// All semantic color roles for one appearance, with per-role overrides.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ColorScheme {
    appearance: Appearance,
    colors: [Argb; ColorRole::ALL.len()],
}

#[cfg(feature = "serde")]
impl serde::Serialize for ColorScheme {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        #[derive(serde::Serialize)]
        #[serde(rename = "ColorScheme")]
        struct SerializedColorScheme {
            appearance: Appearance,
            overrides: std::collections::BTreeMap<ColorRole, Argb>,
        }

        let mut overrides = std::collections::BTreeMap::new();
        for role in ColorRole::ALL {
            let color = self.colors[role as usize];
            if color != self.baseline_color(role) {
                overrides.insert(role, color);
            }
        }
        SerializedColorScheme {
            appearance: self.appearance,
            overrides,
        }
        .serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for ColorScheme {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(serde::Deserialize)]
        #[serde(rename = "ColorScheme")]
        struct SerializedColorScheme {
            appearance: Appearance,
            // Keep the serialized form keyed by role so additions can use baseline defaults.
            #[serde(default)]
            overrides: std::collections::BTreeMap<ColorRole, Argb>,
        }

        let raw = SerializedColorScheme::deserialize(deserializer)?;
        let mut scheme = Self::baseline(raw.appearance);
        for (role, color) in raw.overrides {
            scheme.set_color(role, color);
        }
        Ok(scheme)
    }
}

impl Default for ColorScheme {
    fn default() -> Self {
        Self::baseline(Appearance::Light)
    }
}

impl ColorScheme {
    /// Returns the baseline Material 3 color scheme for the given [`Appearance`].
    pub const fn baseline(appearance: Appearance) -> Self {
        Self {
            appearance,
            colors: match appearance {
                Appearance::Light => LIGHT,
                Appearance::Dark => DARK,
            },
        }
    }

    /// Returns the [`Appearance`] (light or dark) associated with this scheme.
    pub const fn appearance(&self) -> Appearance {
        self.appearance
    }

    /// Resolves a semantic [`ColorRole`] to its packed [`Argb`] value.
    pub const fn color(&self, role: ColorRole) -> Argb {
        self.colors[role as usize]
    }

    /// Overrides one role. Callers overriding a background should also check its foreground.
    pub fn set_color(&mut self, role: ColorRole, color: Argb) {
        self.colors[role as usize] = color;
    }

    /// Fluent counterpart to [`Self::set_color`].
    pub fn with_color(mut self, role: ColorRole, color: Argb) -> Self {
        self.set_color(role, color);
        self
    }

    #[cfg(feature = "serde")]
    const fn baseline_color(&self, role: ColorRole) -> Argb {
        match self.appearance {
            Appearance::Light => LIGHT[role as usize],
            Appearance::Dark => DARK[role as usize],
        }
    }

    /// Resolves the standard foreground for a semantic background.
    pub fn pair(&self, background: ColorRole) -> Option<ColorPair> {
        let foreground = background.on_role()?;
        Some(ColorPair {
            foreground: self.color(foreground),
            background: self.color(background),
        })
    }

    /// Interpolates all resolved sRGB and alpha channels. The target appearance
    /// takes effect at the midpoint. Values outside 0–1 clamp, and NaN uses 0.
    pub fn blend(&self, target: &Self, t: f32) -> Self {
        if t.is_nan() || t <= 0.0 || self == target {
            return self.clone();
        }
        if t >= 1.0 {
            return target.clone();
        }
        let appearance = if t < 0.5 {
            self.appearance
        } else {
            target.appearance
        };
        let colors = std::array::from_fn(|index| {
            let from = self.colors[index].channels();
            let to = target.colors[index].channels();
            let channels = std::array::from_fn(|i| {
                let start = f32::from(from[i]);
                (start + (f32::from(to[i]) - start) * t).round() as u8
            });
            Argb::from_u32(u32::from_be_bytes(channels))
        });
        Self { appearance, colors }
    }

    /// Applies the Compose tonal-elevation tint to the surface role.
    pub fn surface_at_elevation(&self, elevation: Elevation) -> Argb {
        let surface = self.color(ColorRole::Surface);
        let dp = elevation.dp();
        if dp == 0.0 {
            return surface;
        }

        let overlay_alpha = (4.5 * dp.ln_1p() + 2.0) / 100.0;
        let [alpha, r, g, b] = surface.channels();
        let [_, tint_r, tint_g, tint_b] = self.color(ColorRole::SurfaceTint).channels();
        let under_alpha = f32::from(alpha) / 255.0 * (1.0 - overlay_alpha);
        let alpha = overlay_alpha + under_alpha;
        let composite = |under, over| {
            ((f32::from(over) * overlay_alpha + f32::from(under) * under_alpha) / alpha).round()
                as u8
        };
        Argb::from_u32(u32::from_be_bytes([
            (alpha * 255.0).round() as u8,
            composite(r, tint_r),
            composite(g, tint_g),
            composite(b, tint_b),
        ]))
    }

    /// Generates Tonal Spot roles using MCU Spec2021. Contrast is clamped to
    /// -1 through 1. NaN selects the default contrast (0).
    #[cfg(feature = "dynamic-color")]
    pub fn from_seed(seed: Argb, appearance: Appearance, contrast: f64) -> Self {
        Self::from_seed_with(
            seed,
            appearance,
            contrast,
            crate::DynamicVariant::TonalSpot,
            crate::SpecVersion::Spec2021,
        )
    }
}

/// The six Material elevation levels. The renderer supplies the shadow.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Elevation {
    /// Resting elevation (`0 dp`, default).
    #[default]
    Level0,
    /// Low elevation (`1 dp`).
    Level1,
    /// Medium-low elevation (`3 dp`).
    Level2,
    /// Medium elevation (`6 dp`).
    Level3,
    /// Medium-high elevation (`8 dp`).
    Level4,
    /// Highest elevation (`12 dp`).
    Level5,
}

impl Elevation {
    /// Returns the Material elevation height in density-independent pixels (`dp`).
    pub const fn dp(self) -> f32 {
        match self {
            Self::Level0 => MD_SYS_ELEVATION_LEVEL_0,
            Self::Level1 => MD_SYS_ELEVATION_LEVEL_1,
            Self::Level2 => MD_SYS_ELEVATION_LEVEL_2,
            Self::Level3 => MD_SYS_ELEVATION_LEVEL_3,
            Self::Level4 => MD_SYS_ELEVATION_LEVEL_4,
            Self::Level5 => MD_SYS_ELEVATION_LEVEL_5,
        }
    }
}

/// Material state layer opacities.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StateLayer {
    /// Pointer hover state layer.
    Hover,
    /// Keyboard or programmatic focus state layer.
    Focus,
    /// Active press state layer.
    Pressed,
    /// Active drag state layer.
    Dragged,
}

impl StateLayer {
    /// Returns the normalized Material state-layer opacity (`0.0..=1.0`).
    pub const fn opacity(self) -> f32 {
        match self {
            Self::Hover => MD_SYS_STATE_HOVER_STATE_LAYER_OPACITY,
            Self::Focus => MD_SYS_STATE_FOCUS_STATE_LAYER_OPACITY,
            Self::Pressed => MD_SYS_STATE_PRESSED_STATE_LAYER_OPACITY,
            Self::Dragged => MD_SYS_STATE_DRAGGED_STATE_LAYER_OPACITY,
        }
    }
}
/// Returns the contrast ratio for two CIE L* tones, clamped to 0 through 100.
/// Returns `None` if either tone is NaN.
#[cfg(feature = "dynamic-color")]
pub fn contrast_ratio_of_tones(first: f64, second: f64) -> Option<f64> {
    use material_color_utils::contrast::contrast_utils::Contrast;

    if first.is_nan() || second.is_nan() {
        return None;
    }
    Some(Contrast::ratio_of_tones(
        first.clamp(0.0, 100.0),
        second.clamp(0.0, 100.0),
    ))
}