From 75ce42b39a66592c9787329e1d63285ac88bcd8d Mon Sep 17 00:00:00 2001 From: Talin Date: Sat, 24 Feb 2024 11:57:00 -0800 Subject: [PATCH] Adding "fluent" methods to modify color channels. Fixes #12075 --- crates/bevy_color/src/hsla.rs | 15 +++++++++++++++ crates/bevy_color/src/lcha.rs | 15 +++++++++++++++ crates/bevy_color/src/linear_rgba.rs | 15 +++++++++++++++ crates/bevy_color/src/oklaba.rs | 15 +++++++++++++++ crates/bevy_color/src/srgba.rs | 15 +++++++++++++++ crates/bevy_color/src/xyza.rs | 17 ++++++++++++++++- 6 files changed, 91 insertions(+), 1 deletion(-) diff --git a/crates/bevy_color/src/hsla.rs b/crates/bevy_color/src/hsla.rs index c6bd8a2a64ba1..465000bebab3b 100644 --- a/crates/bevy_color/src/hsla.rs +++ b/crates/bevy_color/src/hsla.rs @@ -47,6 +47,21 @@ impl Hsla { pub const fn hsl(hue: f32, saturation: f32, lightness: f32) -> Self { Self::new(hue, saturation, lightness, 1.0) } + + /// Return a copy of this color with the hue channel set to the given value. + pub const fn with_hue(self, hue: f32) -> Self { + Self { hue, ..self } + } + + /// Return a copy of this color with the saturation channel set to the given value. + pub const fn with_saturation(self, saturation: f32) -> Self { + Self { saturation, ..self } + } + + /// Return a copy of this color with the lightness channel set to the given value. + pub const fn with_lightness(self, lightness: f32) -> Self { + Self { lightness, ..self } + } } impl Default for Hsla { diff --git a/crates/bevy_color/src/lcha.rs b/crates/bevy_color/src/lcha.rs index 7cc6cdb180b2c..885fb1280053c 100644 --- a/crates/bevy_color/src/lcha.rs +++ b/crates/bevy_color/src/lcha.rs @@ -52,6 +52,21 @@ impl Lcha { alpha: 1.0, } } + + /// Return a copy of this color with the hue channel set to the given value. + pub const fn with_hue(self, hue: f32) -> Self { + Self { hue, ..self } + } + + /// Return a copy of this color with the chroma channel set to the given value. + pub const fn with_chroma(self, chroma: f32) -> Self { + Self { chroma, ..self } + } + + /// Return a copy of this color with the lightness channel set to the given value. + pub const fn with_lightness(self, lightness: f32) -> Self { + Self { lightness, ..self } + } } impl Default for Lcha { diff --git a/crates/bevy_color/src/linear_rgba.rs b/crates/bevy_color/src/linear_rgba.rs index 942d7951fbb84..d61c0d16791a8 100644 --- a/crates/bevy_color/src/linear_rgba.rs +++ b/crates/bevy_color/src/linear_rgba.rs @@ -50,6 +50,21 @@ impl LinearRgba { } } + /// Return a copy of this color with the red channel set to the given value. + pub const fn with_red(self, red: f32) -> Self { + Self { red, ..self } + } + + /// Return a copy of this color with the green channel set to the given value. + pub const fn with_green(self, green: f32) -> Self { + Self { green, ..self } + } + + /// Return a copy of this color with the blue channel set to the given value. + pub const fn with_blue(self, blue: f32) -> Self { + Self { blue, ..self } + } + /// Make the color lighter or darker by some amount fn adjust_lightness(&mut self, amount: f32) { let luminance = self.luminance(); diff --git a/crates/bevy_color/src/oklaba.rs b/crates/bevy_color/src/oklaba.rs index 062a502291da3..35cb3683d0ccf 100644 --- a/crates/bevy_color/src/oklaba.rs +++ b/crates/bevy_color/src/oklaba.rs @@ -50,6 +50,21 @@ impl Oklaba { alpha: 1.0, } } + + /// Return a copy of this color with the 'l' channel set to the given value. + pub const fn with_l(self, l: f32) -> Self { + Self { l, ..self } + } + + /// Return a copy of this color with the 'a' channel set to the given value. + pub const fn with_a(self, a: f32) -> Self { + Self { a, ..self } + } + + /// Return a copy of this color with the 'b' channel set to the given value. + pub const fn with_b(self, b: f32) -> Self { + Self { b, ..self } + } } impl Default for Oklaba { diff --git a/crates/bevy_color/src/srgba.rs b/crates/bevy_color/src/srgba.rs index 31f64bde6f560..f75d1cf99f5b6 100644 --- a/crates/bevy_color/src/srgba.rs +++ b/crates/bevy_color/src/srgba.rs @@ -67,6 +67,21 @@ impl Srgba { } } + /// Return a copy of this color with the red channel set to the given value. + pub const fn with_red(self, red: f32) -> Self { + Self { red, ..self } + } + + /// Return a copy of this color with the green channel set to the given value. + pub const fn with_green(self, green: f32) -> Self { + Self { green, ..self } + } + + /// Return a copy of this color with the blue channel set to the given value. + pub const fn with_blue(self, blue: f32) -> Self { + Self { blue, ..self } + } + /// New `Srgba` from a CSS-style hexadecimal string. /// /// # Examples diff --git a/crates/bevy_color/src/xyza.rs b/crates/bevy_color/src/xyza.rs index 3d6420365bd62..b8457724f64a1 100644 --- a/crates/bevy_color/src/xyza.rs +++ b/crates/bevy_color/src/xyza.rs @@ -39,7 +39,7 @@ impl Xyza { /// * `x` - x-axis. [0.0, 1.0] /// * `y` - y-axis. [0.0, 1.0] /// * `z` - z-axis. [0.0, 1.0] - pub const fn rgb(x: f32, y: f32, z: f32) -> Self { + pub const fn xyz(x: f32, y: f32, z: f32) -> Self { Self { x, y, @@ -47,6 +47,21 @@ impl Xyza { alpha: 1.0, } } + + /// Return a copy of this color with the 'x' channel set to the given value. + pub const fn with_x(self, x: f32) -> Self { + Self { x, ..self } + } + + /// Return a copy of this color with the 'y' channel set to the given value. + pub const fn with_y(self, y: f32) -> Self { + Self { y, ..self } + } + + /// Return a copy of this color with the 'z' channel set to the given value. + pub const fn with_z(self, z: f32) -> Self { + Self { z, ..self } + } } impl Default for Xyza {