Skip to content

Commit

Permalink
Migrate from LegacyColor to bevy_color::Color (#12163)
Browse files Browse the repository at this point in the history
# Objective

- As part of the migration process we need to a) see the end effect of
the migration on user ergonomics b) check for serious perf regressions
c) actually migrate the code
- To accomplish this, I'm going to attempt to migrate all of the
remaining user-facing usages of `LegacyColor` in one PR, being careful
to keep a clean commit history.
- Fixes #12056.

## Solution

I've chosen to use the polymorphic `Color` type as our standard
user-facing API.

- [x] Migrate `bevy_gizmos`.
- [x] Take `impl Into<Color>` in all `bevy_gizmos` APIs
- [x] Migrate sprites
- [x] Migrate UI
- [x] Migrate `ColorMaterial`
- [x] Migrate `MaterialMesh2D`
- [x] Migrate fog
- [x] Migrate lights
- [x] Migrate StandardMaterial
- [x] Migrate wireframes
- [x] Migrate clear color
- [x] Migrate text
- [x] Migrate gltf loader
- [x] Register color types for reflection
- [x] Remove `LegacyColor`
- [x] Make sure CI passes

Incidental improvements to ease migration:

- added `Color::srgba_u8`, `Color::srgba_from_array` and friends
- added `set_alpha`, `is_fully_transparent` and `is_fully_opaque` to the
`Alpha` trait
- add and immediately deprecate (lol) `Color::rgb` and friends in favor
of more explicit and consistent `Color::srgb`
- standardized on white and black for most example text colors
- added vector field traits to `LinearRgba`: ~~`Add`, `Sub`,
`AddAssign`, `SubAssign`,~~ `Mul<f32>` and `Div<f32>`. Multiplications
and divisions do not scale alpha. `Add` and `Sub` have been cut from
this PR.
- added `LinearRgba` and `Srgba` `RED/GREEN/BLUE`
- added `LinearRgba_to_f32_array` and `LinearRgba::to_u32`

## Migration Guide

Bevy's color types have changed! Wherever you used a
`bevy::render::Color`, a `bevy::color::Color` is used instead.

These are quite similar! Both are enums storing a color in a specific
color space (or to be more precise, using a specific color model).
However, each of the different color models now has its own type.

TODO...

- `Color::rgba`, `Color::rgb`, `Color::rbga_u8`, `Color::rgb_u8`,
`Color::rgb_from_array` are now `Color::srgba`, `Color::srgb`,
`Color::srgba_u8`, `Color::srgb_u8` and `Color::srgb_from_array`.
- `Color::set_a` and `Color::a` is now `Color::set_alpha` and
`Color::alpha`. These are part of the `Alpha` trait in `bevy_color`.
- `Color::is_fully_transparent` is now part of the `Alpha` trait in
`bevy_color`
- `Color::r`, `Color::set_r`, `Color::with_r` and the equivalents for
`g`, `b` `h`, `s` and `l` have been removed due to causing silent
relatively expensive conversions. Convert your `Color` into the desired
color space, perform your operations there, and then convert it back
into a polymorphic `Color` enum.
- `Color::hex` is now `Srgba::hex`. Call `.into` or construct a
`Color::Srgba` variant manually to convert it.
- `WireframeMaterial`, `ExtractedUiNode`, `ExtractedDirectionalLight`,
`ExtractedPointLight`, `ExtractedSpotLight` and `ExtractedSprite` now
store a `LinearRgba`, rather than a polymorphic `Color`
- `Color::rgb_linear` and `Color::rgba_linear` are now
`Color::linear_rgb` and `Color::linear_rgba`
- The various CSS color constants are no longer stored directly on
`Color`. Instead, they're defined in the `Srgba` color space, and
accessed via `bevy::color::palettes::css`. Call `.into()` on them to
convert them into a `Color` for quick debugging use, and consider using
the much prettier `tailwind` palette for prototyping.
- The `LIME_GREEN` color has been renamed to `LIMEGREEN` to comply with
the standard naming.
- Vector field arithmetic operations on `Color` (add, subtract, multiply
and divide by a f32) have been removed. Instead, convert your colors
into `LinearRgba` space, and perform your operations explicitly there.
This is particularly relevant when working with emissive or HDR colors,
whose color channel values are routinely outside of the ordinary 0 to 1
range.
- `Color::as_linear_rgba_f32` has been removed. Call
`LinearRgba::to_f32_array` instead, converting if needed.
- `Color::as_linear_rgba_u32` has been removed. Call
`LinearRgba::to_u32` instead, converting if needed.
- Several other color conversion methods to transform LCH or HSL colors
into float arrays or `Vec` types have been removed. Please reimplement
these externally or open a PR to re-add them if you found them
particularly useful.
- Various methods on `Color` such as `rgb` or `hsl` to convert the color
into a specific color space have been removed. Convert into
`LinearRgba`, then to the color space of your choice.
- Various implicitly-converting color value methods on `Color` such as
`r`, `g`, `b` or `h` have been removed. Please convert it into the color
space of your choice, then check these properties.
- `Color` no longer implements `AsBindGroup`. Store a `LinearRgba`
internally instead to avoid conversion costs.

