bevy_color/color_ops.rs
1use bevy_math::{ops, Vec3, Vec4};
2
3/// Methods for changing the luminance of a color. Note that these methods are not
4/// guaranteed to produce consistent results across color spaces,
5/// but will be within a given space.
6pub trait Luminance: Sized {
7 /// Return the luminance of this color (0.0 - 1.0).
8 fn luminance(&self) -> f32;
9
10 /// Return a new version of this color with the given luminance. The resulting color will
11 /// be clamped to the valid range for the color space; for some color spaces, clamping
12 /// may cause the hue or chroma to change.
13 fn with_luminance(&self, value: f32) -> Self;
14
15 /// Return a darker version of this color. The `amount` should be between 0.0 and 1.0.
16 /// The amount represents an absolute decrease in luminance, and is distributive:
17 /// `color.darker(a).darker(b) == color.darker(a + b)`. Colors are clamped to black
18 /// if the amount would cause them to go below black.
19 ///
20 /// For a relative decrease in luminance, you can simply `mix()` with black.
21 fn darker(&self, amount: f32) -> Self;
22
23 /// Return a lighter version of this color. The `amount` should be between 0.0 and 1.0.
24 /// The amount represents an absolute increase in luminance, and is distributive:
25 /// `color.lighter(a).lighter(b) == color.lighter(a + b)`. Colors are clamped to white
26 /// if the amount would cause them to go above white.
27 ///
28 /// For a relative increase in luminance, you can simply `mix()` with white.
29 fn lighter(&self, amount: f32) -> Self;
30}
31
32/// Linear interpolation of two colors within a given color space.
33pub trait Mix: Sized {
34 /// Linearly interpolate between this and another color, by factor.
35 /// Factor should be between 0.0 and 1.0.
36 fn mix(&self, other: &Self, factor: f32) -> Self;
37
38 /// Linearly interpolate between this and another color, by factor, storing the result
39 /// in this color. Factor should be between 0.0 and 1.0.
40 fn mix_assign(&mut self, other: Self, factor: f32) {
41 *self = self.mix(&other, factor);
42 }
43}
44
45/// Trait for returning a grayscale color of a provided lightness.
46pub trait Gray: Mix + Sized {
47 /// A pure black color.
48 const BLACK: Self;
49 /// A pure white color.
50 const WHITE: Self;
51
52 /// Returns a grey color with the provided lightness from (0.0 - 1.0). 0 is black, 1 is white.
53 fn gray(lightness: f32) -> Self {
54 Self::BLACK.mix(&Self::WHITE, lightness)
55 }
56}
57
58/// Methods for manipulating alpha values.
59pub trait Alpha: Sized {
60 /// Return a new version of this color with the given alpha value.
61 fn with_alpha(&self, alpha: f32) -> Self;
62
63 /// Return the alpha component of this color.
64 fn alpha(&self) -> f32;
65
66 /// Sets the alpha component of this color.
67 fn set_alpha(&mut self, alpha: f32);
68
69 /// Is the alpha component of this color less than or equal to 0.0?
70 fn is_fully_transparent(&self) -> bool {
71 self.alpha() <= 0.0
72 }
73
74 /// Is the alpha component of this color greater than or equal to 1.0?
75 fn is_fully_opaque(&self) -> bool {
76 self.alpha() >= 1.0
77 }
78}
79
80impl Alpha for f32 {
81 fn with_alpha(&self, alpha: f32) -> Self {
82 alpha
83 }
84
85 fn alpha(&self) -> f32 {
86 *self
87 }
88
89 fn set_alpha(&mut self, alpha: f32) {
90 *self = alpha;
91 }
92}
93
94/// Trait for manipulating the hue of a color.
95pub trait Hue: Sized {
96 /// Return a new version of this color with the hue channel set to the given value.
97 fn with_hue(&self, hue: f32) -> Self;
98
99 /// Return the hue of this color [0.0, 360.0].
100 fn hue(&self) -> f32;
101
102 /// Sets the hue of this color.
103 fn set_hue(&mut self, hue: f32);
104
105 /// Return a new version of this color with the hue channel rotated by the given degrees.
106 fn rotate_hue(&self, degrees: f32) -> Self {
107 let rotated_hue = ops::rem_euclid(self.hue() + degrees, 360.);
108 self.with_hue(rotated_hue)
109 }
110}
111
112/// Trait for manipulating the saturation of a color.
113///
114/// When working with color spaces that do not have native saturation components
115/// the operations are performed in [`Hsla`](`crate::Hsla`).
116pub trait Saturation: Sized {
117 /// Return a new version of this color with the saturation channel set to the given value.
118 fn with_saturation(&self, saturation: f32) -> Self;
119
120 /// Return the saturation of this color [0.0, 1.0].
121 fn saturation(&self) -> f32;
122
123 /// Sets the saturation of this color.
124 fn set_saturation(&mut self, saturation: f32);
125}
126
127/// Trait with methods for converting colors to non-color types
128pub trait ColorToComponents {
129 /// Convert to an f32 array
130 fn to_f32_array(self) -> [f32; 4];
131 /// Convert to an f32 array without the alpha value
132 fn to_f32_array_no_alpha(self) -> [f32; 3];
133 /// Convert to a Vec4
134 fn to_vec4(self) -> Vec4;
135 /// Convert to a Vec3
136 fn to_vec3(self) -> Vec3;
137 /// Convert from an f32 array
138 fn from_f32_array(color: [f32; 4]) -> Self;
139 /// Convert from an f32 array without the alpha value
140 fn from_f32_array_no_alpha(color: [f32; 3]) -> Self;
141 /// Convert from a Vec4
142 fn from_vec4(color: Vec4) -> Self;
143 /// Convert from a Vec3
144 fn from_vec3(color: Vec3) -> Self;
145}
146
147/// Trait with methods for converting colors to packed non-color types
148pub trait ColorToPacked {
149 /// Convert to [u8; 4] where that makes sense (Srgba is most relevant)
150 fn to_u8_array(self) -> [u8; 4];
151 /// Convert to [u8; 3] where that makes sense (Srgba is most relevant)
152 fn to_u8_array_no_alpha(self) -> [u8; 3];
153 /// Convert from [u8; 4] where that makes sense (Srgba is most relevant)
154 fn from_u8_array(color: [u8; 4]) -> Self;
155 /// Convert to [u8; 3] where that makes sense (Srgba is most relevant)
156 fn from_u8_array_no_alpha(color: [u8; 3]) -> Self;
157}
158
159/// Utility function for interpolating hue values. This ensures that the interpolation
160/// takes the shortest path around the color wheel, and that the result is always between
161/// 0 and 360.
162pub(crate) fn lerp_hue(a: f32, b: f32, t: f32) -> f32 {
163 let diff = ops::rem_euclid(b - a + 180.0, 360.) - 180.;
164 ops::rem_euclid(a + diff * t, 360.)
165}
166
167#[cfg(test)]
168mod tests {
169 use core::fmt::Debug;
170
171 use super::*;
172 use crate::{testing::assert_approx_eq, Hsla};
173
174 #[test]
175 fn test_rotate_hue() {
176 let hsla = Hsla::hsl(180.0, 1.0, 0.5);
177 assert_eq!(hsla.rotate_hue(90.0), Hsla::hsl(270.0, 1.0, 0.5));
178 assert_eq!(hsla.rotate_hue(-90.0), Hsla::hsl(90.0, 1.0, 0.5));
179 assert_eq!(hsla.rotate_hue(180.0), Hsla::hsl(0.0, 1.0, 0.5));
180 assert_eq!(hsla.rotate_hue(-180.0), Hsla::hsl(0.0, 1.0, 0.5));
181 assert_eq!(hsla.rotate_hue(0.0), hsla);
182 assert_eq!(hsla.rotate_hue(360.0), hsla);
183 assert_eq!(hsla.rotate_hue(-360.0), hsla);
184 }
185
186 #[test]
187 fn test_hue_wrap() {
188 assert_approx_eq!(lerp_hue(10., 20., 0.25), 12.5, 0.001);
189 assert_approx_eq!(lerp_hue(10., 20., 0.5), 15., 0.001);
190 assert_approx_eq!(lerp_hue(10., 20., 0.75), 17.5, 0.001);
191
192 assert_approx_eq!(lerp_hue(20., 10., 0.25), 17.5, 0.001);
193 assert_approx_eq!(lerp_hue(20., 10., 0.5), 15., 0.001);
194 assert_approx_eq!(lerp_hue(20., 10., 0.75), 12.5, 0.001);
195
196 assert_approx_eq!(lerp_hue(10., 350., 0.25), 5., 0.001);
197 assert_approx_eq!(lerp_hue(10., 350., 0.5), 0., 0.001);
198 assert_approx_eq!(lerp_hue(10., 350., 0.75), 355., 0.001);
199
200 assert_approx_eq!(lerp_hue(350., 10., 0.25), 355., 0.001);
201 assert_approx_eq!(lerp_hue(350., 10., 0.5), 0., 0.001);
202 assert_approx_eq!(lerp_hue(350., 10., 0.75), 5., 0.001);
203 }
204
205 fn verify_gray<Col>()
206 where
207 Col: Gray + Debug + PartialEq,
208 {
209 assert_eq!(Col::gray(0.), Col::BLACK);
210 assert_eq!(Col::gray(1.), Col::WHITE);
211 }
212
213 #[test]
214 fn test_gray() {
215 verify_gray::<Hsla>();
216 verify_gray::<crate::Hsva>();
217 verify_gray::<crate::Hwba>();
218 verify_gray::<crate::Laba>();
219 verify_gray::<crate::Lcha>();
220 verify_gray::<crate::LinearRgba>();
221 verify_gray::<crate::Oklaba>();
222 verify_gray::<crate::Oklcha>();
223 verify_gray::<crate::Xyza>();
224 }
225}