use std::hash::{Hash, Hasher};
use palette::Srgb;
pub type Color = Srgb<u8>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Style {
color: Color,
bg: Option<Color>,
underline: bool,
strikethrough: bool,
italic: bool,
bold: bool,
}
impl Style {
pub fn new(
color: Color,
bg: Option<Color>,
underline: bool,
strikethrough: bool,
italic: bool,
bold: bool,
) -> Self {
Self {
color,
bg,
underline,
strikethrough,
italic,
bold,
}
}
pub fn color_only(red: u8, green: u8, blue: u8) -> Self {
Self::new(
Color::new(red, green, blue),
None,
false,
false,
false,
false,
)
}
pub fn color(&self) -> Color {
self.color
}
pub fn bg(&self) -> Option<Color> {
self.bg
}
pub fn underline(&self) -> bool {
self.underline
}
pub fn strikethrough(&self) -> bool {
self.strikethrough
}
pub fn italic(&self) -> bool {
self.italic
}
pub fn bold(&self) -> bool {
self.bold
}
}
impl From<Color> for Style {
fn from(color: Color) -> Self {
Self::new(color, None, false, false, false, false)
}
}
impl Hash for Style {
fn hash<H: Hasher>(&self, state: &mut H) {
Hash::hash(&self.color.red, state);
Hash::hash(&self.color.green, state);
Hash::hash(&self.color.blue, state);
Hash::hash(&self.color.standard, state);
if let Some(color) = self.bg {
Hash::hash(&true, state);
Hash::hash(&color.red, state);
Hash::hash(&color.green, state);
Hash::hash(&color.blue, state);
Hash::hash(&color.standard, state);
} else {
Hash::hash(&false, state);
}
Hash::hash(&self.underline, state);
Hash::hash(&self.strikethrough, state);
Hash::hash(&self.italic, state);
Hash::hash(&self.bold, state);
}
}