Skip to content

Commit 8184591

Browse files
authored
Added StableInterpolate implementations for linear colors. (#18601)
# Objective Colors currently do not implement `StableInterpolate`, which makes them ineligible for functions like `smooth_nudge` and make some generic APIs awkward. ## Solution Implemented `StableInterpolate` for linear color types that should be uncontroversial. Non-linear types like `Hsl` are not implemented in this PR. ## Testing Added a test that checks implementations are correct.
1 parent 798e1c5 commit 8184591

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! TODO: Implement for non-linear colors.
2+
3+
#[cfg(test)]
4+
mod test {
5+
use bevy_math::StableInterpolate;
6+
7+
use crate::{Gray, Laba, LinearRgba, Oklaba, Srgba, Xyza};
8+
9+
#[test]
10+
pub fn test_color_stable_interpolate() {
11+
let b = Srgba::BLACK;
12+
let w = Srgba::WHITE;
13+
assert_eq!(
14+
b.interpolate_stable(&w, 0.5),
15+
Srgba::new(0.5, 0.5, 0.5, 1.0),
16+
);
17+
18+
let b = LinearRgba::BLACK;
19+
let w = LinearRgba::WHITE;
20+
assert_eq!(
21+
b.interpolate_stable(&w, 0.5),
22+
LinearRgba::new(0.5, 0.5, 0.5, 1.0),
23+
);
24+
25+
let b = Xyza::BLACK;
26+
let w = Xyza::WHITE;
27+
assert_eq!(b.interpolate_stable(&w, 0.5), Xyza::gray(0.5),);
28+
29+
let b = Laba::BLACK;
30+
let w = Laba::WHITE;
31+
assert_eq!(b.interpolate_stable(&w, 0.5), Laba::new(0.5, 0.0, 0.0, 1.0),);
32+
33+
let b = Oklaba::BLACK;
34+
let w = Oklaba::WHITE;
35+
assert_eq!(b.interpolate_stable(&w, 0.5), Oklaba::gray(0.5),);
36+
}
37+
}

crates/bevy_color/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ mod color_range;
105105
mod hsla;
106106
mod hsva;
107107
mod hwba;
108+
mod interpolate;
108109
mod laba;
109110
mod lcha;
110111
mod linear_rgba;
@@ -265,6 +266,12 @@ macro_rules! impl_componentwise_vector_space {
265266
$($element: 0.0,)+
266267
};
267268
}
269+
270+
impl bevy_math::StableInterpolate for $ty {
271+
fn interpolate_stable(&self, other: &Self, t: f32) -> Self {
272+
bevy_math::VectorSpace::lerp(*self, *other, t)
273+
}
274+
}
268275
};
269276
}
270277

0 commit comments

Comments
 (0)