Skip to content

Commit

Permalink
Remove ClampColor (#13307)
Browse files Browse the repository at this point in the history
# Objective

- Fixes #12543

## Solution

- Removed `ClampColor`

## Testing

- CI Passed

---

## Migration Guide

Manually clamp the various colour components yourself if this behaviour
is still required.

```rust
fn clamped_srgba(color: Srgba) -> Srgba {
    Srgba {
        red: color.red.clamp(0., 1.),
        green: color.green.clamp(0., 1.),
        blue: color.blue.clamp(0., 1.),
        alpha: color.alpha.clamp(0., 1.),
    }
}
```
  • Loading branch information
bushrat011899 committed May 10, 2024
1 parent 4b61bbe commit dcb8a13
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 391 deletions.
6 changes: 3 additions & 3 deletions crates/bevy_animation/src/animatable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::util;
use bevy_color::{ClampColor, Laba, LinearRgba, Oklaba, Srgba, Xyza};
use bevy_color::{Laba, LinearRgba, Oklaba, Srgba, Xyza};
use bevy_ecs::world::World;
use bevy_math::*;
use bevy_reflect::Reflect;
Expand Down Expand Up @@ -63,7 +63,7 @@ macro_rules! impl_color_animatable {
#[inline]
fn interpolate(a: &Self, b: &Self, t: f32) -> Self {
let value = *a * (1. - t) + *b * t;
value.clamped()
value
}

#[inline]
Expand All @@ -76,7 +76,7 @@ macro_rules! impl_color_animatable {
value = Self::interpolate(&value, &input.value, input.weight);
}
}
value.clamped()
value
}
}
};
Expand Down
17 changes: 0 additions & 17 deletions crates/bevy_color/src/color_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,6 @@ pub trait Hue: Sized {
}
}

/// Trait with methods for asserting a colorspace is within bounds.
///
/// During ordinary usage (e.g. reading images from disk, rendering images, picking colors for UI), colors should always be within their ordinary bounds (such as 0 to 1 for RGB colors).
/// However, some applications, such as high dynamic range rendering or bloom rely on unbounded colors to naturally represent a wider array of choices.
pub trait ClampColor: Sized {
/// Return a new version of this color clamped, with all fields in bounds.
fn clamped(&self) -> Self;

/// Changes all the fields of this color to ensure they are within bounds.
fn clamp(&mut self) {
*self = self.clamped();
}

/// Are all the fields of this color in bounds?
fn is_within_bounds(&self) -> bool;
}

/// Trait with methods for converting colors to non-color types
pub trait ColorToComponents {
/// Convert to an f32 array
Expand Down
37 changes: 1 addition & 36 deletions crates/bevy_color/src/hsla.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
Alpha, ClampColor, ColorToComponents, Hsva, Hue, Hwba, Lcha, LinearRgba, Luminance, Mix, Srgba,
Alpha, ColorToComponents, Hsva, Hue, Hwba, Lcha, LinearRgba, Luminance, Mix, Srgba,
StandardColor, Xyza,
};
use bevy_math::{Vec3, Vec4};
Expand Down Expand Up @@ -178,24 +178,6 @@ impl Luminance for Hsla {
}
}

impl ClampColor for Hsla {
fn clamped(&self) -> Self {
Self {
hue: self.hue.rem_euclid(360.),
saturation: self.saturation.clamp(0., 1.),
lightness: self.lightness.clamp(0., 1.),
alpha: self.alpha.clamp(0., 1.),
}
}

fn is_within_bounds(&self) -> bool {
(0. ..=360.).contains(&self.hue)
&& (0. ..=1.).contains(&self.saturation)
&& (0. ..=1.).contains(&self.lightness)
&& (0. ..=1.).contains(&self.alpha)
}
}

impl ColorToComponents for Hsla {
fn to_f32_array(self) -> [f32; 4] {
[self.hue, self.saturation, self.lightness, self.alpha]
Expand Down Expand Up @@ -440,21 +422,4 @@ mod tests {
assert_approx_eq!(color.hue, reference.hue, 0.001);
}
}

#[test]
fn test_clamp() {
let color_1 = Hsla::hsl(361., 2., -1.);
let color_2 = Hsla::hsl(250.2762, 1., 0.67);
let mut color_3 = Hsla::hsl(-50., 1., 1.);

assert!(!color_1.is_within_bounds());
assert_eq!(color_1.clamped(), Hsla::hsl(1., 1., 0.));

assert!(color_2.is_within_bounds());
assert_eq!(color_2, color_2.clamped());

color_3.clamp();
assert!(color_3.is_within_bounds());
assert_eq!(color_3, Hsla::hsl(310., 1., 1.));
}
}
38 changes: 1 addition & 37 deletions crates/bevy_color/src/hsva.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
Alpha, ClampColor, ColorToComponents, Hue, Hwba, Lcha, LinearRgba, Mix, Srgba, StandardColor,
Xyza,
Alpha, ColorToComponents, Hue, Hwba, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza,
};
use bevy_math::{Vec3, Vec4};
use bevy_reflect::prelude::*;
Expand Down Expand Up @@ -124,24 +123,6 @@ impl Hue for Hsva {
}
}

