1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use geom::Point;
use palette::Colora;
use std::rc::Rc;

pub enum BlendMode {
    Normal,
}

#[derive(Clone)]
pub struct Colorer(Option<Rc<Fn(Point) -> Colora>>);

impl Default for Colorer {
    fn default() -> Self { Colorer(None) }
}

impl From<Colora> for Colorer {
    fn from(color: Colora) -> Self { Colorer(Some(Rc::new(move |_| color))) }
}

impl<F: 'static + Fn(Point) -> Colora> From<F> for Colorer {
    default fn from(f: F) -> Self { Colorer(Some(Rc::new(f))) }
}

impl<F: 'static + Fn(Point) -> Colora> From<Rc<F>> for Colorer {
    fn from(f: Rc<F>) -> Self { Colorer(Some(f.clone())) }
}

impl Colorer {
    pub fn red() -> Self { Self::from(Colora::rgb(1.0, 0.0, 0.0, 1.0)) }
    pub fn blue() -> Self { Self::from(Colora::rgb(0.0, 0.0, 1.0, 1.0)) }
    pub fn black() -> Self { Self::from(Colora::rgb(0.0, 0.0, 0.0, 1.0)) }

    pub fn color(&self, point: Point) -> Colora {
        match self.0 {
            None => Colora::rgb(0.0, 0.0, 0.0, 0.0),
            Some(ref f) => (f.as_ref())(point),
        }
    }
}