bevy_color/
color_range.rs

1use std::ops::Range;
2
3use crate::Mix;
4
5/// Represents a range of colors that can be linearly interpolated, defined by a start and
6/// end point which must be in the same color space. It works for any color type that
7/// implements [`Mix`].
8///
9/// This is useful for defining gradients or animated color transitions.
10pub trait ColorRange<T: Mix> {
11    /// Get the color value at the given interpolation factor, which should be between 0.0 (start)
12    /// and 1.0 (end).
13    fn at(&self, factor: f32) -> T;
14}
15
16impl<T: Mix> ColorRange<T> for Range<T> {
17    fn at(&self, factor: f32) -> T {
18        self.start.mix(&self.end, factor)
19    }
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25    use crate::palettes::basic;
26    use crate::{LinearRgba, Srgba};
27
28    #[test]
29    fn test_color_range() {
30        let range = basic::RED..basic::BLUE;
31        assert_eq!(range.at(0.0), basic::RED);
32        assert_eq!(range.at(0.5), Srgba::new(0.5, 0.0, 0.5, 1.0));
33        assert_eq!(range.at(1.0), basic::BLUE);
34
35        let lred: LinearRgba = basic::RED.into();
36        let lblue: LinearRgba = basic::BLUE.into();
37
38        let range = lred..lblue;
39        assert_eq!(range.at(0.0), lred);
40        assert_eq!(range.at(0.5), LinearRgba::new(0.5, 0.0, 0.5, 1.0));
41        assert_eq!(range.at(1.0), lblue);
42    }
43}