1use crate::{AnimationValue, Transform};
4
5pub trait Interpolate {
7 fn interpolate(&self, to: &Self, progress: f64) -> Self;
9}
10
11pub fn lerp(from: f64, to: f64, progress: f64) -> f64 {
13 from + (to - from) * progress
14}
15
16impl Interpolate for f64 {
17 fn interpolate(&self, to: &Self, progress: f64) -> Self {
18 lerp(*self, *to, progress)
19 }
20}
21
22impl Interpolate for AnimationValue {
23 fn interpolate(&self, to: &Self, progress: f64) -> Self {
24 match (self, to) {
25 (AnimationValue::Number(from), AnimationValue::Number(to)) => {
26 AnimationValue::Number(lerp(*from, *to, progress))
27 }
28 (AnimationValue::Pixels(from), AnimationValue::Pixels(to)) => {
29 AnimationValue::Pixels(lerp(*from, *to, progress))
30 }
31 (AnimationValue::Percentage(from), AnimationValue::Percentage(to)) => {
32 AnimationValue::Percentage(lerp(*from, *to, progress))
33 }
34 (AnimationValue::Degrees(from), AnimationValue::Degrees(to)) => {
35 AnimationValue::Degrees(lerp(*from, *to, progress))
36 }
37 (AnimationValue::Radians(from), AnimationValue::Radians(to)) => {
38 AnimationValue::Radians(lerp(*from, *to, progress))
39 }
40 (AnimationValue::Transform(from), AnimationValue::Transform(to)) => {
41 AnimationValue::Transform(from.interpolate(to, progress))
42 }
43 (AnimationValue::Color(from), AnimationValue::Color(to)) => {
44 AnimationValue::Color(interpolate_color(from, to, progress))
45 }
46 _ => {
47 if progress < 0.5 {
49 self.clone()
50 } else {
51 to.clone()
52 }
53 }
54 }
55 }
56}
57
58impl Interpolate for Transform {
59 fn interpolate(&self, to: &Self, progress: f64) -> Self {
60 Transform {
61 x: interpolate_option(self.x, to.x, progress),
62 y: interpolate_option(self.y, to.y, progress),
63 z: interpolate_option(self.z, to.z, progress),
64 rotate_x: interpolate_option(self.rotate_x, to.rotate_x, progress),
65 rotate_y: interpolate_option(self.rotate_y, to.rotate_y, progress),
66 rotate_z: interpolate_option(self.rotate_z, to.rotate_z, progress),
67 scale: interpolate_option(self.scale, to.scale, progress),
68 scale_x: interpolate_option(self.scale_x, to.scale_x, progress),
69 scale_y: interpolate_option(self.scale_y, to.scale_y, progress),
70 skew_x: interpolate_option(self.skew_x, to.skew_x, progress),
71 skew_y: interpolate_option(self.skew_y, to.skew_y, progress),
72 }
73 }
74}
75
76fn interpolate_option(from: Option<f64>, to: Option<f64>, progress: f64) -> Option<f64> {
78 match (from, to) {
79 (Some(from), Some(to)) => Some(lerp(from, to, progress)),
80 (Some(from), None) => Some(lerp(from, 0.0, progress)),
81 (None, Some(to)) => Some(lerp(0.0, to, progress)),
82 (None, None) => None,
83 }
84}
85
86fn interpolate_color(from: &str, to: &str, progress: f64) -> String {
88 if progress < 0.5 {
91 from.to_string()
92 } else {
93 to.to_string()
94 }
95}
96
97pub mod color {
99 use super::lerp;
100
101 #[derive(Debug, Clone, Copy, PartialEq)]
103 pub struct Rgba {
104 pub r: f64,
105 pub g: f64,
106 pub b: f64,
107 pub a: f64,
108 }
109
110 impl Rgba {
111 pub fn new(r: f64, g: f64, b: f64, a: f64) -> Self {
112 Self {
113 r: r.clamp(0.0, 255.0),
114 g: g.clamp(0.0, 255.0),
115 b: b.clamp(0.0, 255.0),
116 a: a.clamp(0.0, 1.0),
117 }
118 }
119
120 pub fn interpolate(&self, to: &Self, progress: f64) -> Self {
121 Self {
122 r: lerp(self.r, to.r, progress),
123 g: lerp(self.g, to.g, progress),
124 b: lerp(self.b, to.b, progress),
125 a: lerp(self.a, to.a, progress),
126 }
127 }
128
129 pub fn to_css(&self) -> String {
130 if self.a < 1.0 {
131 format!("rgba({}, {}, {}, {})", self.r, self.g, self.b, self.a)
132 } else {
133 format!("rgb({}, {}, {})", self.r, self.g, self.b)
134 }
135 }
136
137 pub fn from_hex(hex: &str) -> Option<Self> {
138 let hex = hex.trim_start_matches('#');
139 if hex.len() == 6 {
140 let r = u8::from_str_radix(&hex[0..2], 16).ok()? as f64;
141 let g = u8::from_str_radix(&hex[2..4], 16).ok()? as f64;
142 let b = u8::from_str_radix(&hex[4..6], 16).ok()? as f64;
143 Some(Self::new(r, g, b, 1.0))
144 } else {
145 None
146 }
147 }
148 }
149}
150
151#[cfg(test)]
152mod tests {
153 use super::*;
154 use approx::assert_relative_eq;
155
156 #[test]
157 fn test_lerp() {
158 assert_relative_eq!(lerp(0.0, 100.0, 0.0), 0.0);
159 assert_relative_eq!(lerp(0.0, 100.0, 0.5), 50.0);
160 assert_relative_eq!(lerp(0.0, 100.0, 1.0), 100.0);
161 assert_relative_eq!(lerp(50.0, 150.0, 0.25), 75.0);
162 }
163
164 #[test]
165 fn test_animation_value_interpolation() {
166 let from = AnimationValue::Number(0.0);
167 let to = AnimationValue::Number(100.0);
168
169 let mid = from.interpolate(&to, 0.5);
170 assert_eq!(mid, AnimationValue::Number(50.0));
171
172 let pixels_from = AnimationValue::Pixels(10.0);
173 let pixels_to = AnimationValue::Pixels(20.0);
174 let pixels_mid = pixels_from.interpolate(&pixels_to, 0.3);
175 assert_eq!(pixels_mid, AnimationValue::Pixels(13.0));
176 }
177
178 #[test]
179 fn test_transform_interpolation() {
180 let from = Transform {
181 x: Some(0.0),
182 y: Some(0.0),
183 scale: Some(1.0),
184 ..Default::default()
185 };
186
187 let to = Transform {
188 x: Some(100.0),
189 y: Some(50.0),
190 scale: Some(2.0),
191 ..Default::default()
192 };
193
194 let mid = from.interpolate(&to, 0.5);
195 assert_eq!(mid.x, Some(50.0));
196 assert_eq!(mid.y, Some(25.0));
197 assert_eq!(mid.scale, Some(1.5));
198 }
199
200 #[test]
201 fn test_option_interpolation() {
202 assert_eq!(interpolate_option(Some(0.0), Some(100.0), 0.5), Some(50.0));
203 assert_eq!(interpolate_option(Some(10.0), None, 0.5), Some(5.0));
204 assert_eq!(interpolate_option(None, Some(20.0), 0.5), Some(10.0));
205 assert_eq!(interpolate_option(None, None, 0.5), None);
206 }
207
208 #[test]
209 fn test_rgba_color() {
210 use super::color::Rgba;
211
212 let red = Rgba::new(255.0, 0.0, 0.0, 1.0);
213 let blue = Rgba::new(0.0, 0.0, 255.0, 1.0);
214
215 let purple = red.interpolate(&blue, 0.5);
216 assert_relative_eq!(purple.r, 127.5, epsilon = 0.1);
217 assert_relative_eq!(purple.g, 0.0);
218 assert_relative_eq!(purple.b, 127.5, epsilon = 0.1);
219
220 assert_eq!(red.to_css(), "rgb(255, 0, 0)");
221
222 let transparent_red = Rgba::new(255.0, 0.0, 0.0, 0.5);
223 assert_eq!(transparent_red.to_css(), "rgba(255, 0, 0, 0.5)");
224 }
225
226 #[test]
227 fn test_hex_color_parsing() {
228 use super::color::Rgba;
229
230 let red = Rgba::from_hex("#ff0000").unwrap();
231 assert_eq!(red.r, 255.0);
232 assert_eq!(red.g, 0.0);
233 assert_eq!(red.b, 0.0);
234 assert_eq!(red.a, 1.0);
235
236 let green = Rgba::from_hex("00ff00").unwrap();
237 assert_eq!(green.g, 255.0);
238
239 assert!(Rgba::from_hex("invalid").is_none());
240 }
241}