Skip to main content

ferroui_base/animation/animators/
byte_animator.rs

1use crate::animation::animators::{Animator, AnimatorBase};
2
3/// Animator that handles `u8` properties.
4#[derive(Default)]
5pub struct ByteAnimator {
6    base: AnimatorBase,
7}
8
9impl ByteAnimator {
10    pub fn new() -> Self {
11        Self::default()
12    }
13
14    /// Interpolates between two values using the specified progress.
15    pub fn interpolate_core(progress: f64, old_value: &u8, new_value: &u8) -> u8 {
16        const MAX_VAL: f64 = u8::MAX as f64;
17
18        let norm_ov = *old_value as f64 / MAX_VAL;
19        let norm_nv = *new_value as f64 / MAX_VAL;
20        let delta_v = norm_nv - norm_ov;
21        (MAX_VAL * ((delta_v * progress) + norm_ov)).round_ties_even() as u8
22    }
23}
24
25impl Animator for ByteAnimator {
26    type Value = u8;
27
28    fn base(&self) -> &AnimatorBase {
29        &self.base
30    }
31
32    #[inline]
33    fn interpolate(&self, progress: f64, old_value: &u8, new_value: &u8) -> u8 {
34        Self::interpolate_core(progress, old_value, new_value)
35    }
36}