use core::fmt::{Display, Formatter, Result};
use crate::{
Style, color::Color, impl_macros::additive_styling::impl_additive_styling_type,
impl_styling_atribute_for, impl_styling_element_for,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TargetedColor {
color: Color,
target: ColorTarget,
}
impl TargetedColor {
#[must_use]
pub fn new(color: impl Into<Color>, target: ColorTarget) -> Self {
let color = color.into();
Self { color, target }
}
#[must_use]
pub fn new_for_fg(color: impl Into<Color>) -> Self {
Self::new(color, ColorTarget::Foreground)
}
#[must_use]
pub fn new_for_bg(color: impl Into<Color>) -> Self {
Self::new(color, ColorTarget::Background)
}
#[must_use]
pub fn new_for_underline(color: impl Into<Color>) -> Self {
Self::new(color, ColorTarget::Underline)
}
#[must_use]
pub const fn get_color(self) -> Color {
self.color
}
#[must_use]
pub const fn get_target(self) -> ColorTarget {
self.target
}
}
impl_additive_styling_type!(TargetedColor {
args: [self];
to_style: { Style::new().color(self) }
});
impl_styling_element_for! { TargetedColor {
args: [self, composed_styling];
add_to: {
composed_styling.set_color(self.get_target(), Some(self.get_color()))
}
}}
impl Display for TargetedColor {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
self.to_style().fmt(f)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ColorTarget {
Foreground,
Background,
Underline,
}
impl_styling_atribute_for! { ColorTarget {
type Value = Option<Color>;
args: [self, composed_styling, value];
set_in: {
composed_styling.set_color(self, value)
}
get_from: {
composed_styling.get_color(self)
}
}}