From 204863d3a8f51b2d4f576d9981b9143c479c9ea6 Mon Sep 17 00:00:00 2001 From: roman Date: Mon, 30 Oct 2023 18:12:50 +0300 Subject: [PATCH 1/8] Add color conversion methods --- crates/bevy_gltf/src/loader.rs | 6 +- crates/bevy_render/src/color/mod.rs | 322 +++++++++++++++++++++------- 2 files changed, 243 insertions(+), 85 deletions(-) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index fa3085e287cf1..b829cd763ac2f 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -935,7 +935,7 @@ fn load_node( gltf::khr_lights_punctual::Kind::Directional => { let mut entity = parent.spawn(DirectionalLightBundle { directional_light: DirectionalLight { - color: Color::from(light.color()), + color: Color::rgb_from_array(light.color()), // NOTE: KHR_punctual_lights defines the intensity units for directional // lights in lux (lm/m^2) which is what we need. illuminance: light.intensity(), @@ -955,7 +955,7 @@ fn load_node( gltf::khr_lights_punctual::Kind::Point => { let mut entity = parent.spawn(PointLightBundle { point_light: PointLight { - color: Color::from(light.color()), + color: Color::rgb_from_array(light.color()), // NOTE: KHR_punctual_lights defines the intensity units for point lights in // candela (lm/sr) which is luminous intensity and we need luminous power. // For a point light, luminous power = 4 * pi * luminous intensity @@ -981,7 +981,7 @@ fn load_node( } => { let mut entity = parent.spawn(SpotLightBundle { spot_light: SpotLight { - color: Color::from(light.color()), + color: Color::rgb_from_array(light.color()), // NOTE: KHR_punctual_lights defines the intensity units for spot lights in // candela (lm/sr) which is luminous intensity and we need luminous power. // For a spot light, we map luminous power = 4 * pi * luminous intensity diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index 3643439383bda..7d9eadade55d5 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -1083,67 +1083,222 @@ impl Color { } } } -} -impl Default for Color { - fn default() -> Self { - Color::WHITE + /// New `Color` from `Vec4` in sRGB colorspace. + #[inline] + pub fn rgba_from_vec4(vec4: Vec4) -> Self { + Color::rgba(vec4.x, vec4.y, vec4.z, vec4.w) } -} -impl AddAssign for Color { - fn add_assign(&mut self, rhs: Color) { - match self { + /// New `Color` from `[f32; 4]` in sRGB colorspace. + #[inline] + pub fn rgba_from_array([r, g, b, a]: [f32; 4]) -> Self { + Color::rgba(r, g, b, a) + } + + /// New `Color` from `Vec3` in sRGB colorspace. + #[inline] + pub fn rgb_from_vec3(vec3: Vec3) -> Self { + Color::rgb(vec3.x, vec3.y, vec3.z) + } + + /// New `Color` from `[f32; 3]` in sRGB colorspace. + #[inline] + pub fn rgb_from_array([r, g, b]: [f32; 3]) -> Self { + Color::rgb(r, g, b) + } + + /// New `Color` from `Vec4` in linear RGB colorspace. + #[inline] + pub fn rgba_linear_from_vec4(vec4: Vec4) -> Self { + Color::rgba_linear(vec4.x, vec4.y, vec4.z, vec4.w) + } + + /// New `Color` from `[f32; 4]` in linear RGB colorspace. + #[inline] + pub fn rgba_linear_from_array([r, g, b, a]: [f32; 4]) -> Self { + Color::rgba_linear(r, g, b, a) + } + + /// New `Color` from `Vec3` in linear RGB colorspace. + #[inline] + pub fn rgb_linear_from_vec3(vec3: Vec3) -> Self { + Color::rgb_linear(vec3.x, vec3.y, vec3.z) + } + + /// New `Color` from `[f32; 3]` in linear RGB colorspace. + #[inline] + pub fn rgb_linear_from_array([r, g, b]: [f32; 3]) -> Self { + Color::rgb_linear(r, g, b) + } + + /// New `Color` from `Vec4` with HSL representation in sRGB colorspace. + #[inline] + pub fn hsla_from_vec4(vec4: Vec4) -> Self { + Color::hsla(vec4.x, vec4.y, vec4.z, vec4.w) + } + + /// New `Color` from `[f32; 4]` with HSL representation in sRGB colorspace. + #[inline] + pub fn hsla_from_array([h, s, l, a]: [f32; 4]) -> Self { + Color::hsla(h, s, l, a) + } + + /// New `Color` from `Vec3` with HSL representation in sRGB colorspace. + #[inline] + pub fn hsl_from_vec3(vec3: Vec3) -> Self { + Color::hsl(vec3.x, vec3.y, vec3.z) + } + + /// New `Color` from `[f32; 3]` with HSL representation in sRGB colorspace. + #[inline] + pub fn hsl_from_array([h, s, l]: [f32; 3]) -> Self { + Color::hsl(h, s, l) + } + + /// New `Color` from `Vec4` with LCH representation in sRGB colorspace. + #[inline] + pub fn lcha_from_vec4(vec4: Vec4) -> Self { + Color::lcha(vec4.x, vec4.y, vec4.z, vec4.w) + } + + /// New `Color` from `[f32; 4]` with LCH representation in sRGB colorspace. + #[inline] + pub fn lcha_from_array([l, c, h, a]: [f32; 4]) -> Self { + Color::lcha(l, c, h, a) + } + + /// New `Color` from `Vec3` with LCH representation in sRGB colorspace. + #[inline] + pub fn lch_from_vec3(vec3: Vec3) -> Self { + Color::lch(vec3.x, vec3.y, vec3.z) + } + + /// New `Color` from `[f32; 3]` with LCH representation in sRGB colorspace. + #[inline] + pub fn lch_from_array([l, c, h]: [f32; 3]) -> Self { + Color::lch(l, c, h) + } + + /// Convert `Color` to RGBA and return as `Vec4`. + #[inline] + pub fn rgba_to_vec4(&self) -> Vec4 { + let color = self.as_rgba(); + match color { Color::Rgba { red, green, blue, alpha, - } => { - let rhs = rhs.as_rgba_f32(); - *red += rhs[0]; - *green += rhs[1]; - *blue += rhs[2]; - *alpha += rhs[3]; - } + } => Vec4::new(red, green, blue, alpha), + _ => unreachable!(), + } + } + + /// Convert `Color` to RGBA and return as `Vec3`. + #[inline] + pub fn rgb_to_vec3(&self) -> Vec3 { + let color = self.as_rgba(); + match color { + Color::Rgba { + red, green, blue, .. + } => Vec3::new(red, green, blue), + _ => unreachable!(), + } + } + + /// Convert `Color` to linear RGBA and return as `Vec4`. + #[inline] + pub fn rgba_linear_to_vec4(&self) -> Vec4 { + let color = self.as_rgba_linear(); + match color { Color::RgbaLinear { red, green, blue, alpha, - } => { - let rhs = rhs.as_linear_rgba_f32(); - *red += rhs[0]; - *green += rhs[1]; - *blue += rhs[2]; - *alpha += rhs[3]; - } + } => Vec4::new(red, green, blue, alpha), + _ => unreachable!(), + } + } + + /// Convert `Color` to linear RGBA and return as `Vec3`. + #[inline] + pub fn rgb_linear_to_vec3(&self) -> Vec3 { + let color = self.as_rgba_linear(); + match color { + Color::RgbaLinear { + red, green, blue, .. + } => Vec3::new(red, green, blue), + _ => unreachable!(), + } + } + + /// Convert `Color` to HSLA and return as `Vec4`. + #[inline] + pub fn hsla_to_vec4(&self) -> Vec4 { + let color = self.as_hsla(); + match color { Color::Hsla { hue, saturation, lightness, alpha, - } => { - let rhs = rhs.as_hsla_f32(); - *hue += rhs[0]; - *saturation += rhs[1]; - *lightness += rhs[2]; - *alpha += rhs[3]; - } + } => Vec4::new(hue, saturation, lightness, alpha), + _ => unreachable!(), + } + } + + /// Convert `Color` to HSLA and return as `Vec3`. + #[inline] + pub fn hsl_to_vec3(&self) -> Vec3 { + let color = self.as_hsla(); + match color { + Color::Hsla { + hue, + saturation, + lightness, + .. + } => Vec3::new(hue, saturation, lightness), + _ => unreachable!(), + } + } + + /// Convert `Color` to LCHA and return as `Vec4`. + #[inline] + pub fn lcha_to_vec4(&self) -> Vec4 { + let color = self.as_lcha(); + match color { Color::Lcha { lightness, chroma, hue, alpha, - } => { - let rhs = rhs.as_lcha_f32(); - *lightness += rhs[0]; - *chroma += rhs[1]; - *hue += rhs[2]; - *alpha += rhs[3]; - } + } => Vec4::new(lightness, chroma, hue, alpha), + _ => unreachable!(), } } + + /// Convert `Color` to LCHA and return as `Vec3`. + #[inline] + pub fn lch_to_vec3(&self) -> Vec3 { + let color = self.as_lcha(); + match color { + Color::Lcha { + lightness, + chroma, + hue, + .. + } => Vec3::new(lightness, chroma, hue), + _ => unreachable!(), + } + } +} + +impl Default for Color { + fn default() -> Self { + Color::WHITE + } } impl Add for Color { @@ -1200,7 +1355,6 @@ impl Add for Color { alpha, } => { let rhs = rhs.as_lcha_f32(); - Color::Lcha { lightness: lightness + rhs[0], chroma: chroma + rhs[1], @@ -1214,48 +1368,52 @@ impl Add for Color { impl AddAssign for Color { fn add_assign(&mut self, rhs: Vec4) { - let rhs: Color = rhs.into(); - *self += rhs; - } -} - -impl Add for Color { - type Output = Color; - - fn add(self, rhs: Vec4) -> Self::Output { - let rhs: Color = rhs.into(); - self + rhs - } -} - -impl From for [f32; 4] { - fn from(color: Color) -> Self { - color.as_rgba_f32() - } -} - -impl From<[f32; 4]> for Color { - fn from([r, g, b, a]: [f32; 4]) -> Self { - Color::rgba(r, g, b, a) - } -} - -impl From<[f32; 3]> for Color { - fn from([r, g, b]: [f32; 3]) -> Self { - Color::rgb(r, g, b) - } -} - -impl From for Vec4 { - fn from(color: Color) -> Self { - let color: [f32; 4] = color.into(); - Vec4::new(color[0], color[1], color[2], color[3]) - } -} - -impl From for Color { - fn from(vec4: Vec4) -> Self { - Color::rgba(vec4.x, vec4.y, vec4.z, vec4.w) + match self { + Color::Rgba { + red, + green, + blue, + alpha, + } => { + *red += rhs.x; + *green += rhs.y; + *blue += rhs.z; + *alpha += rhs.w; + } + Color::RgbaLinear { + red, + green, + blue, + alpha, + } => { + *red += rhs.x; + *green += rhs.y; + *blue += rhs.z; + *alpha += rhs.w; + } + Color::Hsla { + hue, + saturation, + lightness, + alpha, + } => { + *hue += rhs.x; + *saturation += rhs.y; + *lightness += rhs.z; + *alpha += rhs.w; + } + Color::Lcha { + lightness, + chroma, + hue, + alpha, + } => { + *lightness += rhs.x; + *chroma += rhs.y; + *hue += rhs.z; + *alpha += rhs.w; + } + } } } @@ -1890,15 +2048,15 @@ mod tests { #[test] fn conversions_vec4() { let starting_vec4 = Vec4::new(0.4, 0.5, 0.6, 1.0); - let starting_color = Color::from(starting_vec4); + let starting_color = Color::rgba_from_vec4(starting_vec4); - assert_eq!(starting_vec4, Vec4::from(starting_color)); + assert_eq!(starting_vec4, starting_color.rgba_to_vec4()); let transformation = Vec4::new(0.5, 0.5, 0.5, 1.0); assert_eq!( starting_color * transformation, - Color::from(starting_vec4 * transformation), + Color::rgba_from_vec4(starting_vec4 * transformation) ); } From 379c1eb0dab4063e11ae11a554c907a6db6e42b7 Mon Sep 17 00:00:00 2001 From: roman Date: Mon, 30 Oct 2023 18:37:40 +0300 Subject: [PATCH 2/8] clippy --- crates/bevy_render/src/color/mod.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index 7d9eadade55d5..30ef073cec756 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -1374,13 +1374,8 @@ impl AddAssign for Color { green, blue, alpha, - } => { - *red += rhs.x; - *green += rhs.y; - *blue += rhs.z; - *alpha += rhs.w; } - Color::RgbaLinear { + | Color::RgbaLinear { red, green, blue, From 15b277237022a410c951e756ae1c48d37bccca03 Mon Sep 17 00:00:00 2001 From: roman Date: Mon, 30 Oct 2023 19:07:35 +0300 Subject: [PATCH 3/8] Remove AddAssign impl --- crates/bevy_render/src/color/mod.rs | 46 ----------------------------- 1 file changed, 46 deletions(-) diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index 30ef073cec756..a13c6cc86d99a 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -1366,52 +1366,6 @@ impl Add for Color { } } -impl AddAssign for Color { - fn add_assign(&mut self, rhs: Vec4) { - match self { - Color::Rgba { - red, - green, - blue, - alpha, - } - | Color::RgbaLinear { - red, - green, - blue, - alpha, - } => { - *red += rhs.x; - *green += rhs.y; - *blue += rhs.z; - *alpha += rhs.w; - } - Color::Hsla { - hue, - saturation, - lightness, - alpha, - } => { - *hue += rhs.x; - *saturation += rhs.y; - *lightness += rhs.z; - *alpha += rhs.w; - } - Color::Lcha { - lightness, - chroma, - hue, - alpha, - } => { - *lightness += rhs.x; - *chroma += rhs.y; - *hue += rhs.z; - *alpha += rhs.w; - } - } - } -} - impl From for wgpu::Color { fn from(color: Color) -> Self { if let Color::RgbaLinear { From 6a648858f5bd1b7e20dcd2c4446f71d9bc7450c1 Mon Sep 17 00:00:00 2001 From: roman Date: Mon, 30 Oct 2023 19:18:07 +0300 Subject: [PATCH 4/8] Remove unused import --- crates/bevy_render/src/color/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index a13c6cc86d99a..7c6497d06d06b 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -5,7 +5,7 @@ pub use colorspace::*; use bevy_math::{Vec3, Vec4}; use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize}; use serde::{Deserialize, Serialize}; -use std::ops::{Add, AddAssign, Mul, MulAssign}; +use std::ops::{Add, Mul, MulAssign}; use thiserror::Error; #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Reflect)] From 810e2de0d33271e0f068876f0b867c14df911e12 Mon Sep 17 00:00:00 2001 From: roman Date: Tue, 31 Oct 2023 22:17:55 +0300 Subject: [PATCH 5/8] Make the functions to take impl Into<[f32]> --- crates/bevy_gltf/src/loader.rs | 6 +-- crates/bevy_render/src/color/mod.rs | 76 +++++++---------------------- 2 files changed, 21 insertions(+), 61 deletions(-) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index b829cd763ac2f..5d7846c1754bb 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -935,7 +935,7 @@ fn load_node( gltf::khr_lights_punctual::Kind::Directional => { let mut entity = parent.spawn(DirectionalLightBundle { directional_light: DirectionalLight { - color: Color::rgb_from_array(light.color()), + color: Color::rgb_from(light.color()), // NOTE: KHR_punctual_lights defines the intensity units for directional // lights in lux (lm/m^2) which is what we need. illuminance: light.intensity(), @@ -955,7 +955,7 @@ fn load_node( gltf::khr_lights_punctual::Kind::Point => { let mut entity = parent.spawn(PointLightBundle { point_light: PointLight { - color: Color::rgb_from_array(light.color()), + color: Color::rgb_from(light.color()), // NOTE: KHR_punctual_lights defines the intensity units for point lights in // candela (lm/sr) which is luminous intensity and we need luminous power. // For a point light, luminous power = 4 * pi * luminous intensity @@ -981,7 +981,7 @@ fn load_node( } => { let mut entity = parent.spawn(SpotLightBundle { spot_light: SpotLight { - color: Color::rgb_from_array(light.color()), + color: Color::rgb_from(light.color()), // NOTE: KHR_punctual_lights defines the intensity units for spot lights in // candela (lm/sr) which is luminous intensity and we need luminous power. // For a spot light, we map luminous power = 4 * pi * luminous intensity diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index 7c6497d06d06b..98a4f48e174ca 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -1084,99 +1084,59 @@ impl Color { } } - /// New `Color` from `Vec4` in sRGB colorspace. - #[inline] - pub fn rgba_from_vec4(vec4: Vec4) -> Self { - Color::rgba(vec4.x, vec4.y, vec4.z, vec4.w) - } - /// New `Color` from `[f32; 4]` in sRGB colorspace. #[inline] - pub fn rgba_from_array([r, g, b, a]: [f32; 4]) -> Self { + pub fn rgba_from(arr: impl Into<[f32; 4]>) -> Self { + let [r, g, b, a]: [f32; 4] = arr.into(); Color::rgba(r, g, b, a) } - /// New `Color` from `Vec3` in sRGB colorspace. - #[inline] - pub fn rgb_from_vec3(vec3: Vec3) -> Self { - Color::rgb(vec3.x, vec3.y, vec3.z) - } - /// New `Color` from `[f32; 3]` in sRGB colorspace. #[inline] - pub fn rgb_from_array([r, g, b]: [f32; 3]) -> Self { + pub fn rgb_from(arr: impl Into<[f32; 3]>) -> Self { + let [r, g, b]: [f32; 3] = arr.into(); Color::rgb(r, g, b) } - /// New `Color` from `Vec4` in linear RGB colorspace. - #[inline] - pub fn rgba_linear_from_vec4(vec4: Vec4) -> Self { - Color::rgba_linear(vec4.x, vec4.y, vec4.z, vec4.w) - } - /// New `Color` from `[f32; 4]` in linear RGB colorspace. #[inline] - pub fn rgba_linear_from_array([r, g, b, a]: [f32; 4]) -> Self { + pub fn rgba_linear_from(arr: impl Into<[f32; 4]>) -> Self { + let [r, g, b, a]: [f32; 4] = arr.into(); Color::rgba_linear(r, g, b, a) } - /// New `Color` from `Vec3` in linear RGB colorspace. - #[inline] - pub fn rgb_linear_from_vec3(vec3: Vec3) -> Self { - Color::rgb_linear(vec3.x, vec3.y, vec3.z) - } - /// New `Color` from `[f32; 3]` in linear RGB colorspace. #[inline] - pub fn rgb_linear_from_array([r, g, b]: [f32; 3]) -> Self { + pub fn rgb_linear_from(arr: impl Into<[f32; 3]>) -> Self { + let [r, g, b]: [f32; 3] = arr.into(); Color::rgb_linear(r, g, b) } - /// New `Color` from `Vec4` with HSL representation in sRGB colorspace. - #[inline] - pub fn hsla_from_vec4(vec4: Vec4) -> Self { - Color::hsla(vec4.x, vec4.y, vec4.z, vec4.w) - } - /// New `Color` from `[f32; 4]` with HSL representation in sRGB colorspace. #[inline] - pub fn hsla_from_array([h, s, l, a]: [f32; 4]) -> Self { + pub fn hsla_from(arr: impl Into<[f32; 4]>) -> Self { + let [h, s, l, a]: [f32; 4] = arr.into(); Color::hsla(h, s, l, a) } - /// New `Color` from `Vec3` with HSL representation in sRGB colorspace. - #[inline] - pub fn hsl_from_vec3(vec3: Vec3) -> Self { - Color::hsl(vec3.x, vec3.y, vec3.z) - } - /// New `Color` from `[f32; 3]` with HSL representation in sRGB colorspace. #[inline] - pub fn hsl_from_array([h, s, l]: [f32; 3]) -> Self { + pub fn hsl_from(arr: impl Into<[f32; 3]>) -> Self { + let [h, s, l]: [f32; 3] = arr.into(); Color::hsl(h, s, l) } - /// New `Color` from `Vec4` with LCH representation in sRGB colorspace. - #[inline] - pub fn lcha_from_vec4(vec4: Vec4) -> Self { - Color::lcha(vec4.x, vec4.y, vec4.z, vec4.w) - } - /// New `Color` from `[f32; 4]` with LCH representation in sRGB colorspace. #[inline] - pub fn lcha_from_array([l, c, h, a]: [f32; 4]) -> Self { + pub fn lcha_from(arr: impl Into<[f32; 4]>) -> Self { + let [l, c, h, a]: [f32; 4] = arr.into(); Color::lcha(l, c, h, a) } - /// New `Color` from `Vec3` with LCH representation in sRGB colorspace. - #[inline] - pub fn lch_from_vec3(vec3: Vec3) -> Self { - Color::lch(vec3.x, vec3.y, vec3.z) - } - /// New `Color` from `[f32; 3]` with LCH representation in sRGB colorspace. #[inline] - pub fn lch_from_array([l, c, h]: [f32; 3]) -> Self { + pub fn lch_from(arr: impl Into<[f32; 3]>) -> Self { + let [l, c, h]: [f32; 3] = arr.into(); Color::lch(l, c, h) } @@ -1997,7 +1957,7 @@ mod tests { #[test] fn conversions_vec4() { let starting_vec4 = Vec4::new(0.4, 0.5, 0.6, 1.0); - let starting_color = Color::rgba_from_vec4(starting_vec4); + let starting_color = Color::rgba_from(starting_vec4); assert_eq!(starting_vec4, starting_color.rgba_to_vec4()); @@ -2005,7 +1965,7 @@ mod tests { assert_eq!( starting_color * transformation, - Color::rgba_from_vec4(starting_vec4 * transformation) + Color::rgba_from(starting_vec4 * transformation) ); } From ef8807f3c498fc5abe6c4fc1bc524a6060bc34cd Mon Sep 17 00:00:00 2001 From: roman Date: Wed, 1 Nov 2023 00:35:14 +0300 Subject: [PATCH 6/8] *_from => *_from_array --- crates/bevy_gltf/src/loader.rs | 6 +++--- crates/bevy_render/src/color/mod.rs | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 5d7846c1754bb..b829cd763ac2f 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -935,7 +935,7 @@ fn load_node( gltf::khr_lights_punctual::Kind::Directional => { let mut entity = parent.spawn(DirectionalLightBundle { directional_light: DirectionalLight { - color: Color::rgb_from(light.color()), + color: Color::rgb_from_array(light.color()), // NOTE: KHR_punctual_lights defines the intensity units for directional // lights in lux (lm/m^2) which is what we need. illuminance: light.intensity(), @@ -955,7 +955,7 @@ fn load_node( gltf::khr_lights_punctual::Kind::Point => { let mut entity = parent.spawn(PointLightBundle { point_light: PointLight { - color: Color::rgb_from(light.color()), + color: Color::rgb_from_array(light.color()), // NOTE: KHR_punctual_lights defines the intensity units for point lights in // candela (lm/sr) which is luminous intensity and we need luminous power. // For a point light, luminous power = 4 * pi * luminous intensity @@ -981,7 +981,7 @@ fn load_node( } => { let mut entity = parent.spawn(SpotLightBundle { spot_light: SpotLight { - color: Color::rgb_from(light.color()), + color: Color::rgb_from_array(light.color()), // NOTE: KHR_punctual_lights defines the intensity units for spot lights in // candela (lm/sr) which is luminous intensity and we need luminous power. // For a spot light, we map luminous power = 4 * pi * luminous intensity diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index 98a4f48e174ca..2677c6a508c55 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -1086,56 +1086,56 @@ impl Color { /// New `Color` from `[f32; 4]` in sRGB colorspace. #[inline] - pub fn rgba_from(arr: impl Into<[f32; 4]>) -> Self { + pub fn rgba_from_array(arr: impl Into<[f32; 4]>) -> Self { let [r, g, b, a]: [f32; 4] = arr.into(); Color::rgba(r, g, b, a) } /// New `Color` from `[f32; 3]` in sRGB colorspace. #[inline] - pub fn rgb_from(arr: impl Into<[f32; 3]>) -> Self { + pub fn rgb_from_array(arr: impl Into<[f32; 3]>) -> Self { let [r, g, b]: [f32; 3] = arr.into(); Color::rgb(r, g, b) } /// New `Color` from `[f32; 4]` in linear RGB colorspace. #[inline] - pub fn rgba_linear_from(arr: impl Into<[f32; 4]>) -> Self { + pub fn rgba_linear_from_array(arr: impl Into<[f32; 4]>) -> Self { let [r, g, b, a]: [f32; 4] = arr.into(); Color::rgba_linear(r, g, b, a) } /// New `Color` from `[f32; 3]` in linear RGB colorspace. #[inline] - pub fn rgb_linear_from(arr: impl Into<[f32; 3]>) -> Self { + pub fn rgb_linear_from_array(arr: impl Into<[f32; 3]>) -> Self { let [r, g, b]: [f32; 3] = arr.into(); Color::rgb_linear(r, g, b) } /// New `Color` from `[f32; 4]` with HSL representation in sRGB colorspace. #[inline] - pub fn hsla_from(arr: impl Into<[f32; 4]>) -> Self { + pub fn hsla_from_array(arr: impl Into<[f32; 4]>) -> Self { let [h, s, l, a]: [f32; 4] = arr.into(); Color::hsla(h, s, l, a) } /// New `Color` from `[f32; 3]` with HSL representation in sRGB colorspace. #[inline] - pub fn hsl_from(arr: impl Into<[f32; 3]>) -> Self { + pub fn hsl_from_array(arr: impl Into<[f32; 3]>) -> Self { let [h, s, l]: [f32; 3] = arr.into(); Color::hsl(h, s, l) } /// New `Color` from `[f32; 4]` with LCH representation in sRGB colorspace. #[inline] - pub fn lcha_from(arr: impl Into<[f32; 4]>) -> Self { + pub fn lcha_from_array(arr: impl Into<[f32; 4]>) -> Self { let [l, c, h, a]: [f32; 4] = arr.into(); Color::lcha(l, c, h, a) } /// New `Color` from `[f32; 3]` with LCH representation in sRGB colorspace. #[inline] - pub fn lch_from(arr: impl Into<[f32; 3]>) -> Self { + pub fn lch_from_array(arr: impl Into<[f32; 3]>) -> Self { let [l, c, h]: [f32; 3] = arr.into(); Color::lch(l, c, h) } @@ -1957,7 +1957,7 @@ mod tests { #[test] fn conversions_vec4() { let starting_vec4 = Vec4::new(0.4, 0.5, 0.6, 1.0); - let starting_color = Color::rgba_from(starting_vec4); + let starting_color = Color::rgba_from_array(starting_vec4); assert_eq!(starting_vec4, starting_color.rgba_to_vec4()); @@ -1965,7 +1965,7 @@ mod tests { assert_eq!( starting_color * transformation, - Color::rgba_from(starting_vec4 * transformation) + Color::rgba_from_array(starting_vec4 * transformation) ); } From 8c18fc2a666ec168f2ce4ee7884df65f5be0453d Mon Sep 17 00:00:00 2001 From: roman Date: Wed, 1 Nov 2023 01:08:00 +0300 Subject: [PATCH 7/8] Update docs --- crates/bevy_render/src/color/mod.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index 2677c6a508c55..69aac87eabbd1 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -1084,56 +1084,56 @@ impl Color { } } - /// New `Color` from `[f32; 4]` in sRGB colorspace. + /// New `Color` from `[f32; 4]` (or a type that can be converted into them) with RGB representation in sRGB colorspace. #[inline] pub fn rgba_from_array(arr: impl Into<[f32; 4]>) -> Self { let [r, g, b, a]: [f32; 4] = arr.into(); Color::rgba(r, g, b, a) } - /// New `Color` from `[f32; 3]` in sRGB colorspace. + /// New `Color` from `[f32; 3]` (or a type that can be converted into them) with RGB representation in sRGB colorspace. #[inline] pub fn rgb_from_array(arr: impl Into<[f32; 3]>) -> Self { let [r, g, b]: [f32; 3] = arr.into(); Color::rgb(r, g, b) } - /// New `Color` from `[f32; 4]` in linear RGB colorspace. + /// New `Color` from `[f32; 4]` (or a type that can be converted into them) with RGB representation in linear RGB colorspace. #[inline] pub fn rgba_linear_from_array(arr: impl Into<[f32; 4]>) -> Self { let [r, g, b, a]: [f32; 4] = arr.into(); Color::rgba_linear(r, g, b, a) } - /// New `Color` from `[f32; 3]` in linear RGB colorspace. + /// New `Color` from `[f32; 3]` (or a type that can be converted into them) with RGB representation in linear RGB colorspace. #[inline] pub fn rgb_linear_from_array(arr: impl Into<[f32; 3]>) -> Self { let [r, g, b]: [f32; 3] = arr.into(); Color::rgb_linear(r, g, b) } - /// New `Color` from `[f32; 4]` with HSL representation in sRGB colorspace. + /// New `Color` from `[f32; 4]` (or a type that can be converted into them) with HSL representation in sRGB colorspace. #[inline] pub fn hsla_from_array(arr: impl Into<[f32; 4]>) -> Self { let [h, s, l, a]: [f32; 4] = arr.into(); Color::hsla(h, s, l, a) } - /// New `Color` from `[f32; 3]` with HSL representation in sRGB colorspace. + /// New `Color` from `[f32; 3]` (or a type that can be converted into them) with HSL representation in sRGB colorspace. #[inline] pub fn hsl_from_array(arr: impl Into<[f32; 3]>) -> Self { let [h, s, l]: [f32; 3] = arr.into(); Color::hsl(h, s, l) } - /// New `Color` from `[f32; 4]` with LCH representation in sRGB colorspace. + /// New `Color` from `[f32; 4]` (or a type that can be converted into them) with LCH representation in sRGB colorspace. #[inline] pub fn lcha_from_array(arr: impl Into<[f32; 4]>) -> Self { let [l, c, h, a]: [f32; 4] = arr.into(); Color::lcha(l, c, h, a) } - /// New `Color` from `[f32; 3]` with LCH representation in sRGB colorspace. + /// New `Color` from `[f32; 3]` (or a type that can be converted into them) with LCH representation in sRGB colorspace. #[inline] pub fn lch_from_array(arr: impl Into<[f32; 3]>) -> Self { let [l, c, h]: [f32; 3] = arr.into(); From 81cf2df4833634f17bdf681548f171380d1063ce Mon Sep 17 00:00:00 2001 From: roman Date: Tue, 14 Nov 2023 09:16:45 +0300 Subject: [PATCH 8/8] Fix examples --- examples/ui/ui_material.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/ui/ui_material.rs b/examples/ui/ui_material.rs index 82a2509f83678..aa57f411923e8 100644 --- a/examples/ui/ui_material.rs +++ b/examples/ui/ui_material.rs @@ -17,7 +17,7 @@ fn update(time: Res