pub trait LinearInterpolate: PartialEq {
// Required method
fn lerp(&self, target: &Self, percent: f32) -> Self;
}
Expand description
Performs a linear interpolation between two values.
This trait can be derived for structs and fieldless enums.
Note: for fields that don’t implement LinerarInterpolate
the wrappers BinaryLerp
and ImmediateLerp
can be used.
use cushy::animation::{BinaryLerp, ImmediateLerp, LinearInterpolate};
use cushy::kludgine::Color;
#[derive(LinearInterpolate, PartialEq, Debug)]
struct Struct(Color, BinaryLerp<&'static str>, ImmediateLerp<&'static str>);
let from = Struct(Color::BLACK, "hello".into(), "hello".into());
let to = Struct(Color::WHITE, "world".into(), "world".into());
assert_eq!(
from.lerp(&to, 0.41),
Struct(Color::DIMGRAY, "hello".into(), "world".into())
);
assert_eq!(
from.lerp(&to, 0.663),
Struct(Color::DARKGRAY, "world".into(), "world".into())
);
#[derive(LinearInterpolate, PartialEq, Debug)]
enum Enum {
A,
B,
C,
}
assert_eq!(Enum::A.lerp(&Enum::B, 0.4), Enum::A);
assert_eq!(Enum::A.lerp(&Enum::C, 0.1), Enum::A);
assert_eq!(Enum::A.lerp(&Enum::C, 0.4), Enum::B);
assert_eq!(Enum::A.lerp(&Enum::C, 0.9), Enum::C);
Required Methods§
Object Safety§
This trait is not object safe.