Adding "fluent" methods to modify color channels. (#12099)

Fixes #12075
This commit is contained in:
Talin 2024-02-24 13:20:44 -08:00 committed by GitHub
parent e689d46015
commit 10aef141f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 91 additions and 1 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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();

View File

@ -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 {

View File

@ -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

View File

@ -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 {