Skip to content
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

Meshlet rebase #25

Merged
merged 12 commits into from
Feb 25, 2024
10 changes: 8 additions & 2 deletions crates/bevy_color/crates/gen_tests/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use palette::{Hsl, IntoColor, Lch, LinSrgb, Oklab, Srgb};
use palette::{Hsl, IntoColor, Lch, LinSrgb, Oklab, Srgb, Xyz};

const TEST_COLORS: &[(f32, f32, f32, &str)] = &[
(0., 0., 0., "black"),
Expand All @@ -25,7 +25,7 @@ fn main() {
println!(
"// Generated by gen_tests. Do not edit.
#[cfg(test)]
use crate::{{Hsla, Srgba, LinearRgba, Oklaba, Lcha}};
use crate::{{Hsla, Srgba, LinearRgba, Oklaba, Lcha, Xyza}};

#[cfg(test)]
pub struct TestColor {{
Expand All @@ -35,6 +35,7 @@ pub struct TestColor {{
pub hsl: Hsla,
pub lch: Lcha,
pub oklab: Oklaba,
pub xyz: Xyza,
}}
"
);
Expand All @@ -48,6 +49,7 @@ pub struct TestColor {{
let hsl: Hsl = srgb.into_color();
let lch: Lch = srgb.into_color();
let oklab: Oklab = srgb.into_color();
let xyz: Xyz = srgb.into_color();
println!(" // {name}");
println!(
" TestColor {{
Expand All @@ -57,6 +59,7 @@ pub struct TestColor {{
hsl: Hsla::new({}, {}, {}, 1.0),
lch: Lcha::new({}, {}, {}, 1.0),
oklab: Oklaba::new({}, {}, {}, 1.0),
xyz: Xyza::new({}, {}, {}, 1.0),
}},",
VariablePrecision(srgb.red),
VariablePrecision(srgb.green),
Expand All @@ -73,6 +76,9 @@ pub struct TestColor {{
VariablePrecision(oklab.l),
VariablePrecision(oklab.a),
VariablePrecision(oklab.b),
VariablePrecision(xyz.x),
VariablePrecision(xyz.y),
VariablePrecision(xyz.z),
);
}
println!("];");
Expand Down
34 changes: 32 additions & 2 deletions crates/bevy_color/src/color.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{Alpha, Hsla, Lcha, LinearRgba, Oklaba, Srgba, StandardColor};
use crate::{Alpha, Hsla, Lcha, LinearRgba, Oklaba, Srgba, StandardColor, Xyza};
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
use bevy_render::color::Color as LegacyColor;
use bevy_render::color::LegacyColor;
use serde::{Deserialize, Serialize};

/// An enumerated type that can represent any of the color types in this crate.
Expand All @@ -20,6 +20,8 @@ pub enum Color {
Lcha(Lcha),
/// A color in the Oklaba color space with alpha.
Oklaba(Oklaba),
/// A color in the XYZ color space with alpha.
Xyza(Xyza),
}

impl StandardColor for Color {}
Expand All @@ -33,6 +35,7 @@ impl Color {
Color::Hsla(hsla) => (*hsla).into(),
Color::Lcha(lcha) => (*lcha).into(),
Color::Oklaba(oklab) => (*oklab).into(),
Color::Xyza(xyza) => (*xyza).into(),
}
}
}
Expand All @@ -53,6 +56,7 @@ impl Alpha for Color {
Color::Hsla(x) => *x = x.with_alpha(alpha),
Color::Lcha(x) => *x = x.with_alpha(alpha),
Color::Oklaba(x) => *x = x.with_alpha(alpha),
Color::Xyza(x) => *x = x.with_alpha(alpha),
}

new
Expand All @@ -65,6 +69,7 @@ impl Alpha for Color {
Color::Hsla(x) => x.alpha(),
Color::Lcha(x) => x.alpha(),
Color::Oklaba(x) => x.alpha(),
Color::Xyza(x) => x.alpha(),
}
}
}
Expand Down Expand Up @@ -99,6 +104,12 @@ impl From<Lcha> for Color {
}
}

impl From<Xyza> for Color {
fn from(value: Xyza) -> Self {
Self::Xyza(value)
}
}

impl From<Color> for Srgba {
fn from(value: Color) -> Self {
match value {
Expand All @@ -107,6 +118,7 @@ impl From<Color> for Srgba {
Color::Hsla(hsla) => hsla.into(),
Color::Lcha(lcha) => lcha.into(),
Color::Oklaba(oklab) => oklab.into(),
Color::Xyza(xyza) => xyza.into(),
}
}
}
Expand All @@ -119,6 +131,7 @@ impl From<Color> for LinearRgba {
Color::Hsla(hsla) => hsla.into(),
Color::Lcha(lcha) => lcha.into(),
Color::Oklaba(oklab) => oklab.into(),
Color::Xyza(xyza) => xyza.into(),
}
}
}
Expand All @@ -131,6 +144,7 @@ impl From<Color> for Hsla {
Color::Hsla(hsla) => hsla,
Color::Lcha(lcha) => LinearRgba::from(lcha).into(),
Color::Oklaba(oklab) => LinearRgba::from(oklab).into(),
Color::Xyza(xyza) => LinearRgba::from(xyza).into(),
}
}
}
Expand All @@ -143,6 +157,7 @@ impl From<Color> for Lcha {
Color::Hsla(hsla) => Srgba::from(hsla).into(),
Color::Lcha(lcha) => lcha,
Color::Oklaba(oklab) => LinearRgba::from(oklab).into(),
Color::Xyza(xyza) => LinearRgba::from(xyza).into(),
}
}
}
Expand All @@ -155,6 +170,20 @@ impl From<Color> for Oklaba {
Color::Hsla(hsla) => Srgba::from(hsla).into(),
Color::Lcha(lcha) => LinearRgba::from(lcha).into(),
Color::Oklaba(oklab) => oklab,
Color::Xyza(xyza) => LinearRgba::from(xyza).into(),
}
}
}

