use crate::{
color::{ApproxBrightness, Color, Color2},
string::TermGrapheme,
};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Tile {
pub grapheme: TermGrapheme,
pub colors: Color2,
}
pub trait Updater {
fn update(self, tile: &mut Tile);
}
impl Updater for Tile {
fn update(self, tile: &mut Tile) {
*tile = self;
}
}
impl<F> Updater for F
where
F: FnOnce(&mut Tile),
{
fn update(self, tile: &mut Tile) {
self(tile)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Foreground {
pub grapheme: TermGrapheme,
pub color: Color,
}
impl Updater for Foreground {
fn update(self, tile: &mut Tile) {
tile.grapheme = self.grapheme;
tile.colors.foreground = self
.color
.with_approx_brightness(!tile.colors.background.approx_brightness())
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Background {
pub color: Color,
}
impl Updater for Background {
fn update(self, tile: &mut Tile) {
tile.colors.background = self.color;
tile.colors.foreground = tile
.colors
.foreground
.with_approx_brightness(!tile.colors.background.approx_brightness())
}
}