use std::rc::Rc;
use tiny_skia::Color;
use super::{Context, Drawable};
pub struct Styled<'a, T: Styleable<S>, S> {
pub inner: &'a T,
pub style: S,
}
pub trait Styleable<S>: Drawable + Sized {
fn as_styled(&self, style: S) -> Styled<Self, S> {
Styled { inner: self, style }
}
}
#[derive(Clone)]
pub struct Effect<'a, T: Drawable, S> {
#[allow(clippy::type_complexity)]
func: Rc<dyn (Fn(S, &T, &Context) -> S) + 'a>,
}
impl<'a, T: Drawable, S> Effect<'a, T, S> {
pub fn new<F: (Fn(S, &T, &Context) -> S) + 'a>(func: F) -> Self {
Self {
func: Rc::new(func),
}
}
pub fn apply(self, style: S, drawable: &T, context: &Context) -> S {
(self.func)(style, drawable, context)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ColorOptions {
pub foreground: Color,
pub background: Color,
pub anti_alias: bool,
pub border: Option<f32>,
}
impl ColorOptions {
pub fn foreground_as_hex_code(&self) -> String {
let u8_color = self.foreground.to_color_u8();
let array = [
u8_color.red(),
u8_color.green(),
u8_color.blue(),
u8_color.alpha(),
];
format!("#{hex}", hex = hex::encode(array))
}
pub fn background_as_hex_code(&self) -> String {
let u8_color = self.background.to_color_u8();
let array = [
u8_color.red(),
u8_color.green(),
u8_color.blue(),
u8_color.alpha(),
];
format!("#{hex}", hex = hex::encode(array))
}
}
impl Default for ColorOptions {
fn default() -> Self {
Self {
foreground: Color::from_rgba8(248, 248, 248, 255),
background: Color::from_rgba8(26, 26, 26, 255),
anti_alias: true,
border: Some(1.0),
}
}
}