impl From<Color> for Xyza {
fn from(value: Color) -> Self {
match value {
Color::Srgba(x) => x.into(),
Color::LinearRgba(x) => x.into(),
Color::Hsla(x) => x.into(),
Color::Lcha(x) => x.into(),
Color::Oklaba(x) => x.into(),
Color::Xyza(xyza) => xyza,
}
}
}
Expand All @@ -178,6 +207,7 @@ impl From<Color> for LegacyColor {
Color::Hsla(x) => x.into(),
Color::Lcha(x) => x.into(),
Color::Oklaba(x) => x.into(),
Color::Xyza(x) => x.into(),
}
}
}
25 changes: 20 additions & 5 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 Expand Up @@ -121,9 +136,9 @@ impl From<Srgba> for Hsla {
}
}

impl From<Hsla> for bevy_render::color::Color {
impl From<Hsla> for bevy_render::color::LegacyColor {
fn from(value: Hsla) -> Self {
bevy_render::color::Color::Hsla {
bevy_render::color::LegacyColor::Hsla {
hue: value.hue,
saturation: value.saturation,
lightness: value.lightness,
Expand All @@ -132,10 +147,10 @@ impl From<Hsla> for bevy_render::color::Color {
}
}

impl From<bevy_render::color::Color> for Hsla {
fn from(value: bevy_render::color::Color) -> Self {
impl From<bevy_render::color::LegacyColor> for Hsla {
fn from(value: bevy_render::color::LegacyColor) -> Self {
match value.as_hsla() {
bevy_render::color::Color::Hsla {
bevy_render::color::LegacyColor::Hsla {
hue,
saturation,
lightness,
Expand Down
25 changes: 20 additions & 5 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 Expand Up @@ -142,9 +157,9 @@ impl From<Lcha> for LinearRgba {
}
}

impl From<Lcha> for bevy_render::color::Color {
impl From<Lcha> for bevy_render::color::LegacyColor {
fn from(value: Lcha) -> Self {
bevy_render::color::Color::Lcha {
bevy_render::color::LegacyColor::Lcha {
hue: value.hue,
chroma: value.chroma,
lightness: value.lightness,
Expand All @@ -153,10 +168,10 @@ impl From<Lcha> for bevy_render::color::Color {
}
}

impl From<bevy_render::color::Color> for Lcha {
fn from(value: bevy_render::color::Color) -> Self {
impl From<bevy_render::color::LegacyColor> for Lcha {
fn from(value: bevy_render::color::LegacyColor) -> Self {
match value.as_lcha() {
bevy_render::color::Color::Lcha {
bevy_render::color::LegacyColor::Lcha {
hue,
chroma,
lightness,
Expand Down
10 changes: 9 additions & 1 deletion crates/bevy_color/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! - [`Hsla`] (hue, saturation, lightness, alpha)
//! - [`Lcha`] (lightness, chroma, hue, alpha)
//! - [`Oklaba`] (lightness, a-axis, b-axis, alpha)
//! - [`Xyza`] (x-axis, y-axis, z-axis, alpha)
//!
//! Each of these color spaces is represented as a distinct Rust type.
//!
Expand Down Expand Up @@ -35,6 +36,10 @@
//! for tasks such as color correction and image analysis, where it is important to be able
//! to do things like change color saturation without causing hue shifts.
//!
//! XYZ is a foundational space commonly used in the definition of other more modern color
//! spaces. The space is more formally known as CIE 1931, where the `x` and `z` axes represent
//! a form of chromaticity, while `y` defines an illuminance level.
//!
//! See also the [Wikipedia article on color spaces](https://en.wikipedia.org/wiki/Color_space).
//!
//! # Conversions
Expand Down Expand Up @@ -78,6 +83,7 @@ mod srgba;
mod test_colors;
#[cfg(test)]
mod testing;
mod xyza;

pub use color::*;
pub use color_ops::*;
Expand All @@ -87,8 +93,9 @@ pub use lcha::*;
pub use linear_rgba::*;
pub use oklaba::*;
pub use srgba::*;
pub use xyza::*;

use bevy_render::color::Color as LegacyColor;
use bevy_render::color::LegacyColor;

/// Describes the traits that a color should implement for consistency.
pub(crate) trait StandardColor
Expand All @@ -106,6 +113,7 @@ where
Self: From<Hsla> + Into<Hsla>,
Self: From<Lcha> + Into<Lcha>,
Self: From<Oklaba> + Into<Oklaba>,
Self: From<Xyza> + Into<Xyza>,
Self: Alpha,
{
}
25 changes: 20 additions & 5 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 Expand Up @@ -157,9 +172,9 @@ impl From<Srgba> for LinearRgba {
}
}

impl From<LinearRgba> for bevy_render::color::Color {
impl From<LinearRgba> for bevy_render::color::LegacyColor {
fn from(value: LinearRgba) -> Self {
bevy_render::color::Color::RgbaLinear {
bevy_render::color::LegacyColor::RgbaLinear {
red: value.red,
green: value.green,
blue: value.blue,
Expand All @@ -168,10 +183,10 @@ impl From<LinearRgba> for bevy_render::color::Color {
}
}

impl From<bevy_render::color::Color> for LinearRgba {
fn from(value: bevy_render::color::Color) -> Self {
impl From<bevy_render::color::LegacyColor> for LinearRgba {
fn from(value: bevy_render::color::LegacyColor) -> Self {
match value.as_rgba_linear() {
bevy_render::color::Color::RgbaLinear {
bevy_render::color::LegacyColor::RgbaLinear {
red,
green,
blue,
Expand Down
17 changes: 16 additions & 1 deletion crates/bevy_color/src/oklaba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
StandardColor,
};
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
use bevy_render::color::Color as LegacyColor;
use bevy_render::color::LegacyColor;
use serde::{Deserialize, Serialize};

/// Color in Oklaba color space, with alpha
Expand Down 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
Loading