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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::entity::Entity;

use crate::state::animation::Interpolator;
use crate::style::color::Color;

use crate::style::Units;

#[derive(Default, Debug, Clone, PartialEq)]
pub struct GradientStop {
    // Position of the gradient stop
    // TODO - it doesn't make sense for this to be in Units
    pub position: Units,
    // Colour of the gradient stop
    pub color: Color,
}

impl GradientStop {
    pub fn new(position: Units, color: Color) -> Self {
        Self { position, color }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum Direction {
    LeftToRight,
    RightToLeft,
    TopToBottom,
    BottomToTop,
}

impl Default for Direction {
    fn default() -> Self {
        Direction::LeftToRight
    }
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct LinearGradient {
    // Direction of the gradient
    pub direction: Direction,
    // Stops of the gradient
    pub stops: Vec<GradientStop>,
}

impl LinearGradient {
    pub fn new(direction: Direction) -> Self {
        Self {
            direction,
            stops: Vec::new(),
        }
    }

    pub fn add_stop(mut self, stop: GradientStop) -> Self {
        self.stops.push(stop);

        self
    }

    pub fn get_stops(&mut self, parent_length: f32) -> Vec<(f32, Color)> {
        self.stops
            .iter()
            .map(|stop| {
                //println!("Stop: {:?}", stop.position.value_or(parent_length, 0.0));
                (stop.position.value_or(1.0, 0.0), stop.color)
            })
            .collect::<Vec<_>>()
    }
}