impl ClampColor for Hsva {
fn clamped(&self) -> Self {
Self {
hue: self.hue.rem_euclid(360.),
saturation: self.saturation.clamp(0., 1.),
value: self.value.clamp(0., 1.),
alpha: self.alpha.clamp(0., 1.),
}
}

fn is_within_bounds(&self) -> bool {
(0. ..=360.).contains(&self.hue)
&& (0. ..=1.).contains(&self.saturation)
&& (0. ..=1.).contains(&self.value)
&& (0. ..=1.).contains(&self.alpha)
}
}

impl From<Hsva> for Hwba {
fn from(
Hsva {
Expand Down Expand Up @@ -316,21 +297,4 @@ mod tests {
assert_approx_eq!(color.hsv.alpha, hsv2.alpha, 0.001);
}
}

#[test]
fn test_clamp() {
let color_1 = Hsva::hsv(361., 2., -1.);
let color_2 = Hsva::hsv(250.2762, 1., 0.67);
let mut color_3 = Hsva::hsv(-50., 1., 1.);

assert!(!color_1.is_within_bounds());
assert_eq!(color_1.clamped(), Hsva::hsv(1., 1., 0.));

assert!(color_2.is_within_bounds());
assert_eq!(color_2, color_2.clamped());

color_3.clamp();
assert!(color_3.is_within_bounds());
assert_eq!(color_3, Hsva::hsv(310., 1., 1.));
}
}
39 changes: 1 addition & 38 deletions crates/bevy_color/src/hwba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
//! in [_HWB - A More Intuitive Hue-Based Color Model_] by _Smith et al_.
//!
//! [_HWB - A More Intuitive Hue-Based Color Model_]: https://web.archive.org/web/20240226005220/http://alvyray.com/Papers/CG/HWB_JGTv208.pdf
use crate::{
Alpha, ClampColor, ColorToComponents, Hue, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza,
};
use crate::{Alpha, ColorToComponents, Hue, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza};
use bevy_math::{Vec3, Vec4};
use bevy_reflect::prelude::*;

Expand Down Expand Up @@ -127,24 +125,6 @@ impl Hue for Hwba {
}
}

impl ClampColor for Hwba {
fn clamped(&self) -> Self {
Self {
hue: self.hue.rem_euclid(360.),
whiteness: self.whiteness.clamp(0., 1.),
blackness: self.blackness.clamp(0., 1.),
alpha: self.alpha.clamp(0., 1.),
}
}

fn is_within_bounds(&self) -> bool {
(0. ..=360.).contains(&self.hue)
&& (0. ..=1.).contains(&self.whiteness)
&& (0. ..=1.).contains(&self.blackness)
&& (0. ..=1.).contains(&self.alpha)
}
}

impl ColorToComponents for Hwba {
fn to_f32_array(self) -> [f32; 4] {
[self.hue, self.whiteness, self.blackness, self.alpha]
Expand Down Expand Up @@ -348,21 +328,4 @@ mod tests {
assert_approx_eq!(color.hwb.alpha, hwb2.alpha, 0.001);
}
}

#[test]
fn test_clamp() {
let color_1 = Hwba::hwb(361., 2., -1.);
let color_2 = Hwba::hwb(250.2762, 1., 0.67);
let mut color_3 = Hwba::hwb(-50., 1., 1.);

assert!(!color_1.is_within_bounds());
assert_eq!(color_1.clamped(), Hwba::hwb(1., 1., 0.));

assert!(color_2.is_within_bounds());
assert_eq!(color_2, color_2.clamped());

color_3.clamp();
assert!(color_3.is_within_bounds());
assert_eq!(color_3, Hwba::hwb(310., 1., 1.));
}
}
39 changes: 2 additions & 37 deletions crates/bevy_color/src/laba.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
impl_componentwise_vector_space, Alpha, ClampColor, ColorToComponents, Hsla, Hsva, Hwba,
LinearRgba, Luminance, Mix, Oklaba, Srgba, StandardColor, Xyza,
impl_componentwise_vector_space, Alpha, ColorToComponents, Hsla, Hsva, Hwba, LinearRgba,
Luminance, Mix, Oklaba, Srgba, StandardColor, Xyza,
};
use bevy_math::{Vec3, Vec4};
use bevy_reflect::prelude::*;
Expand Down Expand Up @@ -118,24 +118,6 @@ impl Alpha for Laba {
}
}

impl ClampColor for Laba {
fn clamped(&self) -> Self {
Self {
lightness: self.lightness.clamp(0., 1.5),
a: self.a.clamp(-1.5, 1.5),
b: self.b.clamp(-1.5, 1.5),
alpha: self.alpha.clamp(0., 1.),
}
}

fn is_within_bounds(&self) -> bool {
(0. ..=1.5).contains(&self.lightness)
&& (-1.5..=1.5).contains(&self.a)
&& (-1.5..=1.5).contains(&self.b)
&& (0. ..=1.).contains(&self.alpha)
}
}

