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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::time::Duration;

use freya_core::parsing::Parse;
use freya_engine::prelude::Color;

use super::{
    apply_value,
    AnimDirection,
    AnimatedValue,
    Ease,
    Function,
};

/// Animate a color.
#[derive(Clone, PartialEq)]
pub struct AnimColor {
    origin: Color,
    destination: Color,
    time: Duration,
    ease: Ease,
    function: Function,

    value: Color,
}

impl AnimColor {
    pub fn new(origin: &str, destination: &str) -> Self {
        Self {
            origin: Color::parse(origin).unwrap(),
            destination: Color::parse(destination).unwrap(),
            time: Duration::default(),
            ease: Ease::default(),
            function: Function::default(),

            value: Color::parse(origin).unwrap(),
        }
    }

    /// Set the animation duration using milliseconds. Use `Self::duration` if you want to specify the duration in another form.
    pub fn time(mut self, time: u64) -> Self {
        self.time = Duration::from_millis(time);
        self
    }

    /// Set the animation duration using milliseconds.
    pub fn duration(mut self, duration: Duration) -> Self {
        self.time = duration;
        self
    }

    /// Set the easing type. See `Ease` for all the types.
    pub fn ease(mut self, ease: Ease) -> Self {
        self.ease = ease;
        self
    }

    /// Set the easing function. See `Function` for all the types.
    pub fn function(mut self, function: Function) -> Self {
        self.function = function;
        self
    }

    /// Read the value of the [AnimColor] as a String.
    pub fn read(&self) -> String {
        format!(
            "rgb({}, {}, {}, {})",
            self.value.r(),
            self.value.g(),
            self.value.b(),
            self.value.a()
        )
    }
}

impl AnimatedValue for AnimColor {
    fn prepare(&mut self, direction: AnimDirection) {
        match direction {
            AnimDirection::Forward => self.value = self.origin,
            AnimDirection::Reverse => {
                self.value = self.destination;
            }
        }
    }

    fn is_finished(&self, index: u128, direction: AnimDirection) -> bool {
        match direction {
            AnimDirection::Forward => {
                index > self.time.as_millis()
                    && self.value.r() == self.destination.r()
                    && self.value.g() == self.destination.g()
                    && self.value.b() == self.destination.b()
                    && self.value.a() == self.destination.a()
            }
            AnimDirection::Reverse => {
                index > self.time.as_millis()
                    && self.value.r() == self.origin.r()
                    && self.value.g() == self.origin.g()
                    && self.value.b() == self.origin.b()
                    && self.value.a() == self.origin.a()
            }
        }
    }

    fn advance(&mut self, index: u128, direction: AnimDirection) {
        let (origin, destination) = match direction {
            AnimDirection::Forward => (self.origin, self.destination),
            AnimDirection::Reverse => (self.destination, self.origin),
        };
        let r = apply_value(
            origin.r() as f32,
            destination.r() as f32,
            index.min(self.time.as_millis()),
            self.time,
            self.ease,
            self.function,
        );
        let g = apply_value(
            origin.g() as f32,
            destination.g() as f32,
            index.min(self.time.as_millis()),
            self.time,
            self.ease,
            self.function,
        );
        let b = apply_value(
            origin.b() as f32,
            destination.b() as f32,
            index.min(self.time.as_millis()),
            self.time,
            self.ease,
            self.function,
        );
        let a = apply_value(
            origin.a() as f32,
            destination.a() as f32,
            index.min(self.time.as_millis()),
            self.time,
            self.ease,
            self.function,
        );
        self.value = Color::from_argb(a as u8, r as u8, g as u8, b as u8);
    }

    fn finish(&mut self, direction: AnimDirection) {
        self.advance(self.time.as_millis(), direction);
    }
}