---------

Co-authored-by: Alice Cecile <[email protected]>
Co-authored-by: Afonso Lage <[email protected]>
Co-authored-by: Rob Parrett <[email protected]>
Co-authored-by: Zachary Harrold <[email protected]>
  • Loading branch information
5 people authored Feb 29, 2024
1 parent b24ab2e commit 599e5e4
Show file tree
Hide file tree
Showing 180 changed files with 1,372 additions and 2,808 deletions.
103 changes: 99 additions & 4 deletions crates/bevy_color/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ impl Color {
(*self).into()
}

#[deprecated = "Use `Color::srgba` instead"]
/// Creates a new [`Color`] object storing a [`Srgba`] color.
pub const fn rgba(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
Self::srgba(red, green, blue, alpha)
}

/// Creates a new [`Color`] object storing a [`Srgba`] color.
pub const fn srgba(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
Self::Srgba(Srgba {
Expand All @@ -55,6 +61,12 @@ impl Color {
})
}

#[deprecated = "Use `Color::srgb` instead"]
/// Creates a new [`Color`] object storing a [`Srgba`] color with an alpha of 1.0.
pub const fn rgb(red: f32, green: f32, blue: f32) -> Self {
Self::srgb(red, green, blue)
}

/// Creates a new [`Color`] object storing a [`Srgba`] color with an alpha of 1.0.
pub const fn srgb(red: f32, green: f32, blue: f32) -> Self {
Self::Srgba(Srgba {
Expand All @@ -65,7 +77,69 @@ impl Color {
})
}

/// Createsa new [`Color`] object storing a [`LinearRgba`] color.
#[deprecated = "Use `Color::srgb_from_array` instead"]
/// Reads an array of floats to creates a new [`Color`] object storing a [`Srgba`] color with an alpha of 1.0.
pub fn rgb_from_array([r, g, b]: [f32; 3]) -> Self {
Self::Srgba(Srgba::rgb(r, g, b))
}

/// Reads an array of floats to creates a new [`Color`] object storing a [`Srgba`] color with an alpha of 1.0.
pub fn srgb_from_array(array: [f32; 3]) -> Self {
Self::Srgba(Srgba {
red: array[0],
green: array[1],
blue: array[2],
alpha: 1.0,
})
}

#[deprecated = "Use `Color::srgba_u8` instead"]
/// Creates a new [`Color`] object storing a [`Srgba`] color from [`u8`] values.
///
/// A value of 0 is interpreted as 0.0, and a value of 255 is interpreted as 1.0.
pub fn rgba_u8(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
Self::srgba_u8(red, green, blue, alpha)
}

/// Creates a new [`Color`] object storing a [`Srgba`] color from [`u8`] values.
///
/// A value of 0 is interpreted as 0.0, and a value of 255 is interpreted as 1.0.
pub fn srgba_u8(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
Self::Srgba(Srgba {
red: red as f32 / 255.0,
green: green as f32 / 255.0,
blue: blue as f32 / 255.0,
alpha: alpha as f32 / 255.0,
})
}

#[deprecated = "Use `Color::srgb_u8` instead"]
/// Creates a new [`Color`] object storing a [`Srgba`] color from [`u8`] values with an alpha of 1.0.
///
/// A value of 0 is interpreted as 0.0, and a value of 255 is interpreted as 1.0.
pub fn rgb_u8(red: u8, green: u8, blue: u8) -> Self {
Self::srgb_u8(red, green, blue)
}

/// Creates a new [`Color`] object storing a [`Srgba`] color from [`u8`] values with an alpha of 1.0.
///
/// A value of 0 is interpreted as 0.0, and a value of 255 is interpreted as 1.0.
pub fn srgb_u8(red: u8, green: u8, blue: u8) -> Self {
Self::Srgba(Srgba {
red: red as f32 / 255.0,
green: green as f32 / 255.0,
blue: blue as f32 / 255.0,
alpha: 1.0,
})
}

#[deprecated = "Use Color::linear_rgba instead."]
/// Creates a new [`Color`] object storing a [`LinearRgba`] color.
pub const fn rbga_linear(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
Self::linear_rgba(red, green, blue, alpha)
}

/// Creates a new [`Color`] object storing a [`LinearRgba`] color.
pub const fn linear_rgba(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
Self::LinearRgba(LinearRgba {
red,
Expand All @@ -75,7 +149,13 @@ impl Color {
})
}

/// a new [`Color`] object storing a [`LinearRgba`] color with an alpha of 1.0.
#[deprecated = "Use Color::linear_rgb instead."]
/// Creates a new [`Color`] object storing a [`LinearRgba`] color with an alpha of 1.0.
pub const fn rgb_linear(red: f32, green: f32, blue: f32) -> Self {
Self::linear_rgb(red, green, blue)
}

/// Creates a new [`Color`] object storing a [`LinearRgba`] color with an alpha of 1.0.
pub const fn linear_rgb(red: f32, green: f32, blue: f32) -> Self {
Self::LinearRgba(LinearRgba {
red,
Expand Down Expand Up @@ -241,8 +321,8 @@ impl Color {
/// A fully black [`Color::LinearRgba`] color with an alpha of 1.0.
pub const BLACK: Self = Self::linear_rgb(0., 0., 0.);

/// A fully transparent [`Color::LinearRgba`] color.
pub const TRANSPARENT: Self = Self::linear_rgba(0., 0., 0., 0.);
/// A fully transparent [`Color::LinearRgba`] color with 0 red, green and blue.
pub const NONE: Self = Self::linear_rgba(0., 0., 0., 0.);
}

impl Default for Color {
Expand Down Expand Up @@ -286,6 +366,21 @@ impl Alpha for Color {
Color::Xyza(x) => x.alpha(),
}
}

fn set_alpha(&mut self, alpha: f32) {
match self {
Color::Srgba(x) => x.set_alpha(alpha),
Color::LinearRgba(x) => x.set_alpha(alpha),
Color::Hsla(x) => x.set_alpha(alpha),
Color::Hsva(x) => x.set_alpha(alpha),
Color::Hwba(x) => x.set_alpha(alpha),
Color::Laba(x) => x.set_alpha(alpha),
Color::Lcha(x) => x.set_alpha(alpha),
Color::Oklaba(x) => x.set_alpha(alpha),
Color::Oklcha(x) => x.set_alpha(alpha),
Color::Xyza(x) => x.set_alpha(alpha),
}
}
}

impl From<Srgba> for Color {
Expand Down
13 changes: 13 additions & 0 deletions crates/bevy_color/src/color_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,17 @@ pub trait Alpha: Sized {

/// Return a the alpha component of this color.
fn alpha(&self) -> f32;

/// Sets the alpha component of this color.
fn set_alpha(&mut self, alpha: f32);

/// Is the alpha component of this color less than or equal to 0.0?
fn is_fully_transparent(&self) -> bool {
self.alpha() <= 0.0
}

/// Is the alpha component of this color greater than or equal to 1.0?
fn is_fully_opaque(&self) -> bool {
self.alpha() >= 1.0
}
}
5 changes: 5 additions & 0 deletions crates/bevy_color/src/hsla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ impl Alpha for Hsla {
fn alpha(&self) -> f32 {
self.alpha
}

#[inline]
fn set_alpha(&mut self, alpha: f32) {
self.alpha = alpha;
}
}

impl Luminance for Hsla {
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_color/src/hsva.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ impl Alpha for Hsva {
fn alpha(&self) -> f32 {
self.alpha
}

#[inline]
fn set_alpha(&mut self, alpha: f32) {
self.alpha = alpha;
}
}

impl From<Hsva> for Hwba {
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_color/src/hwba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ impl Alpha for Hwba {
fn alpha(&self) -> f32 {
self.alpha
}

#[inline]
fn set_alpha(&mut self, alpha: f32) {
self.alpha = alpha;
}
}

impl From<Srgba> for Hwba {
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_color/src/laba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ impl Alpha for Laba {
fn alpha(&self) -> f32 {
self.alpha
}

#[inline]
fn set_alpha(&mut self, alpha: f32) {
self.alpha = alpha;
}
}

impl Luminance for Laba {
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_color/src/lcha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ impl Alpha for Lcha {
fn alpha(&self) -> f32 {
self.alpha
}

#[inline]
fn set_alpha(&mut self, alpha: f32) {
self.alpha = alpha;
}
}

impl Luminance for Lcha {
Expand Down
93 changes: 93 additions & 0 deletions crates/bevy_color/src/linear_rgba.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::{Div, Mul};

use crate::{color_difference::EuclideanDistance, Alpha, Luminance, Mix, StandardColor};
use bevy_math::Vec4;
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
Expand Down Expand Up @@ -50,6 +52,30 @@ impl LinearRgba {
alpha: 0.0,
};

/// A fully red color with full alpha.
pub const RED: Self = Self {
red: 1.0,
green: 0.0,
blue: 0.0,
alpha: 1.0,
};

/// A fully green color with full alpha.
pub const GREEN: Self = Self {
red: 0.0,
green: 1.0,
blue: 0.0,
alpha: 1.0,
};

/// A fully blue color with full alpha.
pub const BLUE: Self = Self {
red: 0.0,
green: 0.0,
blue: 1.0,
alpha: 1.0,
};

/// An invalid color.
///
/// This type can be used to represent an invalid color value;
Expand Down Expand Up @@ -127,6 +153,26 @@ impl LinearRgba {
self.mix_assign(Self::new(1.0, 1.0, 1.0, self.alpha), adjustment);
}
}

/// Converts the color into a [f32; 4] array in RGBA order.
///
/// This is useful for passing the color to a shader.
pub fn to_f32_array(&self) -> [f32; 4] {
[self.red, self.green, self.blue, self.alpha]
}

/// Converts this color to a u32.
///
/// Maps the RGBA channels in RGBA order to a little-endian byte array (GPUs are little-endian).
/// `A` will be the most significant byte and `R` the least significant.
pub fn as_u32(&self) -> u32 {
u32::from_le_bytes([
(self.red * 255.0) as u8,
(self.green * 255.0) as u8,
(self.blue * 255.0) as u8,
(self.alpha * 255.0) as u8,
])
}
}

impl Default for LinearRgba {
Expand Down Expand Up @@ -193,6 +239,11 @@ impl Alpha for LinearRgba {
fn alpha(&self) -> f32 {
self.alpha
}

#[inline]
fn set_alpha(&mut self, alpha: f32) {
self.alpha = alpha;
}
}

impl EuclideanDistance for LinearRgba {
Expand Down Expand Up @@ -228,6 +279,48 @@ impl From<LinearRgba> for wgpu::Color {
}
}

/// All color channels are scaled directly,
/// but alpha is unchanged.
///
/// Values are not clamped.
impl Mul<f32> for LinearRgba {
type Output = Self;

fn mul(self, rhs: f32) -> Self {
Self {
red: self.red * rhs,
green: self.green * rhs,
blue: self.blue * rhs,
alpha: self.alpha,
}
}
}

impl Mul<LinearRgba> for f32 {
type Output = LinearRgba;

fn mul(self, rhs: LinearRgba) -> LinearRgba {
rhs * self
}
}

/// All color channels are scaled directly,
/// but alpha is unchanged.
///
/// Values are not clamped.
impl Div<f32> for LinearRgba {
type Output = Self;

fn div(self, rhs: f32) -> Self {
Self {
red: self.red / rhs,
green: self.green / rhs,
blue: self.blue / rhs,
alpha: self.alpha,
}
}
}

// [`LinearRgba`] is intended to be used with shaders
// So it's the only color type that implements [`ShaderType`] to make it easier to use inside shaders
impl encase::ShaderType for LinearRgba {
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_color/src/oklaba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ impl Alpha for Oklaba {
fn alpha(&self) -> f32 {
self.alpha
}

#[inline]
fn set_alpha(&mut self, alpha: f32) {
self.alpha = alpha;
}
}

impl Luminance for Oklaba {
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_color/src/oklcha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ impl Alpha for Oklcha {
fn alpha(&self) -> f32 {
self.alpha
}

#[inline]
fn set_alpha(&mut self, alpha: f32) {
self.alpha = alpha;
}
}

impl Luminance for Oklcha {
Expand Down
Loading

0 comments on commit 599e5e4

Please sign in to comment.