#![allow(non_snake_case)]
use crate::animation::{Animatable, AnimationType, Lerp, SpringScalar};
use cranpose_core::{with_current_composer, Owned, State};
use cranpose_ui_graphics::Color;
impl Lerp for Color {
fn lerp(&self, target: &Self, fraction: f32) -> Self {
Color(
self.0.lerp(&target.0, fraction).clamp(0.0, 1.0),
self.1.lerp(&target.1, fraction).clamp(0.0, 1.0),
self.2.lerp(&target.2, fraction).clamp(0.0, 1.0),
self.3.lerp(&target.3, fraction).clamp(0.0, 1.0),
)
}
}
impl SpringScalar for Color {
const DIMENSIONS: usize = 4;
fn dimension(&self, index: usize) -> f32 {
match index {
0 => self.0,
1 => self.1,
2 => self.2,
_ => self.3,
}
}
fn from_dimensions(dimensions: [f32; crate::animation::SPRING_MAX_DIMENSIONS]) -> Self {
Color(
dimensions[0].clamp(0.0, 1.0),
dimensions[1].clamp(0.0, 1.0),
dimensions[2].clamp(0.0, 1.0),
dimensions[3].clamp(0.0, 1.0),
)
}
}
pub fn animateColorAsState(target: Color, animation: AnimationType, label: &str) -> State<Color> {
let _ = label;
with_current_composer(|composer| {
let runtime = composer.runtime_handle();
let anim: Owned<Animatable<Color>> = composer.remember(|| Animatable::new(target, runtime));
anim.update(|animatable| {
let is_new_target = animatable.target() != target;
let is_new_animation = animatable.animation_type() != animation;
if is_new_target || is_new_animation {
animatable.animateTo(target, animation);
}
});
anim.with(|animatable| animatable.state())
})
}
#[cfg(test)]
#[path = "tests/color_tests.rs"]
mod tests;