cosmic_theme/output/
mod.rs

1use palette::{rgb::Rgba, Srgba};
2use thiserror::Error;
3
4use crate::Theme;
5
6/// Module for outputting the Cosmic gtk4 theme type as CSS
7pub mod gtk4_output;
8
9pub mod vs_code;
10
11#[derive(Error, Debug)]
12pub enum OutputError {
13    #[error("IO Error: {0}")]
14    Io(std::io::Error),
15    #[error("Missing config directory")]
16    MissingConfigDir,
17    #[error("Serde Error: {0}")]
18    Serde(#[from] serde_json::Error),
19}
20
21impl Theme {
22    #[inline]
23    pub fn apply_exports(&self) -> Result<(), OutputError> {
24        let gtk_res = Theme::apply_gtk(self.is_dark);
25        let vs_res = self.clone().apply_vs_code();
26        gtk_res?;
27        vs_res?;
28        Ok(())
29    }
30
31    #[inline]
32    pub fn write_exports(&self) -> Result<(), OutputError> {
33        let gtk_res = self.write_gtk4();
34        gtk_res?;
35        Ok(())
36    }
37
38    #[inline]
39    pub fn reset_exports() -> Result<(), OutputError> {
40        let gtk_res = Theme::reset_gtk();
41        let vs_res = Theme::reset_vs_code();
42        gtk_res?;
43        vs_res?;
44        Ok(())
45    }
46}
47
48pub fn to_hex(c: Srgba) -> String {
49    let c_u8: Rgba<palette::encoding::Srgb, u8> = c.into_format();
50    format!(
51        "{:02x}{:02x}{:02x}{:02x}",
52        c_u8.red, c_u8.green, c_u8.blue, c_u8.alpha
53    )
54}
55
56pub fn to_rgba(c: Srgba) -> String {
57    let c_u8: Rgba<palette::encoding::Srgb, u8> = c.into_format();
58    format!(
59        "rgba({}, {}, {}, {:1.2})",
60        c_u8.red, c_u8.green, c_u8.blue, c.alpha
61    )
62}