impl Luminance for Laba {
#[inline]
fn with_luminance(&self, lightness: f32) -> Self {
Expand Down Expand Up @@ -432,21 +414,4 @@ mod tests {
assert_approx_eq!(color.lab.alpha, laba.alpha, 0.001);
}
}

#[test]
fn test_clamp() {
let color_1 = Laba::lab(-1., 2., -2.);
let color_2 = Laba::lab(1., 1.5, -1.2);
let mut color_3 = Laba::lab(-0.4, 1., 1.);

assert!(!color_1.is_within_bounds());
assert_eq!(color_1.clamped(), Laba::lab(0., 1.5, -1.5));

assert!(color_2.is_within_bounds());
assert_eq!(color_2, color_2.clamped());

color_3.clamp();
assert!(color_3.is_within_bounds());
assert_eq!(color_3, Laba::lab(0., 1., 1.));
}
}
38 changes: 1 addition & 37 deletions crates/bevy_color/src/lcha.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
Alpha, ClampColor, ColorToComponents, Hue, Laba, LinearRgba, Luminance, Mix, Srgba,
StandardColor, Xyza,
Alpha, ColorToComponents, Hue, Laba, LinearRgba, Luminance, Mix, Srgba, StandardColor, Xyza,
};
use bevy_math::{Vec3, Vec4};
use bevy_reflect::prelude::*;
Expand Down Expand Up @@ -186,24 +185,6 @@ impl Luminance for Lcha {
}
}

impl ClampColor for Lcha {
fn clamped(&self) -> Self {
Self {
lightness: self.lightness.clamp(0., 1.5),
chroma: self.chroma.clamp(0., 1.5),
hue: self.hue.rem_euclid(360.),
alpha: self.alpha.clamp(0., 1.),
}
}

fn is_within_bounds(&self) -> bool {
(0. ..=1.5).contains(&self.lightness)
&& (0. ..=1.5).contains(&self.chroma)
&& (0. ..=360.).contains(&self.hue)
&& (0. ..=1.).contains(&self.alpha)
}
}

impl ColorToComponents for Lcha {
fn to_f32_array(self) -> [f32; 4] {
[self.lightness, self.chroma, self.hue, self.alpha]
Expand Down Expand Up @@ -404,21 +385,4 @@ mod tests {
assert_approx_eq!(color.lch.alpha, lcha.alpha, 0.001);
}
}

#[test]
fn test_clamp() {
let color_1 = Lcha::lch(-1., 2., 400.);
let color_2 = Lcha::lch(1., 1.5, 249.54);
let mut color_3 = Lcha::lch(-0.4, 1., 1.);

assert!(!color_1.is_within_bounds());
assert_eq!(color_1.clamped(), Lcha::lch(0., 1.5, 40.));

assert!(color_2.is_within_bounds());
assert_eq!(color_2, color_2.clamped());

color_3.clamp();
assert!(color_3.is_within_bounds());
assert_eq!(color_3, Lcha::lch(0., 1., 1.));
}
}
39 changes: 2 additions & 37 deletions crates/bevy_color/src/linear_rgba.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
color_difference::EuclideanDistance, impl_componentwise_vector_space, Alpha, ClampColor,
ColorToComponents, Luminance, Mix, StandardColor,
color_difference::EuclideanDistance, impl_componentwise_vector_space, Alpha, ColorToComponents,
Luminance, Mix, StandardColor,
};
use bevy_math::{Vec3, Vec4};
use bevy_reflect::prelude::*;
Expand Down Expand Up @@ -263,24 +263,6 @@ impl EuclideanDistance for LinearRgba {
}
}

impl ClampColor for LinearRgba {
fn clamped(&self) -> Self {
Self {
red: self.red.clamp(0., 1.),
green: self.green.clamp(0., 1.),
blue: self.blue.clamp(0., 1.),
alpha: self.alpha.clamp(0., 1.),
}
}

fn is_within_bounds(&self) -> bool {
(0. ..=1.).contains(&self.red)
&& (0. ..=1.).contains(&self.green)
&& (0. ..=1.).contains(&self.blue)
&& (0. ..=1.).contains(&self.alpha)
}
}

impl ColorToComponents for LinearRgba {
fn to_f32_array(self) -> [f32; 4] {
[self.red, self.green, self.blue, self.alpha]
Expand Down Expand Up @@ -455,21 +437,4 @@ mod tests {
let twice_as_light = color.lighter(0.2);
assert!(lighter2.distance_squared(&twice_as_light) < 0.0001);
}

#[test]
fn test_clamp() {
let color_1 = LinearRgba::rgb(2., -1., 0.4);
let color_2 = LinearRgba::rgb(0.031, 0.749, 1.);
let mut color_3 = LinearRgba::rgb(-1., 1., 1.);

assert!(!color_1.is_within_bounds());
assert_eq!(color_1.clamped(), LinearRgba::rgb(1., 0., 0.4));

assert!(color_2.is_within_bounds());
assert_eq!(color_2, color_2.clamped());

color_3.clamp();
assert!(color_3.is_within_bounds());
assert_eq!(color_3, LinearRgba::rgb(0., 1., 1.));
}
}
Loading

0 comments on commit dcb8a13

Please sign in to comment.