Skip to content

Add methods for getting and setting individual color channels for all color spaces. #7759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
237 changes: 231 additions & 6 deletions crates/bevy_render/src/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl Color {
}
}

/// Set red in sRGB colorspace.
/// Set red in sRGB colorspace, `self` is modified to variant [`Color::Rgba`] and returned.
pub fn set_r(&mut self, r: f32) -> &mut Self {
*self = self.as_rgba();
match self {
Expand All @@ -402,14 +402,14 @@ impl Color {
self
}

/// Returns this color with red set to a new value in sRGB colorspace.
/// Returns this color with red set to a new value in sRGB colorspace, returned value is of variant [`Color::Rgba`].
#[must_use]
pub fn with_r(mut self, r: f32) -> Self {
self.set_r(r);
self
}

/// Set green in sRGB colorspace.
/// Set green in sRGB colorspace, `self` is modified to variant [`Color::Rgba`] and returned.
pub fn set_g(&mut self, g: f32) -> &mut Self {
*self = self.as_rgba();
match self {
Expand All @@ -419,14 +419,14 @@ impl Color {
self
}

/// Returns this color with green set to a new value in sRGB colorspace.
/// Returns this color with green set to a new value in sRGB colorspace, returned value is of variant [`Color::Rgba`].
#[must_use]
pub fn with_g(mut self, g: f32) -> Self {
self.set_g(g);
self
}

/// Set blue in sRGB colorspace.
/// Set blue in sRGB colorspace, `self` is modified to variant [`Color::Rgba`] and returned.
pub fn set_b(&mut self, b: f32) -> &mut Self {
*self = self.as_rgba();
match self {
Expand All @@ -436,13 +436,238 @@ impl Color {
self
}

/// Returns this color with blue set to a new value in sRGB colorspace.
/// Returns this color with blue set to a new value in sRGB colorspace, returned value is of variant [`Color::Rgba`].
#[must_use]
pub fn with_b(mut self, b: f32) -> Self {
self.set_b(b);
self
}

/// Get the red in rgb linear colorspace.
pub fn rgb_linear_r(&self) -> f32 {
match self.as_rgba_linear() {
Color::RgbaLinear { red, .. } => red,
_ => unreachable!(),
}
}

/// Get the green in rgb linear colorspace.
pub fn rgb_linear_g(&self) -> f32 {
match self.as_rgba_linear() {
Color::RgbaLinear { green, .. } => green,
_ => unreachable!(),
}
}

/// Get the blue in rgb linear colorspace.
pub fn rgb_linear_b(&self) -> f32 {
match self.as_rgba_linear() {
Color::RgbaLinear { blue, .. } => blue,
_ => unreachable!(),
}
}

/// Set the red in rgb linear colorspace, `self` is modified to variant [`Color::RgbaLinear`] and returned.
pub fn set_rgb_linear_r(&mut self, r: f32) -> &mut Self {
*self = self.as_rgba_linear();
match self {
Color::RgbaLinear { red, .. } => *red = r,
_ => unreachable!(),
}
self
}

/// Returns this color with red set to a new value in rgb linear colorspace, returned value is of variant [`Color::RgbaLinear`].
#[must_use]
pub fn with_rgb_linear_r(mut self, r: f32) -> Self {
self.set_rgb_linear_r(r);
self
}

/// Set the green in rgb linear colorspace, `self` is modified to variant [`Color::RgbaLinear`] and returned.
pub fn set_rgb_linear_g(&mut self, g: f32) -> &mut Self {
*self = self.as_rgba_linear();
match self {
Color::RgbaLinear { green, .. } => *green = g,
_ => unreachable!(),
}
self
}

/// Returns this color with green set to a new value in rgb linear colorspace, returned value is of variant [`Color::RgbaLinear`].
#[must_use]
pub fn with_rgb_linear_g(mut self, g: f32) -> Self {
self.set_rgb_linear_g(g);
self
}

/// Set the blue in rgb linear colorspace, `self` is modified to variant [`Color::RgbaLinear`] and returned.
pub fn set_rgb_linear_b(&mut self, b: f32) -> &mut Self {
*self = self.as_rgba_linear();
match self {
Color::RgbaLinear { blue, .. } => *blue = b,
_ => unreachable!(),
}
self
}

/// Returns this color with blue set to a new value in rgb linear colorspace, returned value is of variant [`Color::RgbaLinear`].
#[must_use]
pub fn with_rgb_linear_b(mut self, b: f32) -> Self {
self.set_rgb_linear_b(b);
self
}

/// Get the hue in hsl colorspace.
pub fn hsl_hue(&self) -> f32 {
match self.as_hsla() {
Color::Hsla { hue, .. } => hue,
_ => unreachable!(),
}
}

/// Get the lightness in hsl colorspace.
pub fn hsl_lightness(&self) -> f32 {
match self.as_hsla() {
Color::Hsla { lightness, .. } => lightness,
_ => unreachable!(),
}
}

/// Get the saturation in hsl colorspace.
pub fn hsl_saturation(&self) -> f32 {
match self.as_hsla() {
Color::Hsla { saturation, .. } => saturation,
_ => unreachable!(),
}
}

/// Set the hue in hsl colorspace, `self` is modified to variant [`Color::Hsla`] and returned.
pub fn set_hsl_hue(&mut self, hue: f32) -> &mut Self {
*self = self.as_hsla();
match self {
Color::Hsla { hue: h, .. } => *h = hue,
_ => unreachable!(),
}
self
}

/// Returns this color with hue set to a new value in hsl colorspace, returned value is of variant [`Color::Hsla`].
#[must_use]
pub fn with_hsl_hue(mut self, hue: f32) -> Self {
self.set_hsl_hue(hue);
self
}

/// Set the saturation in hsl colorspace, `self` is modified to variant [`Color::Hsla`] and returned.
pub fn set_hsl_saturation(&mut self, saturation: f32) -> &mut Self {
*self = self.as_hsla();
match self {
Color::Hsla { saturation: s, .. } => *s = saturation,
_ => unreachable!(),
}
self
}

/// Returns this color with saturation set to a new value in hsl colorspace, returned value is of variant [`Color::Hsla`].
#[must_use]
pub fn with_hsl_saturation(mut self, saturation: f32) -> Self {
self.set_hsl_saturation(saturation);
self
}

/// Set the lightness in hsl colorspace, `self` is modified to variant [`Color::Hsla`] and returned.
pub fn set_hsl_lightness(&mut self, lightness: f32) -> &mut Self {
*self = self.as_hsla();
match self {
Color::Hsla { lightness: l, .. } => *l = lightness,
_ => unreachable!(),
}
self
}

/// Returns this color with lightness set to a new value in hsl colorspace, returned value is of variant [`Color::Hsla`].
#[must_use]
pub fn with_hsl_lightness(mut self, lightness: f32) -> Self {
self.set_hsl_lightness(lightness);
self
}

/// Get the lightness in lch colorspace.
pub fn lch_lightness(&self) -> f32 {
match self.as_lcha() {
Color::Lcha { lightness, .. } => lightness,
_ => unreachable!(),
}
}

/// Get the chroma in hsl colorspace.
pub fn lch_chroma(&self) -> f32 {
match self.as_lcha() {
Color::Lcha { chroma, .. } => chroma,
_ => unreachable!(),
}
}

/// Get the hue in lch colorspace.
pub fn lch_hue(&self) -> f32 {
match self.as_lcha() {
Color::Lcha { hue, .. } => hue,
_ => unreachable!(),
}
}

/// Set the lightness in lch colorspace, `self` is modified to variant [`Color::Lcha`] and returned.
pub fn set_lch_lightness(&mut self, lightness: f32) -> &mut Self {
*self = self.as_lcha();
match self {
Color::Lcha { lightness: l, .. } => *l = lightness,
_ => unreachable!(),
}
self
}

/// Returns this color with lightness set to a new value in lch colorspace, returned value is of variant [`Color::Lcha`].
#[must_use]
pub fn with_lch_lightness(mut self, lightness: f32) -> Self {
self.set_lch_lightness(lightness);
self
}

/// Set the chroma in lch colorspace, `self` is modified to variant [`Color::Lcha`] and returned.
pub fn set_lch_chroma(&mut self, chroma: f32) -> &mut Self {
*self = self.as_lcha();
match self {
Color::Lcha { chroma: c, .. } => *c = chroma,
_ => unreachable!(),
}
self
}

/// Returns this color with chroma set to a new value in lch colorspace, returned value is of variant [`Color::Lcha`].
#[must_use]
pub fn with_lch_chroma(mut self, chroma: f32) -> Self {
self.set_lch_chroma(chroma);
self
}

/// Set the hue in hsl colorspace, `self` is modified to variant [`Color::Lcha`] and returned.
pub fn set_lch_hue(&mut self, hue: f32) -> &mut Self {
*self = self.as_lcha();
match self {
Color::Lcha { hue: h, .. } => *h = hue,
_ => unreachable!(),
}
self
}

/// Returns this color with hue set to a new value in hsl colorspace, returned value is of variant [`Color::Lcha`].
#[must_use]
pub fn with_lch_hue(mut self, hue: f32) -> Self {
self.set_lch_hue(hue);
self
}

/// Get alpha.
#[inline(always)]
pub fn a(&self) -> f32 {
Expand Down