Skip to content

Commit

Permalink
rewrite palette code and refactor theming
Browse files Browse the repository at this point in the history
  • Loading branch information
B0ney committed Nov 11, 2023
1 parent f3939ef commit ba6e152
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 213 deletions.
122 changes: 98 additions & 24 deletions data/src/theme.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod themes;

use iced_core::Color;
// use serde::Deserialize;
use serde::{Deserialize, Serialize};
pub use themes::Themes;

pub struct Theme {
Expand All @@ -11,37 +11,111 @@ pub struct Theme {

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Palette {
pub base: BaseColors,
pub normal: NormalColors,
pub bright: BrightColors,
pub background: Color,
pub middleground: Color,
pub foreground: Color,
pub border: Color,
pub text: Color,
pub accent: Color,
pub error: Color,
pub warning: Color,
pub success: Color,
}

impl Default for Palette {
fn default() -> Self {
Themes::Catppuccin.palette()
themes::Themes::Catppuccin.palette()
}
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BaseColors {
pub background: Color,
pub foreground: Color,
pub dark: Color, // TODO: sort
pub border: Color, // TODO: sort
}
fn hex_to_color(hex: &str) -> Option<Color> {
if hex.len() == 7 {
let hash = &hex[0..1];
let r = u8::from_str_radix(&hex[1..3], 16);
let g = u8::from_str_radix(&hex[3..5], 16);
let b = u8::from_str_radix(&hex[5..7], 16);

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NormalColors {
pub primary: Color,
pub secondary: Color,
pub surface: Color,
pub error: Color,
return match (hash, r, g, b) {
("#", Ok(r), Ok(g), Ok(b)) => Some(Color {
r: r as f32 / 255.0,
g: g as f32 / 255.0,
b: b as f32 / 255.0,
a: 1.0,
}),
_ => None,
};
}

None
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BrightColors {
pub primary: Color,
pub secondary: Color,
pub surface: Color,
pub error: Color,
pub mod palette_serde {
use super::{hex_to_color, Palette};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
struct ThemeHex {
background: String,
middleground: String,
foreground: String,
border: String,
text: String,
accent: String,
error: String,
warning: String,
success: String,
}

impl Serialize for Palette {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
fn as_hex(color: iced_core::Color) -> String {
format!(
"#{:02x}{:02x}{:02x}",
(255.0 * color.r).round() as u8,
(255.0 * color.g).round() as u8,
(255.0 * color.b).round() as u8
)
}

ThemeHex {
background: as_hex(self.background),
middleground: as_hex(self.middleground),
foreground: as_hex(self.foreground),
border: as_hex(self.border),
text: as_hex(self.text),
accent: as_hex(self.accent),
error: as_hex(self.error),
warning: as_hex(self.warning),
success: as_hex(self.success),
}
.serialize(serializer)
}
}

impl<'de> Deserialize<'de> for Palette {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let hex: ThemeHex = serde::Deserialize::deserialize(deserializer)?;

let to_color =
|hex: &str| hex_to_color(hex).ok_or_else(|| serde::de::Error::custom("Invalid hex"));

Ok(Palette {
background: to_color(&hex.text)?,
middleground: to_color(&hex.middleground)?,
foreground: to_color(&hex.foreground)?,
border: to_color(&hex.border)?,
text: to_color(&hex.text)?,
accent: to_color(&hex.accent)?,
error: to_color(&hex.error)?,
warning: to_color(&hex.warning)?,
success: to_color(&hex.success)?,
})
}
}
}
166 changes: 56 additions & 110 deletions data/src/theme/themes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use iced_core::color;
use serde::{Deserialize, Serialize};

use super::{BaseColors, BrightColors, NormalColors, Palette};
use super::Palette;

#[derive(Serialize, Deserialize, Default, Debug, PartialEq, Eq, Copy, Clone)]
pub enum Themes {
Expand All @@ -27,125 +27,71 @@ impl Themes {
pub fn palette(&self) -> Palette {
match self {
Self::Dark => Palette {
base: BaseColors {
background: color!(0x272727),
foreground: color!(0x353535),
dark: color!(0x151515),
border: color!(0x474747),
},
normal: NormalColors {
primary: color!(0xBA84FC),
secondary: color!(0x386e50),
surface: color!(0x828282),
error: color!(0xff5555),
},
bright: BrightColors {
primary: color!(0xBA84FC),
secondary: color!(0x49eb7a),
surface: color!(0xE0E0E0),
error: color!(0xff5555),
},
middleground: color!(0x272727),
foreground: color!(0x353535),
background: color!(0x151515),
border: color!(0x474747),
accent: color!(0xBA84FC),
text: color!(0xE0E0E0),
error: color!(0xff5555),
warning: color!(0xff5555),
success: color!(0x49eb7a),
},
Self::Dracula => Palette {
base: BaseColors {
background: color!(0x282a36),
foreground: color!(0x44475a),
dark: color!(0x1D1E26),
border: color!(0x4f5263),
},
normal: NormalColors {
primary: color!(0xff79c6),
secondary: color!(0x50fa7b),
surface: color!(0xf8f8f2),
error: color!(0xff5555),
},
bright: BrightColors {
primary: color!(0xff79c6),
secondary: color!(0x50fa7b),
surface: color!(0xf8f8f2),
error: color!(0xff5555),
},
middleground: color!(0x282a36),
foreground: color!(0x44475a),
background: color!(0x1D1E26),
border: color!(0x4f5263),
accent: color!(0xff79c6),
text: color!(0xf8f8f2),
error: color!(0xff5555),
warning: color!(0xff5555),
success: color!(0x50fa7b),
},
Self::LMMS => Palette {
base: BaseColors {
background: color!(0x262B30),
foreground: color!(0x3B424A), //3B424A
dark: color!(0x111314),
border: color!(0x4C5864),
},
normal: NormalColors {
primary: color!(0x309655),
secondary: color!(0x309655),
surface: color!(0xe5e9f0),
error: color!(0xff5555),
},
bright: BrightColors {
primary: color!(0x0BD556),
secondary: color!(0x0BD556),
surface: color!(0xe5e9f0),
error: color!(0xff5555),
},
middleground: color!(0x262B30),
foreground: color!(0x3B424A), //3B424A
background: color!(0x111314),
border: color!(0x4C5864),
accent: color!(0x0BD556),
text: color!(0xe5e9f0),
error: color!(0xff5555),
warning: color!(0xff5555),
success: color!(0x0BD556),
},
Self::Nord => Palette {
base: BaseColors {
background: color!(0x2e3440),
foreground: color!(0x3b4252),
dark: color!(0x21252d),
border: color!(0x50586d),
},
normal: NormalColors {
primary: color!(0x88c0d0),
secondary: color!(0xa3be8c),
surface: color!(0xe5e9f0),
error: color!(0xbf616a),
},
bright: BrightColors {
primary: color!(0x88c0d0),
secondary: color!(0xa3be8c),
surface: color!(0xe5e9f0),
error: color!(0xbf616a),
},
middleground: color!(0x2e3440),
foreground: color!(0x3b4252),
background: color!(0x21252d),
border: color!(0x50586d),
accent: color!(0x88c0d0),
text: color!(0xe5e9f0),
error: color!(0xbf616a),
warning: color!(0xbf616a),
success: color!(0xa3be8c),
},
Self::OneShot => Palette {
base: BaseColors {
background: color!(0x1A0B1D),
foreground: color!(0x2B0D1A),
dark: color!(0x100213),
border: color!(0xFBCD5D),
},
normal: NormalColors {
primary: color!(0xF48550),
secondary: color!(0x80FF80),
surface: color!(0xFEFECD),
error: color!(0xff5555),
},
bright: BrightColors {
primary: color!(0xFBCD5D),
secondary: color!(0x80FF80),
surface: color!(0xFEFECD),
error: color!(0xff5555),
},
middleground: color!(0x1A0B1D),
foreground: color!(0x2B0D1A),
background: color!(0x100213),
border: color!(0xFBCD5D),
accent: color!(0xFBCD5D), //color!(0xF48550),
text: color!(0xFEFECD),
error: color!(0xff5555),
warning: color!(0xff5555),
success: color!(0x80FF80),
},
Self::Catppuccin => Palette {
base: BaseColors {
background: color!(0x1E1E28),
foreground: color!(0x332E41),
dark: color!(0x1B1923),
border: color!(0x6E6C7E),
},
normal: NormalColors {
primary: color!(0xC6AAE8),
secondary: color!(0xB1E3AD),
surface: color!(0xD7DAE0),
error: color!(0xE38C8F),
},
bright: BrightColors {
primary: color!(0xC6AAE8),
secondary: color!(0xB1E3AD),
surface: color!(0xFEFECD),
error: color!(0xE38C8F),
},
},
middleground: color!(0x1E1E28),
foreground: color!(0x332E41),
background: color!(0x1B1923),
border: color!(0x6E6C7E),
accent: color!(0xC6AAE8),
text: color!(0xFEFECD),
error: color!(0xE38C8F),
warning: color!(0xE38C8F),
success: color!(0xB1E3AD),
}
}
}
}
Expand Down
Loading

0 comments on commit ba6e152

Please sign in to comment.