iced_style/theme/
palette.rs

1//! Define the colors of a theme.
2use crate::core::{color, Color};
3
4use once_cell::sync::Lazy;
5use palette::color_difference::Wcag21RelativeContrast;
6use palette::rgb::Rgb;
7use palette::{FromColor, Hsl, Mix};
8
9/// A color palette.
10#[derive(Debug, Clone, Copy, PartialEq)]
11pub struct Palette {
12    /// The background [`Color`] of the [`Palette`].
13    pub background: Color,
14    /// The text [`Color`] of the [`Palette`].
15    pub text: Color,
16    /// The primary [`Color`] of the [`Palette`].
17    pub primary: Color,
18    /// The success [`Color`] of the [`Palette`].
19    pub success: Color,
20    /// The danger [`Color`] of the [`Palette`].
21    pub danger: Color,
22}
23
24impl Palette {
25    /// The built-in light variant of a [`Palette`].
26    pub const LIGHT: Self = Self {
27        background: Color::WHITE,
28        text: Color::BLACK,
29        primary: Color::from_rgb(
30            0x5E as f32 / 255.0,
31            0x7C as f32 / 255.0,
32            0xE2 as f32 / 255.0,
33        ),
34        success: Color::from_rgb(
35            0x12 as f32 / 255.0,
36            0x66 as f32 / 255.0,
37            0x4F as f32 / 255.0,
38        ),
39        danger: Color::from_rgb(
40            0xC3 as f32 / 255.0,
41            0x42 as f32 / 255.0,
42            0x3F as f32 / 255.0,
43        ),
44    };
45
46    /// The built-in dark variant of a [`Palette`].
47    pub const DARK: Self = Self {
48        background: Color::from_rgb(
49            0x20 as f32 / 255.0,
50            0x22 as f32 / 255.0,
51            0x25 as f32 / 255.0,
52        ),
53        text: Color::from_rgb(0.90, 0.90, 0.90),
54        primary: Color::from_rgb(
55            0x5E as f32 / 255.0,
56            0x7C as f32 / 255.0,
57            0xE2 as f32 / 255.0,
58        ),
59        success: Color::from_rgb(
60            0x12 as f32 / 255.0,
61            0x66 as f32 / 255.0,
62            0x4F as f32 / 255.0,
63        ),
64        danger: Color::from_rgb(
65            0xC3 as f32 / 255.0,
66            0x42 as f32 / 255.0,
67            0x3F as f32 / 255.0,
68        ),
69    };
70
71    /// The built-in [Dracula] variant of a [`Palette`].
72    ///
73    /// [Dracula]: https://draculatheme.com
74    pub const DRACULA: Self = Self {
75        background: color!(0x282A36), // BACKGROUND
76        text: color!(0xf8f8f2),       // FOREGROUND
77        primary: color!(0xbd93f9),    // PURPLE
78        success: color!(0x50fa7b),    // GREEN
79        danger: color!(0xff5555),     // RED
80    };
81
82    /// The built-in [Nord] variant of a [`Palette`].
83    ///
84    /// [Nord]: https://www.nordtheme.com/docs/colors-and-palettes
85    pub const NORD: Self = Self {
86        background: color!(0x2e3440), // nord0
87        text: color!(0xeceff4),       // nord6
88        primary: color!(0x8fbcbb),    // nord7
89        success: color!(0xa3be8c),    // nord14
90        danger: color!(0xbf616a),     // nord11
91    };
92
93    /// The built-in [Solarized] Light variant of a [`Palette`].
94    ///
95    /// [Solarized]: https://ethanschoonover.com/solarized
96    pub const SOLARIZED_LIGHT: Self = Self {
97        background: color!(0xfdf6e3), // base3
98        text: color!(0x657b83),       // base00
99        primary: color!(0x2aa198),    // cyan
100        success: color!(0x859900),    // green
101        danger: color!(0xdc322f),     // red
102    };
103
104    /// The built-in [Solarized] Dark variant of a [`Palette`].
105    ///
106    /// [Solarized]: https://ethanschoonover.com/solarized
107    pub const SOLARIZED_DARK: Self = Self {
108        background: color!(0x002b36), // base03
109        text: color!(0x839496),       // base0
110        primary: color!(0x2aa198),    // cyan
111        success: color!(0x859900),    // green
112        danger: color!(0xdc322f),     // red
113    };
114
115    /// The built-in [Gruvbox] Light variant of a [`Palette`].
116    ///
117    /// [Gruvbox]: https://github.com/morhetz/gruvbox
118    pub const GRUVBOX_LIGHT: Self = Self {
119        background: color!(0xfbf1c7), // light BG_0
120        text: color!(0x282828),       // light FG0_29
121        primary: color!(0x458588),    // light BLUE_4
122        success: color!(0x98971a),    // light GREEN_2
123        danger: color!(0xcc241d),     // light RED_1
124    };
125
126    /// The built-in [Gruvbox] Dark variant of a [`Palette`].
127    ///
128    /// [Gruvbox]: https://github.com/morhetz/gruvbox
129    pub const GRUVBOX_DARK: Self = Self {
130        background: color!(0x282828), // dark BG_0
131        text: color!(0xfbf1c7),       // dark FG0_29
132        primary: color!(0x458588),    // dark BLUE_4
133        success: color!(0x98971a),    // dark GREEN_2
134        danger: color!(0xcc241d),     // dark RED_1
135    };
136
137    /// The built-in [Catppuccin] Latte variant of a [`Palette`].
138    ///
139    /// [Catppuccin]: https://github.com/catppuccin/catppuccin
140    pub const CATPPUCCIN_LATTE: Self = Self {
141        background: color!(0xeff1f5), // Base
142        text: color!(0x4c4f69),       // Text
143        primary: color!(0x1e66f5),    // Blue
144        success: color!(0x40a02b),    // Green
145        danger: color!(0xd20f39),     // Red
146    };
147
148    /// The built-in [Catppuccin] Frappé variant of a [`Palette`].
149    ///
150    /// [Catppuccin]: https://github.com/catppuccin/catppuccin
151    pub const CATPPUCCIN_FRAPPE: Self = Self {
152        background: color!(0x303446), // Base
153        text: color!(0xc6d0f5),       // Text
154        primary: color!(0x8caaee),    // Blue
155        success: color!(0xa6d189),    // Green
156        danger: color!(0xe78284),     // Red
157    };
158
159    /// The built-in [Catppuccin] Macchiato variant of a [`Palette`].
160    ///
161    /// [Catppuccin]: https://github.com/catppuccin/catppuccin
162    pub const CATPPUCCIN_MACCHIATO: Self = Self {
163        background: color!(0x24273a), // Base
164        text: color!(0xcad3f5),       // Text
165        primary: color!(0x8aadf4),    // Blue
166        success: color!(0xa6da95),    // Green
167        danger: color!(0xed8796),     // Red
168    };
169
170    /// The built-in [Catppuccin] Mocha variant of a [`Palette`].
171    ///
172    /// [Catppuccin]: https://github.com/catppuccin/catppuccin
173    pub const CATPPUCCIN_MOCHA: Self = Self {
174        background: color!(0x1e1e2e), // Base
175        text: color!(0xcdd6f4),       // Text
176        primary: color!(0x89b4fa),    // Blue
177        success: color!(0xa6e3a1),    // Green
178        danger: color!(0xf38ba8),     // Red
179    };
180
181    /// The built-in [Tokyo Night] variant of a [`Palette`].
182    ///
183    /// [Tokyo Night]: https://github.com/enkia/tokyo-night-vscode-theme
184    pub const TOKYO_NIGHT: Self = Self {
185        background: color!(0x1a1b26), // Background (Night)
186        text: color!(0x9aa5ce),       // Text
187        primary: color!(0x2ac3de),    // Blue
188        success: color!(0x9ece6a),    // Green
189        danger: color!(0xf7768e),     // Red
190    };
191
192    /// The built-in [Tokyo Night] Storm variant of a [`Palette`].
193    ///
194    /// [Tokyo Night]: https://github.com/enkia/tokyo-night-vscode-theme
195    pub const TOKYO_NIGHT_STORM: Self = Self {
196        background: color!(0x24283b), // Background (Storm)
197        text: color!(0x9aa5ce),       // Text
198        primary: color!(0x2ac3de),    // Blue
199        success: color!(0x9ece6a),    // Green
200        danger: color!(0xf7768e),     // Red
201    };
202
203    /// The built-in [Tokyo Night] Light variant of a [`Palette`].
204    ///
205    /// [Tokyo Night]: https://github.com/enkia/tokyo-night-vscode-theme
206    pub const TOKYO_NIGHT_LIGHT: Self = Self {
207        background: color!(0xd5d6db), // Background
208        text: color!(0x565a6e),       // Text
209        primary: color!(0x166775),    // Blue
210        success: color!(0x485e30),    // Green
211        danger: color!(0x8c4351),     // Red
212    };
213
214    /// The built-in [Kanagawa] Wave variant of a [`Palette`].
215    ///
216    /// [Kanagawa]: https://github.com/rebelot/kanagawa.nvim
217    pub const KANAGAWA_WAVE: Self = Self {
218        background: color!(0x363646), // Sumi Ink 3
219        text: color!(0xCD7BA),        // Fuji White
220        primary: color!(0x2D4F67),    // Wave Blue 2
221        success: color!(0x76946A),    // Autumn Green
222        danger: color!(0xC34043),     // Autumn Red
223    };
224
225    /// The built-in [Kanagawa] Dragon variant of a [`Palette`].
226    ///
227    /// [Kanagawa]: https://github.com/rebelot/kanagawa.nvim
228    pub const KANAGAWA_DRAGON: Self = Self {
229        background: color!(0x181616), // Dragon Black 3
230        text: color!(0xc5c9c5),       // Dragon White
231        primary: color!(0x223249),    // Wave Blue 1
232        success: color!(0x8a9a7b),    // Dragon Green 2
233        danger: color!(0xc4746e),     // Dragon Red
234    };
235
236    /// The built-in [Kanagawa] Lotus variant of a [`Palette`].
237    ///
238    /// [Kanagawa]: https://github.com/rebelot/kanagawa.nvim
239    pub const KANAGAWA_LOTUS: Self = Self {
240        background: color!(0xf2ecbc), // Lotus White 3
241        text: color!(0x545464),       // Lotus Ink 1
242        primary: color!(0xc9cbd1),    // Lotus Violet 3
243        success: color!(0x6f894e),    // Lotus Green
244        danger: color!(0xc84053),     // Lotus Red
245    };
246
247    /// The built-in [Moonfly] variant of a [`Palette`].
248    ///
249    /// [Moonfly]: https://github.com/bluz71/vim-moonfly-colors
250    pub const MOONFLY: Self = Self {
251        background: color!(0x080808), // Background
252        text: color!(0xbdbdbd),       // Foreground
253        primary: color!(0x80a0ff),    // Blue (normal)
254        success: color!(0x8cc85f),    // Green (normal)
255        danger: color!(0xff5454),     // Red (normal)
256    };
257
258    /// The built-in [Nightfly] variant of a [`Palette`].
259    ///
260    /// [Nightfly]: https://github.com/bluz71/vim-nightfly-colors
261    pub const NIGHTFLY: Self = Self {
262        background: color!(0x011627), // Background
263        text: color!(0xbdc1c6),       // Foreground
264        primary: color!(0x82aaff),    // Blue (normal)
265        success: color!(0xa1cd5e),    // Green (normal)
266        danger: color!(0xfc514e),     // Red (normal)
267    };
268
269    /// The built-in [Oxocarbon] variant of a [`Palette`].
270    ///
271    /// [Oxocarbon]: https://github.com/nyoom-engineering/oxocarbon.nvim
272    pub const OXOCARBON: Self = Self {
273        background: color!(0x232323),
274        text: color!(0xd0d0d0),
275        primary: color!(0x00b4ff),
276        success: color!(0x00c15a),
277        danger: color!(0xf62d0f),
278    };
279}
280
281/// An extended set of colors generated from a [`Palette`].
282#[derive(Debug, Clone, Copy, PartialEq)]
283pub struct Extended {
284    /// The set of background colors.
285    pub background: Background,
286    /// The set of primary colors.
287    pub primary: Primary,
288    /// The set of secondary colors.
289    pub secondary: Secondary,
290    /// The set of success colors.
291    pub success: Success,
292    /// The set of danger colors.
293    pub danger: Danger,
294    /// Whether the palette is dark or not.
295    pub is_dark: bool,
296}
297
298/// The built-in light variant of an [`Extended`] palette.
299pub static EXTENDED_LIGHT: Lazy<Extended> =
300    Lazy::new(|| Extended::generate(Palette::LIGHT));
301
302/// The built-in dark variant of an [`Extended`] palette.
303pub static EXTENDED_DARK: Lazy<Extended> =
304    Lazy::new(|| Extended::generate(Palette::DARK));
305
306/// The built-in Dracula variant of an [`Extended`] palette.
307pub static EXTENDED_DRACULA: Lazy<Extended> =
308    Lazy::new(|| Extended::generate(Palette::DRACULA));
309
310/// The built-in Nord variant of an [`Extended`] palette.
311pub static EXTENDED_NORD: Lazy<Extended> =
312    Lazy::new(|| Extended::generate(Palette::NORD));
313
314/// The built-in Solarized Light variant of an [`Extended`] palette.
315pub static EXTENDED_SOLARIZED_LIGHT: Lazy<Extended> =
316    Lazy::new(|| Extended::generate(Palette::SOLARIZED_LIGHT));
317
318/// The built-in Solarized Dark variant of an [`Extended`] palette.
319pub static EXTENDED_SOLARIZED_DARK: Lazy<Extended> =
320    Lazy::new(|| Extended::generate(Palette::SOLARIZED_DARK));
321
322/// The built-in Gruvbox Light variant of an [`Extended`] palette.
323pub static EXTENDED_GRUVBOX_LIGHT: Lazy<Extended> =
324    Lazy::new(|| Extended::generate(Palette::GRUVBOX_LIGHT));
325
326/// The built-in Gruvbox Dark variant of an [`Extended`] palette.
327pub static EXTENDED_GRUVBOX_DARK: Lazy<Extended> =
328    Lazy::new(|| Extended::generate(Palette::GRUVBOX_DARK));
329
330/// The built-in Catppuccin Latte variant of an [`Extended`] palette.
331pub static EXTENDED_CATPPUCCIN_LATTE: Lazy<Extended> =
332    Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_LATTE));
333
334/// The built-in Catppuccin Frappé variant of an [`Extended`] palette.
335pub static EXTENDED_CATPPUCCIN_FRAPPE: Lazy<Extended> =
336    Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_FRAPPE));
337
338/// The built-in Catppuccin Macchiato variant of an [`Extended`] palette.
339pub static EXTENDED_CATPPUCCIN_MACCHIATO: Lazy<Extended> =
340    Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_MACCHIATO));
341
342/// The built-in Catppuccin Mocha variant of an [`Extended`] palette.
343pub static EXTENDED_CATPPUCCIN_MOCHA: Lazy<Extended> =
344    Lazy::new(|| Extended::generate(Palette::CATPPUCCIN_MOCHA));
345
346/// The built-in Tokyo Night variant of an [`Extended`] palette.
347pub static EXTENDED_TOKYO_NIGHT: Lazy<Extended> =
348    Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT));
349
350/// The built-in Tokyo Night Storm variant of an [`Extended`] palette.
351pub static EXTENDED_TOKYO_NIGHT_STORM: Lazy<Extended> =
352    Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT_STORM));
353
354/// The built-in Tokyo Night variant of an [`Extended`] palette.
355pub static EXTENDED_TOKYO_NIGHT_LIGHT: Lazy<Extended> =
356    Lazy::new(|| Extended::generate(Palette::TOKYO_NIGHT_LIGHT));
357
358/// The built-in Kanagawa Wave variant of an [`Extended`] palette.
359pub static EXTENDED_KANAGAWA_WAVE: Lazy<Extended> =
360    Lazy::new(|| Extended::generate(Palette::KANAGAWA_WAVE));
361
362/// The built-in Kanagawa Dragon variant of an [`Extended`] palette.
363pub static EXTENDED_KANAGAWA_DRAGON: Lazy<Extended> =
364    Lazy::new(|| Extended::generate(Palette::KANAGAWA_DRAGON));
365
366/// The built-in Kanagawa Lotus variant of an [`Extended`] palette.
367pub static EXTENDED_KANAGAWA_LOTUS: Lazy<Extended> =
368    Lazy::new(|| Extended::generate(Palette::KANAGAWA_LOTUS));
369
370/// The built-in Moonfly variant of an [`Extended`] palette.
371pub static EXTENDED_MOONFLY: Lazy<Extended> =
372    Lazy::new(|| Extended::generate(Palette::MOONFLY));
373
374/// The built-in Nightfly variant of an [`Extended`] palette.
375pub static EXTENDED_NIGHTFLY: Lazy<Extended> =
376    Lazy::new(|| Extended::generate(Palette::NIGHTFLY));
377
378/// The built-in Oxocarbon variant of an [`Extended`] palette.
379pub static EXTENDED_OXOCARBON: Lazy<Extended> =
380    Lazy::new(|| Extended::generate(Palette::OXOCARBON));
381
382impl Extended {
383    /// Generates an [`Extended`] palette from a simple [`Palette`].
384    pub fn generate(palette: Palette) -> Self {
385        Self {
386            background: Background::new(palette.background, palette.text),
387            primary: Primary::generate(
388                palette.primary,
389                palette.background,
390                palette.text,
391            ),
392            secondary: Secondary::generate(palette.background, palette.text),
393            success: Success::generate(
394                palette.success,
395                palette.background,
396                palette.text,
397            ),
398            danger: Danger::generate(
399                palette.danger,
400                palette.background,
401                palette.text,
402            ),
403            is_dark: is_dark(palette.background),
404        }
405    }
406}
407
408/// A pair of background and text colors.
409#[derive(Debug, Clone, Copy, PartialEq)]
410pub struct Pair {
411    /// The background color.
412    pub color: Color,
413
414    /// The text color.
415    ///
416    /// It's guaranteed to be readable on top of the background [`color`].
417    ///
418    /// [`color`]: Self::color
419    pub text: Color,
420}
421
422impl Pair {
423    /// Creates a new [`Pair`] from a background [`Color`] and some text [`Color`].
424    pub fn new(color: Color, text: Color) -> Self {
425        Self {
426            color,
427            text: readable(color, text),
428        }
429    }
430}
431
432/// A set of background colors.
433#[derive(Debug, Clone, Copy, PartialEq)]
434pub struct Background {
435    /// The base background color.
436    pub base: Pair,
437    /// A weaker version of the base background color.
438    pub weak: Pair,
439    /// A stronger version of the base background color.
440    pub strong: Pair,
441}
442
443impl Background {
444    /// Generates a set of [`Background`] colors from the base and text colors.
445    pub fn new(base: Color, text: Color) -> Self {
446        let weak = mix(base, text, 0.15);
447        let strong = mix(base, text, 0.40);
448
449        Self {
450            base: Pair::new(base, text),
451            weak: Pair::new(weak, text),
452            strong: Pair::new(strong, text),
453        }
454    }
455}
456
457/// A set of primary colors.
458#[derive(Debug, Clone, Copy, PartialEq)]
459pub struct Primary {
460    /// The base primary color.
461    pub base: Pair,
462    /// A weaker version of the base primary color.
463    pub weak: Pair,
464    /// A stronger version of the base primary color.
465    pub strong: Pair,
466}
467
468impl Primary {
469    /// Generates a set of [`Primary`] colors from the base, background, and text colors.
470    pub fn generate(base: Color, background: Color, text: Color) -> Self {
471        let weak = mix(base, background, 0.4);
472        let strong = deviate(base, 0.1);
473
474        Self {
475            base: Pair::new(base, text),
476            weak: Pair::new(weak, text),
477            strong: Pair::new(strong, text),
478        }
479    }
480}
481
482/// A set of secondary colors.
483#[derive(Debug, Clone, Copy, PartialEq)]
484pub struct Secondary {
485    /// The base secondary color.
486    pub base: Pair,
487    /// A weaker version of the base secondary color.
488    pub weak: Pair,
489    /// A stronger version of the base secondary color.
490    pub strong: Pair,
491}
492
493impl Secondary {
494    /// Generates a set of [`Secondary`] colors from the base and text colors.
495    pub fn generate(base: Color, text: Color) -> Self {
496        let base = mix(base, text, 0.2);
497        let weak = mix(base, text, 0.1);
498        let strong = mix(base, text, 0.3);
499
500        Self {
501            base: Pair::new(base, text),
502            weak: Pair::new(weak, text),
503            strong: Pair::new(strong, text),
504        }
505    }
506}
507
508/// A set of success colors.
509#[derive(Debug, Clone, Copy, PartialEq)]
510pub struct Success {
511    /// The base success color.
512    pub base: Pair,
513    /// A weaker version of the base success color.
514    pub weak: Pair,
515    /// A stronger version of the base success color.
516    pub strong: Pair,
517}
518
519impl Success {
520    /// Generates a set of [`Success`] colors from the base, background, and text colors.
521    pub fn generate(base: Color, background: Color, text: Color) -> Self {
522        let weak = mix(base, background, 0.4);
523        let strong = deviate(base, 0.1);
524
525        Self {
526            base: Pair::new(base, text),
527            weak: Pair::new(weak, text),
528            strong: Pair::new(strong, text),
529        }
530    }
531}
532
533/// A set of danger colors.
534#[derive(Debug, Clone, Copy, PartialEq)]
535pub struct Danger {
536    /// The base danger color.
537    pub base: Pair,
538    /// A weaker version of the base danger color.
539    pub weak: Pair,
540    /// A stronger version of the base danger color.
541    pub strong: Pair,
542}
543
544impl Danger {
545    /// Generates a set of [`Danger`] colors from the base, background, and text colors.
546    pub fn generate(base: Color, background: Color, text: Color) -> Self {
547        let weak = mix(base, background, 0.4);
548        let strong = deviate(base, 0.1);
549
550        Self {
551            base: Pair::new(base, text),
552            weak: Pair::new(weak, text),
553            strong: Pair::new(strong, text),
554        }
555    }
556}
557
558fn darken(color: Color, amount: f32) -> Color {
559    let mut hsl = to_hsl(color);
560
561    hsl.lightness = if hsl.lightness - amount < 0.0 {
562        0.0
563    } else {
564        hsl.lightness - amount
565    };
566
567    from_hsl(hsl)
568}
569
570fn lighten(color: Color, amount: f32) -> Color {
571    let mut hsl = to_hsl(color);
572
573    hsl.lightness = if hsl.lightness + amount > 1.0 {
574        1.0
575    } else {
576        hsl.lightness + amount
577    };
578
579    from_hsl(hsl)
580}
581
582fn deviate(color: Color, amount: f32) -> Color {
583    if is_dark(color) {
584        lighten(color, amount)
585    } else {
586        darken(color, amount)
587    }
588}
589
590fn mix(a: Color, b: Color, factor: f32) -> Color {
591    let a_lin = Rgb::from(a).into_linear();
592    let b_lin = Rgb::from(b).into_linear();
593
594    let mixed = a_lin.mix(b_lin, factor);
595    Rgb::from_linear(mixed).into()
596}
597
598fn readable(background: Color, text: Color) -> Color {
599    if is_readable(background, text) {
600        text
601    } else if is_dark(background) {
602        Color::WHITE
603    } else {
604        Color::BLACK
605    }
606}
607
608fn is_dark(color: Color) -> bool {
609    to_hsl(color).lightness < 0.6
610}
611
612fn is_readable(a: Color, b: Color) -> bool {
613    let a_srgb = Rgb::from(a);
614    let b_srgb = Rgb::from(b);
615
616    a_srgb.has_enhanced_contrast_text(b_srgb)
617}
618
619fn to_hsl(color: Color) -> Hsl {
620    Hsl::from_color(Rgb::from(color))
621}
622
623fn from_hsl(hsl: Hsl) -> Color {
624    Rgb::from_color(hsl).into()
625}