cosmic_theme/model/
cosmic_palette.rs

1use palette::Srgba;
2use serde::{Deserialize, Serialize};
3use std::sync::LazyLock;
4
5/// built-in light palette
6pub static LIGHT_PALETTE: LazyLock<CosmicPalette> =
7    LazyLock::new(|| ron::from_str(include_str!("light.ron")).unwrap());
8
9/// built-in dark palette
10pub static DARK_PALETTE: LazyLock<CosmicPalette> =
11    LazyLock::new(|| ron::from_str(include_str!("dark.ron")).unwrap());
12
13/// Palette type
14#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
15pub enum CosmicPalette {
16    /// Dark mode
17    Dark(CosmicPaletteInner),
18    /// Light mode
19    Light(CosmicPaletteInner),
20    /// High contrast light mode
21    HighContrastLight(CosmicPaletteInner),
22    /// High contrast dark mode
23    HighContrastDark(CosmicPaletteInner),
24}
25
26impl CosmicPalette {
27    /// extract the inner palette
28    #[inline]
29    pub fn inner(self) -> CosmicPaletteInner {
30        match self {
31            CosmicPalette::Dark(p) => p,
32            CosmicPalette::Light(p) => p,
33            CosmicPalette::HighContrastLight(p) => p,
34            CosmicPalette::HighContrastDark(p) => p,
35        }
36    }
37}
38
39impl AsMut<CosmicPaletteInner> for CosmicPalette {
40    #[inline]
41    fn as_mut(&mut self) -> &mut CosmicPaletteInner {
42        match self {
43            CosmicPalette::Dark(p) => p,
44            CosmicPalette::Light(p) => p,
45            CosmicPalette::HighContrastLight(p) => p,
46            CosmicPalette::HighContrastDark(p) => p,
47        }
48    }
49}
50
51impl AsRef<CosmicPaletteInner> for CosmicPalette {
52    #[inline]
53    fn as_ref(&self) -> &CosmicPaletteInner {
54        match self {
55            CosmicPalette::Dark(p) => p,
56            CosmicPalette::Light(p) => p,
57            CosmicPalette::HighContrastLight(p) => p,
58            CosmicPalette::HighContrastDark(p) => p,
59        }
60    }
61}
62
63impl CosmicPalette {
64    /// check if the palette is dark
65    #[inline]
66    pub fn is_dark(&self) -> bool {
67        match self {
68            CosmicPalette::Dark(_) | CosmicPalette::HighContrastDark(_) => true,
69            CosmicPalette::Light(_) | CosmicPalette::HighContrastLight(_) => false,
70        }
71    }
72
73    /// check if the palette is high_contrast
74    #[inline]
75    pub fn is_high_contrast(&self) -> bool {
76        match self {
77            CosmicPalette::HighContrastLight(_) | CosmicPalette::HighContrastDark(_) => true,
78            CosmicPalette::Light(_) | CosmicPalette::Dark(_) => false,
79        }
80    }
81}
82
83impl Default for CosmicPalette {
84    #[inline]
85    fn default() -> Self {
86        CosmicPalette::Dark(Default::default())
87    }
88}
89
90/// The palette for Cosmic Theme, from which all color properties are derived
91#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
92pub struct CosmicPaletteInner {
93    /// name of the palette
94    pub name: String,
95
96    /// Utility Colors
97    /// Colors used for various points of emphasis in the UI.
98    pub bright_red: Srgba,
99    /// Colors used for various points of emphasis in the UI.
100    pub bright_green: Srgba,
101    /// Colors used for various points of emphasis in the UI.
102    pub bright_orange: Srgba,
103
104    /// Surface Grays
105    /// Colors used for three levels of surfaces in the UI.
106    pub gray_1: Srgba,
107    /// Colors used for three levels of surfaces in the UI.
108    pub gray_2: Srgba,
109
110    /// System Neutrals
111    /// A wider spread of dark colors for more general use.
112    pub neutral_0: Srgba,
113    /// A wider spread of dark colors for more general use.
114    pub neutral_1: Srgba,
115    /// A wider spread of dark colors for more general use.
116    pub neutral_2: Srgba,
117    /// A wider spread of dark colors for more general use.
118    pub neutral_3: Srgba,
119    /// A wider spread of dark colors for more general use.
120    pub neutral_4: Srgba,
121    /// A wider spread of dark colors for more general use.
122    pub neutral_5: Srgba,
123    /// A wider spread of dark colors for more general use.
124    pub neutral_6: Srgba,
125    /// A wider spread of dark colors for more general use.
126    pub neutral_7: Srgba,
127    /// A wider spread of dark colors for more general use.
128    pub neutral_8: Srgba,
129    /// A wider spread of dark colors for more general use.
130    pub neutral_9: Srgba,
131    /// A wider spread of dark colors for more general use.
132    pub neutral_10: Srgba,
133
134    /// Potential Accent Color Combos
135    pub accent_blue: Srgba,
136    /// Potential Accent Color Combos
137    pub accent_indigo: Srgba,
138    /// Potential Accent Color Combos
139    pub accent_purple: Srgba,
140    /// Potential Accent Color Combos
141    pub accent_pink: Srgba,
142    /// Potential Accent Color Combos
143    pub accent_red: Srgba,
144    /// Potential Accent Color Combos
145    pub accent_orange: Srgba,
146    /// Potential Accent Color Combos
147    pub accent_yellow: Srgba,
148    /// Potential Accent Color Combos
149    pub accent_green: Srgba,
150    /// Potential Accent Color Combos
151    pub accent_warm_grey: Srgba,
152
153    /// Extended Color Palette
154    /// Colors used for themes, app icons, illustrations, and other brand purposes.
155    pub ext_warm_grey: Srgba,
156    /// Colors used for themes, app icons, illustrations, and other brand purposes.
157    pub ext_orange: Srgba,
158    /// Colors used for themes, app icons, illustrations, and other brand purposes.
159    pub ext_yellow: Srgba,
160    /// Colors used for themes, app icons, illustrations, and other brand purposes.
161    pub ext_blue: Srgba,
162    /// Colors used for themes, app icons, illustrations, and other brand purposes.
163    pub ext_purple: Srgba,
164    /// Colors used for themes, app icons, illustrations, and other brand purposes.
165    pub ext_pink: Srgba,
166    /// Colors used for themes, app icons, illustrations, and other brand purposes.
167    pub ext_indigo: Srgba,
168}
169
170impl CosmicPalette {
171    /// name of the palette
172    #[inline]
173    pub fn name(&self) -> &str {
174        match &self {
175            CosmicPalette::Dark(p) => &p.name,
176            CosmicPalette::Light(p) => &p.name,
177            CosmicPalette::HighContrastLight(p) => &p.name,
178            CosmicPalette::HighContrastDark(p) => &p.name,
179        }
180    }
181}