From 709ebd7cd138baaaef1a11263ddb7681562c4e05 Mon Sep 17 00:00:00 2001 From: Marco Buono Date: Tue, 31 Oct 2023 23:48:55 -0300 Subject: [PATCH] Add `Color::lerp()` convenience method for interpolating/mixing colors --- crates/bevy_render/src/color/mod.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index b4139b8787482..f8cb36221446d 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -1102,6 +1102,22 @@ impl Color { } } } + + /// Performs a linear interpolation between `self` and `rhs` based on the value `s`. + /// + /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result + /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly + /// extrapolated. + /// + /// **Note:** To ensure mathematically correct results, interpolation takes place in linear RGB + /// colorspace. Both `self` and `rhs` will be converted as necessary, before the operation. + #[doc(alias = "mix")] + #[inline] + pub fn lerp(&self, rhs: Color, s: f32) -> Color { + let linear_self = self.as_rgba_linear(); + let linear_rhs = rhs.as_rgba_linear(); + linear_self * (1.0 - s) + linear_rhs * s + } } impl Default for Color {