Skip to content

Commit

Permalink
Adding "fluent" methods to modify color channels. (#12099)
Browse files Browse the repository at this point in the history
Fixes #12075
  • Loading branch information
viridia authored Feb 24, 2024
1 parent e689d46 commit 10aef14
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
15 changes: 15 additions & 0 deletions crates/bevy_color/src/hsla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions crates/bevy_color/src/lcha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions crates/bevy_color/src/linear_rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions crates/bevy_color/src/oklaba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions crates/bevy_color/src/srgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion crates/bevy_color/src/xyza.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,29 @@ 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,
z,
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 {
Expand Down

0 comments on commit 10aef14

Please sign in to comment.