kk/src/display/theme.rs

77 lines
1.8 KiB
Rust

use std::ops::Deref;
use termion::color;
#[derive(Clone, Copy, Debug)]
pub struct Theme {
pub colors: Colors,
}
#[derive(Clone, Copy, Debug)]
pub struct Colors {
pub primary_bg: Color,
pub frame_bg: Color,
pub frame_fg: Color,
pub text: Color,
}
impl Theme {}
#[derive(Clone, Copy, Debug)]
pub struct Color(color::Rgb);
impl Deref for Color {
type Target = color::Rgb;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl TryFrom<String> for Color {
type Error = anyhow::Error;
fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(value.as_str().try_into()?)
}
}
impl TryFrom<&str> for Color {
type Error = anyhow::Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
if value.len() < 6 || value.len() > 7 {
return Err(anyhow::anyhow!("hex code length invalid: {}", value.len()));
}
let mut i = 0;
if value.starts_with('#') {
i = 1;
}
Ok(Self(color::Rgb(
u8::from_str_radix(&value[i..i + 2], 16)?,
u8::from_str_radix(&value[i + 2..i + 4], 16)?,
u8::from_str_radix(&value[i + 4..i + 6], 16)?,
)))
}
}
impl Default for Theme {
fn default() -> Self {
Self {
colors: Colors {
primary_bg: "#3b224c".try_into().unwrap(),
text: "#ffe6ff".try_into().unwrap(),
frame_bg: "#330033".try_into().unwrap(),
frame_fg: "#ffe6ff".try_into().unwrap(),
},
}
}
}
impl Theme {
pub fn display_string(&self) -> String {
format!(
"{primary_bg}{text}",
primary_bg = self.colors.primary_bg.bg_string(),
text = self.colors.text.fg_string()
)
}
}