diff --git a/crates/bevy_color/src/interpolate.rs b/crates/bevy_color/src/interpolate.rs new file mode 100644 index 0000000000..75d1717d5a --- /dev/null +++ b/crates/bevy_color/src/interpolate.rs @@ -0,0 +1,37 @@ +//! TODO: Implement for non-linear colors. + +#[cfg(test)] +mod test { + use bevy_math::StableInterpolate; + + use crate::{Gray, Laba, LinearRgba, Oklaba, Srgba, Xyza}; + + #[test] + pub fn test_color_stable_interpolate() { + let b = Srgba::BLACK; + let w = Srgba::WHITE; + assert_eq!( + b.interpolate_stable(&w, 0.5), + Srgba::new(0.5, 0.5, 0.5, 1.0), + ); + + let b = LinearRgba::BLACK; + let w = LinearRgba::WHITE; + assert_eq!( + b.interpolate_stable(&w, 0.5), + LinearRgba::new(0.5, 0.5, 0.5, 1.0), + ); + + let b = Xyza::BLACK; + let w = Xyza::WHITE; + assert_eq!(b.interpolate_stable(&w, 0.5), Xyza::gray(0.5),); + + let b = Laba::BLACK; + let w = Laba::WHITE; + assert_eq!(b.interpolate_stable(&w, 0.5), Laba::new(0.5, 0.0, 0.0, 1.0),); + + let b = Oklaba::BLACK; + let w = Oklaba::WHITE; + assert_eq!(b.interpolate_stable(&w, 0.5), Oklaba::gray(0.5),); + } +} diff --git a/crates/bevy_color/src/lib.rs b/crates/bevy_color/src/lib.rs index e1ee1fbe38..712da5d7ec 100644 --- a/crates/bevy_color/src/lib.rs +++ b/crates/bevy_color/src/lib.rs @@ -105,6 +105,7 @@ mod color_range; mod hsla; mod hsva; mod hwba; +mod interpolate; mod laba; mod lcha; mod linear_rgba; @@ -265,6 +266,12 @@ macro_rules! impl_componentwise_vector_space { $($element: 0.0,)+ }; } + + impl bevy_math::StableInterpolate for $ty { + fn interpolate_stable(&self, other: &Self, t: f32) -> Self { + bevy_math::VectorSpace::lerp(*self, *other, t) + } + } }; }