iced_core/theme/
palette.rs

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