use super::palette::Palette;
use std::marker::PhantomData;
pub trait Color {
fn rgb(&self) -> (u8, u8, u8);
fn alpha(&self) -> f64;
}
pub trait Mixable: Color {
fn mix(&self, alpha: f64) -> CompsitableColor<Self> {
CompsitableColor(self, alpha)
}
}
impl<T: Color + Sized> Mixable for T {}
impl Color for Box<&dyn Color> {
fn rgb(&self) -> (u8, u8, u8) {
self.as_ref().rgb()
}
fn alpha(&self) -> f64 {
self.as_ref().alpha()
}
}
pub trait SimpleColor {
fn rgb(&self) -> (u8, u8, u8);
}
impl<T: SimpleColor> Color for T {
fn rgb(&self) -> (u8, u8, u8) {
SimpleColor::rgb(self)
}
fn alpha(&self) -> f64 {
1.0
}
}
pub struct PaletteColor<P: Palette>(usize, PhantomData<P>);
impl<P: Palette> PaletteColor<P> {
pub fn pick(idx: usize) -> PaletteColor<P> {
return PaletteColor(idx % P::COLORS.len(), PhantomData);
}
}
impl<P: Palette> SimpleColor for PaletteColor<P> {
fn rgb(&self) -> (u8, u8, u8) {
P::COLORS[self.0]
}
}
pub struct CompsitableColor<'a, T: Color + ?Sized>(&'a T, f64);
impl<'a, T: Color> Color for CompsitableColor<'a, T> {
fn rgb(&self) -> (u8, u8, u8) {
(self.0).rgb()
}
fn alpha(&self) -> f64 {
(self.0).alpha() * self.1
}
}
pub struct RGBColor(pub u8, pub u8, pub u8);
impl SimpleColor for RGBColor {
fn rgb(&self) -> (u8, u8, u8) {
(self.0, self.1, self.2)
}
}
pub struct Transparent;
pub struct White;
pub struct Black;
pub struct Red;
pub struct Green;
pub struct Blue;
pub struct Yellow;
pub struct Cyan;
pub struct Magenta;
impl SimpleColor for White {
fn rgb(&self) -> (u8, u8, u8) {
return (255, 255, 255);
}
}
impl SimpleColor for Black {
fn rgb(&self) -> (u8, u8, u8) {
return (0, 0, 0);
}
}
impl SimpleColor for Green {
fn rgb(&self) -> (u8, u8, u8) {
return (0, 255, 0);
}
}
impl SimpleColor for Red {
fn rgb(&self) -> (u8, u8, u8) {
return (255, 0, 0);
}
}
impl SimpleColor for Blue {
fn rgb(&self) -> (u8, u8, u8) {
return (0, 0, 255);
}
}
impl SimpleColor for Cyan {
fn rgb(&self) -> (u8, u8, u8) {
return (0, 255, 255);
}
}
impl SimpleColor for Yellow {
fn rgb(&self) -> (u8, u8, u8) {
return (255, 255, 0);
}
}
impl SimpleColor for Magenta {
fn rgb(&self) -> (u8, u8, u8) {
return (255, 255, 0);
}
}
impl Color for Transparent {
fn rgb(&self) -> (u8, u8, u8) {
return (0, 0, 0);
}
fn alpha(&self) -> f64 {
return 0.0;
}
}