From 99be3442533a21861074e20bd1c0019ce9626e9a Mon Sep 17 00:00:00 2001 From: John Payne <20407779+johngpayne@users.noreply.github.com> Date: Fri, 7 Jun 2024 14:01:04 +0100 Subject: [PATCH 1/4] Rename Color::linear to Color::to_linear, added Color::to_srgba as the other major option. Added to_u8_array, from_u8_array and _no_alpha versions of them to ColorToComponent, implemented for SRgba and LinearRgba and left unimplemented! for other color types which don't make sense. Removed redundant extra version of to_f32_array in LinearRgba and added use ColorToComponent where that was used in other code, some extra tidy up of usage to use these new functions. --- crates/bevy_color/src/color.rs | 7 ++- crates/bevy_color/src/color_ops.rs | 9 +++ crates/bevy_color/src/hsla.rs | 16 +++++ crates/bevy_color/src/hsva.rs | 16 +++++ crates/bevy_color/src/hwba.rs | 16 +++++ crates/bevy_color/src/laba.rs | 16 +++++ crates/bevy_color/src/lcha.rs | 16 +++++ crates/bevy_color/src/linear_rgba.rs | 58 ++++++++++++++----- crates/bevy_color/src/oklaba.rs | 16 +++++ crates/bevy_color/src/oklcha.rs | 16 +++++ crates/bevy_color/src/srgba.rs | 30 ++++++---- crates/bevy_color/src/xyza.rs | 16 +++++ crates/bevy_pbr/src/fog.rs | 2 +- crates/bevy_pbr/src/render/light.rs | 1 + crates/bevy_pbr/src/volumetric_fog/mod.rs | 17 ++---- .../bevy_sprite/src/mesh2d/color_material.rs | 2 +- crates/bevy_sprite/src/render/mod.rs | 2 +- crates/bevy_ui/src/render/mod.rs | 2 +- 18 files changed, 216 insertions(+), 42 deletions(-) diff --git a/crates/bevy_color/src/color.rs b/crates/bevy_color/src/color.rs index 4ddc9599a1d3b..385c3582aa8ea 100644 --- a/crates/bevy_color/src/color.rs +++ b/crates/bevy_color/src/color.rs @@ -73,7 +73,12 @@ impl StandardColor for Color {} impl Color { /// Return the color as a linear RGBA color. - pub fn linear(&self) -> LinearRgba { + pub fn to_linear(&self) -> LinearRgba { + (*self).into() + } + + /// Return the color as an SRGBA color. + pub fn to_srgba(&self) -> Srgba { (*self).into() } diff --git a/crates/bevy_color/src/color_ops.rs b/crates/bevy_color/src/color_ops.rs index e731a6f33c5a7..46030f5909021 100644 --- a/crates/bevy_color/src/color_ops.rs +++ b/crates/bevy_color/src/color_ops.rs @@ -105,6 +105,10 @@ pub trait ColorToComponents { fn to_vec4(self) -> Vec4; /// Convert to a Vec3 fn to_vec3(self) -> Vec3; + /// Convert to [u8; 4] where that makes sense (Srgba is most relevant) + fn to_u8_array(self) -> [u8; 4]; + /// Convert to [u8; 3] where that makes sense (Srgba is most relevant) + fn to_u8_array_no_alpha(self) -> [u8; 3]; /// Convert from an f32 array fn from_f32_array(color: [f32; 4]) -> Self; /// Convert from an f32 array without the alpha value @@ -113,6 +117,11 @@ pub trait ColorToComponents { fn from_vec4(color: Vec4) -> Self; /// Convert from a Vec3 fn from_vec3(color: Vec3) -> Self; + /// Convert from [u8; 4] where that makes sense (Srgba is most relevant) + fn from_u8_array(color: [u8; 4]) -> Self; + /// Convert to [u8; 3] where that makes sense (Srgba is most relevant) + fn from_u8_array_no_alpha(color: [u8; 3]) -> Self; + } /// Utility function for interpolating hue values. This ensures that the interpolation diff --git a/crates/bevy_color/src/hsla.rs b/crates/bevy_color/src/hsla.rs index 1f49e4875941f..10bbf59118962 100644 --- a/crates/bevy_color/src/hsla.rs +++ b/crates/bevy_color/src/hsla.rs @@ -235,6 +235,22 @@ impl ColorToComponents for Hsla { alpha: 1.0, } } + + fn to_u8_array(self) -> [u8; 4] { + unimplemented!() + } + + fn to_u8_array_no_alpha(self) -> [u8; 3] { + unimplemented!() + } + + fn from_u8_array(_color: [u8; 4]) -> Self { + unimplemented!() + } + + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { + unimplemented!() + } } impl From for Hsva { diff --git a/crates/bevy_color/src/hsva.rs b/crates/bevy_color/src/hsva.rs index cf89ea1013976..c7f64df64b008 100644 --- a/crates/bevy_color/src/hsva.rs +++ b/crates/bevy_color/src/hsva.rs @@ -214,6 +214,22 @@ impl ColorToComponents for Hsva { alpha: 1.0, } } + + fn to_u8_array(self) -> [u8; 4] { + unimplemented!() + } + + fn to_u8_array_no_alpha(self) -> [u8; 3] { + unimplemented!() + } + + fn from_u8_array(_color: [u8; 4]) -> Self { + unimplemented!() + } + + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { + unimplemented!() + } } // Derived Conversions diff --git a/crates/bevy_color/src/hwba.rs b/crates/bevy_color/src/hwba.rs index 094b363f5b6a5..473e7107c610b 100644 --- a/crates/bevy_color/src/hwba.rs +++ b/crates/bevy_color/src/hwba.rs @@ -184,6 +184,22 @@ impl ColorToComponents for Hwba { alpha: 1.0, } } + + fn to_u8_array(self) -> [u8; 4] { + unimplemented!() + } + + fn to_u8_array_no_alpha(self) -> [u8; 3] { + unimplemented!() + } + + fn from_u8_array(_color: [u8; 4]) -> Self { + unimplemented!() + } + + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { + unimplemented!() + } } impl From for Hwba { diff --git a/crates/bevy_color/src/laba.rs b/crates/bevy_color/src/laba.rs index 35943aa3b903f..521daf68c9247 100644 --- a/crates/bevy_color/src/laba.rs +++ b/crates/bevy_color/src/laba.rs @@ -204,6 +204,22 @@ impl ColorToComponents for Laba { alpha: 1.0, } } + + fn to_u8_array(self) -> [u8; 4] { + unimplemented!() + } + + fn to_u8_array_no_alpha(self) -> [u8; 3] { + unimplemented!() + } + + fn from_u8_array(_color: [u8; 4]) -> Self { + unimplemented!() + } + + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { + unimplemented!() + } } impl From for Xyza { diff --git a/crates/bevy_color/src/lcha.rs b/crates/bevy_color/src/lcha.rs index ddb039f194862..ce1078776f4d0 100644 --- a/crates/bevy_color/src/lcha.rs +++ b/crates/bevy_color/src/lcha.rs @@ -243,6 +243,22 @@ impl ColorToComponents for Lcha { alpha: 1.0, } } + + fn to_u8_array(self) -> [u8; 4] { + unimplemented!() + } + + fn to_u8_array_no_alpha(self) -> [u8; 3] { + unimplemented!() + } + + fn from_u8_array(_color: [u8; 4]) -> Self { + unimplemented!() + } + + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { + unimplemented!() + } } impl From for Laba { diff --git a/crates/bevy_color/src/linear_rgba.rs b/crates/bevy_color/src/linear_rgba.rs index 041c039d9abb0..0fa7550447df6 100644 --- a/crates/bevy_color/src/linear_rgba.rs +++ b/crates/bevy_color/src/linear_rgba.rs @@ -149,24 +149,12 @@ impl LinearRgba { } } - /// 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, - ]) + u32::from_le_bytes(self.to_u8_array()) } } @@ -308,6 +296,22 @@ impl ColorToComponents for LinearRgba { alpha: 1.0, } } + + fn to_u8_array(self) -> [u8; 4] { + [self.red, self.green, self.blue, self.alpha].map(|v| (v.clamp(0.0, 1.0) * 255.0) as u8) + } + + fn to_u8_array_no_alpha(self) -> [u8; 3] { + [self.red, self.green, self.blue].map(|v| (v.clamp(0.0, 1.0) * 255.0) as u8) + } + + fn from_u8_array(color: [u8; 4]) -> Self { + Self::from_f32_array(color.map(|u| u as f32 / 255.0)) + } + + fn from_u8_array_no_alpha(color: [u8; 3]) -> Self { + Self::from_f32_array_no_alpha(color.map(|u| u as f32 / 255.0)) + } } #[cfg(feature = "wgpu-types")] @@ -416,6 +420,34 @@ mod tests { assert_eq!(a.distance_squared(&b), 1.0); } + #[test] + fn to_and_from_u8() { + // from_u8_array + let a = LinearRgba::from_u8_array([255, 0, 0, 255]); + let b = LinearRgba::new(1.0, 0.0, 0.0, 1.0); + assert_eq!(a, b); + + // from_u8_array_no_alpha + let a = LinearRgba::from_u8_array_no_alpha([255, 255, 0]); + let b = LinearRgba::rgb(1.0, 1.0, 0.0); + assert_eq!(a, b); + + // to_u8_array + let a = LinearRgba::new(0.0, 0.0, 1.0, 1.0).to_u8_array(); + let b = [0, 0, 255, 255]; + assert_eq!(a, b); + + // to_u8_array_no_alpha + let a = LinearRgba::rgb(0.0, 1.0, 1.0).to_u8_array_no_alpha(); + let b = [0, 255, 255]; + assert_eq!(a, b); + + // clamping + let a = LinearRgba::rgb(0.0, 100.0, -100.0).to_u8_array_no_alpha(); + let b = [0, 255, 0]; + assert_eq!(a, b); + } + #[test] fn darker_lighter() { // Darker and lighter should be commutative. diff --git a/crates/bevy_color/src/oklaba.rs b/crates/bevy_color/src/oklaba.rs index ec725adbdf6ba..bcafe3fbd9619 100644 --- a/crates/bevy_color/src/oklaba.rs +++ b/crates/bevy_color/src/oklaba.rs @@ -213,6 +213,22 @@ impl ColorToComponents for Oklaba { alpha: 1.0, } } + + fn to_u8_array(self) -> [u8; 4] { + unimplemented!() + } + + fn to_u8_array_no_alpha(self) -> [u8; 3] { + unimplemented!() + } + + fn from_u8_array(_color: [u8; 4]) -> Self { + unimplemented!() + } + + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { + unimplemented!() + } } #[allow(clippy::excessive_precision)] diff --git a/crates/bevy_color/src/oklcha.rs b/crates/bevy_color/src/oklcha.rs index 72b178fdc972b..24a3eab5cdb02 100644 --- a/crates/bevy_color/src/oklcha.rs +++ b/crates/bevy_color/src/oklcha.rs @@ -248,6 +248,22 @@ impl ColorToComponents for Oklcha { alpha: 1.0, } } + + fn to_u8_array(self) -> [u8; 4] { + unimplemented!() + } + + fn to_u8_array_no_alpha(self) -> [u8; 3] { + unimplemented!() + } + + fn from_u8_array(_color: [u8; 4]) -> Self { + unimplemented!() + } + + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { + unimplemented!() + } } impl From for Oklcha { diff --git a/crates/bevy_color/src/srgba.rs b/crates/bevy_color/src/srgba.rs index 9de33883af82d..87eff6f927414 100644 --- a/crates/bevy_color/src/srgba.rs +++ b/crates/bevy_color/src/srgba.rs @@ -168,10 +168,7 @@ impl Srgba { /// Convert this color to CSS-style hexadecimal notation. pub fn to_hex(&self) -> String { - let r = (self.red * 255.0).round() as u8; - let g = (self.green * 255.0).round() as u8; - let b = (self.blue * 255.0).round() as u8; - let a = (self.alpha * 255.0).round() as u8; + let [r, g, b, a] = self.to_u8_array(); match a { 255 => format!("#{:02X}{:02X}{:02X}", r, g, b), _ => format!("#{:02X}{:02X}{:02X}{:02X}", r, g, b, a), @@ -189,7 +186,7 @@ impl Srgba { /// See also [`Srgba::new`], [`Srgba::rgba_u8`], [`Srgba::hex`]. /// pub fn rgb_u8(r: u8, g: u8, b: u8) -> Self { - Self::rgba_u8(r, g, b, u8::MAX) + Self::from_u8_array_no_alpha([r, g, b]) } // Float operations in const fn are not stable yet @@ -206,12 +203,7 @@ impl Srgba { /// See also [`Srgba::new`], [`Srgba::rgb_u8`], [`Srgba::hex`]. /// pub fn rgba_u8(r: u8, g: u8, b: u8, a: u8) -> Self { - Self::new( - r as f32 / u8::MAX as f32, - g as f32 / u8::MAX as f32, - b as f32 / u8::MAX as f32, - a as f32 / u8::MAX as f32, - ) + Self::from_u8_array([r, g, b, a]) } /// Converts a non-linear sRGB value to a linear one via [gamma correction](https://en.wikipedia.org/wiki/Gamma_correction). @@ -371,6 +363,22 @@ impl ColorToComponents for Srgba { alpha: 1.0, } } + + fn to_u8_array(self) -> [u8; 4] { + [self.red, self.green, self.blue, self.alpha].map(|v| (v.clamp(0.0, 1.0) * 255.0) as u8) + } + + fn to_u8_array_no_alpha(self) -> [u8; 3] { + [self.red, self.green, self.blue].map(|v| (v.clamp(0.0, 1.0) * 255.0) as u8) + } + + fn from_u8_array(color: [u8; 4]) -> Self { + Self::from_f32_array(color.map(|u| u as f32 / 255.0)) + } + + fn from_u8_array_no_alpha(color: [u8; 3]) -> Self { + Self::from_f32_array_no_alpha(color.map(|u| u as f32 / 255.0)) + } } impl From for Srgba { diff --git a/crates/bevy_color/src/xyza.rs b/crates/bevy_color/src/xyza.rs index ea20a2ee72fe4..7c7656225c00e 100644 --- a/crates/bevy_color/src/xyza.rs +++ b/crates/bevy_color/src/xyza.rs @@ -201,6 +201,22 @@ impl ColorToComponents for Xyza { alpha: 1.0, } } + + fn to_u8_array(self) -> [u8; 4] { + unimplemented!() + } + + fn to_u8_array_no_alpha(self) -> [u8; 3] { + unimplemented!() + } + + fn from_u8_array(_color: [u8; 4]) -> Self { + unimplemented!() + } + + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { + unimplemented!() + } } impl From for Xyza { diff --git a/crates/bevy_pbr/src/fog.rs b/crates/bevy_pbr/src/fog.rs index 8ed7533a6c043..8ea4eadb59516 100644 --- a/crates/bevy_pbr/src/fog.rs +++ b/crates/bevy_pbr/src/fog.rs @@ -1,4 +1,4 @@ -use bevy_color::{Color, LinearRgba}; +use bevy_color::{Color, LinearRgba, ColorToComponents}; use bevy_ecs::prelude::*; use bevy_math::Vec3; use bevy_reflect::{std_traits::ReflectDefault, Reflect}; diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index ded628c5b33ee..0f17c2cabc8f9 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -1,4 +1,5 @@ use bevy_asset::AssetId; +use bevy_color::ColorToComponents; use bevy_core_pipeline::core_3d::CORE_3D_DEPTH_FORMAT; use bevy_ecs::entity::EntityHashSet; use bevy_ecs::prelude::*; diff --git a/crates/bevy_pbr/src/volumetric_fog/mod.rs b/crates/bevy_pbr/src/volumetric_fog/mod.rs index acc3b303c7445..9ff0a5d45a616 100644 --- a/crates/bevy_pbr/src/volumetric_fog/mod.rs +++ b/crates/bevy_pbr/src/volumetric_fog/mod.rs @@ -31,7 +31,7 @@ use bevy_app::{App, Plugin}; use bevy_asset::{load_internal_asset, Handle}; -use bevy_color::Color; +use bevy_color::{ColorToComponents, Color}; use bevy_core_pipeline::{ core_3d::{ graph::{Core3d, Node3d}, @@ -617,18 +617,9 @@ pub fn prepare_volumetric_fog_uniforms( for (entity, volumetric_fog_settings) in view_targets.iter() { let offset = writer.write(&VolumetricFogUniform { - fog_color: Vec3::from_slice( - &volumetric_fog_settings.fog_color.linear().to_f32_array()[0..3], - ), - light_tint: Vec3::from_slice( - &volumetric_fog_settings.light_tint.linear().to_f32_array()[0..3], - ), - ambient_color: Vec3::from_slice( - &volumetric_fog_settings - .ambient_color - .linear() - .to_f32_array()[0..3], - ), + fog_color: volumetric_fog_settings.fog_color.to_linear().to_vec3(), + light_tint: volumetric_fog_settings.light_tint.to_linear().to_vec3(), + ambient_color: volumetric_fog_settings.ambient_color.to_linear().to_vec3(), ambient_intensity: volumetric_fog_settings.ambient_intensity, step_count: volumetric_fog_settings.step_count, max_depth: volumetric_fog_settings.max_depth, diff --git a/crates/bevy_sprite/src/mesh2d/color_material.rs b/crates/bevy_sprite/src/mesh2d/color_material.rs index 9bf6581b8848f..71e45dd844338 100644 --- a/crates/bevy_sprite/src/mesh2d/color_material.rs +++ b/crates/bevy_sprite/src/mesh2d/color_material.rs @@ -1,7 +1,7 @@ use crate::{Material2d, Material2dPlugin, MaterialMesh2dBundle}; use bevy_app::{App, Plugin}; use bevy_asset::{load_internal_asset, Asset, AssetApp, Assets, Handle}; -use bevy_color::{Color, LinearRgba}; +use bevy_color::{Color, LinearRgba, ColorToComponents}; use bevy_math::Vec4; use bevy_reflect::prelude::*; use bevy_render::{ diff --git a/crates/bevy_sprite/src/render/mod.rs b/crates/bevy_sprite/src/render/mod.rs index 64dda440be2a0..0e7efc1f75c23 100644 --- a/crates/bevy_sprite/src/render/mod.rs +++ b/crates/bevy_sprite/src/render/mod.rs @@ -5,7 +5,7 @@ use crate::{ ComputedTextureSlices, Sprite, WithSprite, SPRITE_SHADER_HANDLE, }; use bevy_asset::{AssetEvent, AssetId, Assets, Handle}; -use bevy_color::LinearRgba; +use bevy_color::{ColorToComponents, LinearRgba}; use bevy_core_pipeline::{ core_2d::Transparent2d, tonemapping::{ diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index 33f7443787225..410f3aa07d439 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -2,7 +2,7 @@ mod pipeline; mod render_pass; mod ui_material_pipeline; -use bevy_color::{Alpha, LinearRgba}; +use bevy_color::{Alpha, LinearRgba, ColorToComponents}; use bevy_core_pipeline::core_2d::graph::{Core2d, Node2d}; use bevy_core_pipeline::core_3d::graph::{Core3d, Node3d}; use bevy_core_pipeline::{core_2d::Camera2d, core_3d::Camera3d}; From 38d62aced5342e79acd04e0ba9254a6a6829d36e Mon Sep 17 00:00:00 2001 From: John Payne <20407779+johngpayne@users.noreply.github.com> Date: Fri, 7 Jun 2024 18:56:42 +0100 Subject: [PATCH 2/4] round float before converting to u8 to to_u8_array --- crates/bevy_color/src/linear_rgba.rs | 4 ++-- crates/bevy_color/src/srgba.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/bevy_color/src/linear_rgba.rs b/crates/bevy_color/src/linear_rgba.rs index 0fa7550447df6..79ecd73224bfc 100644 --- a/crates/bevy_color/src/linear_rgba.rs +++ b/crates/bevy_color/src/linear_rgba.rs @@ -298,11 +298,11 @@ impl ColorToComponents for LinearRgba { } fn to_u8_array(self) -> [u8; 4] { - [self.red, self.green, self.blue, self.alpha].map(|v| (v.clamp(0.0, 1.0) * 255.0) as u8) + [self.red, self.green, self.blue, self.alpha].map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8) } fn to_u8_array_no_alpha(self) -> [u8; 3] { - [self.red, self.green, self.blue].map(|v| (v.clamp(0.0, 1.0) * 255.0) as u8) + [self.red, self.green, self.blue].map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8) } fn from_u8_array(color: [u8; 4]) -> Self { diff --git a/crates/bevy_color/src/srgba.rs b/crates/bevy_color/src/srgba.rs index 87eff6f927414..fc78b67192b9e 100644 --- a/crates/bevy_color/src/srgba.rs +++ b/crates/bevy_color/src/srgba.rs @@ -365,11 +365,11 @@ impl ColorToComponents for Srgba { } fn to_u8_array(self) -> [u8; 4] { - [self.red, self.green, self.blue, self.alpha].map(|v| (v.clamp(0.0, 1.0) * 255.0) as u8) + [self.red, self.green, self.blue, self.alpha].map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8) } fn to_u8_array_no_alpha(self) -> [u8; 3] { - [self.red, self.green, self.blue].map(|v| (v.clamp(0.0, 1.0) * 255.0) as u8) + [self.red, self.green, self.blue].map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8) } fn from_u8_array(color: [u8; 4]) -> Self { From 32c0dc41451c9a857ef8ead4688c0593f875a172 Mon Sep 17 00:00:00 2001 From: John Payne <20407779+johngpayne@users.noreply.github.com> Date: Fri, 7 Jun 2024 20:49:08 +0100 Subject: [PATCH 3/4] cargo fmt --- crates/bevy_color/src/color_ops.rs | 1 - crates/bevy_color/src/hsla.rs | 4 ++-- crates/bevy_color/src/hsva.rs | 4 ++-- crates/bevy_color/src/hwba.rs | 4 ++-- crates/bevy_color/src/laba.rs | 4 ++-- crates/bevy_color/src/lcha.rs | 4 ++-- crates/bevy_color/src/linear_rgba.rs | 3 ++- crates/bevy_color/src/oklaba.rs | 8 ++++---- crates/bevy_color/src/oklcha.rs | 8 ++++---- crates/bevy_color/src/srgba.rs | 3 ++- crates/bevy_color/src/xyza.rs | 8 ++++---- crates/bevy_pbr/src/fog.rs | 2 +- crates/bevy_pbr/src/volumetric_fog/mod.rs | 2 +- crates/bevy_sprite/src/mesh2d/color_material.rs | 2 +- crates/bevy_ui/src/render/mod.rs | 2 +- 15 files changed, 30 insertions(+), 29 deletions(-) diff --git a/crates/bevy_color/src/color_ops.rs b/crates/bevy_color/src/color_ops.rs index 46030f5909021..4f04e962b873e 100644 --- a/crates/bevy_color/src/color_ops.rs +++ b/crates/bevy_color/src/color_ops.rs @@ -121,7 +121,6 @@ pub trait ColorToComponents { fn from_u8_array(color: [u8; 4]) -> Self; /// Convert to [u8; 3] where that makes sense (Srgba is most relevant) fn from_u8_array_no_alpha(color: [u8; 3]) -> Self; - } /// Utility function for interpolating hue values. This ensures that the interpolation diff --git a/crates/bevy_color/src/hsla.rs b/crates/bevy_color/src/hsla.rs index 10bbf59118962..28b0bc8c5e1aa 100644 --- a/crates/bevy_color/src/hsla.rs +++ b/crates/bevy_color/src/hsla.rs @@ -243,11 +243,11 @@ impl ColorToComponents for Hsla { fn to_u8_array_no_alpha(self) -> [u8; 3] { unimplemented!() } - + fn from_u8_array(_color: [u8; 4]) -> Self { unimplemented!() } - + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { unimplemented!() } diff --git a/crates/bevy_color/src/hsva.rs b/crates/bevy_color/src/hsva.rs index c7f64df64b008..d78340a013f0b 100644 --- a/crates/bevy_color/src/hsva.rs +++ b/crates/bevy_color/src/hsva.rs @@ -222,11 +222,11 @@ impl ColorToComponents for Hsva { fn to_u8_array_no_alpha(self) -> [u8; 3] { unimplemented!() } - + fn from_u8_array(_color: [u8; 4]) -> Self { unimplemented!() } - + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { unimplemented!() } diff --git a/crates/bevy_color/src/hwba.rs b/crates/bevy_color/src/hwba.rs index 473e7107c610b..a3b6b1922ea34 100644 --- a/crates/bevy_color/src/hwba.rs +++ b/crates/bevy_color/src/hwba.rs @@ -192,11 +192,11 @@ impl ColorToComponents for Hwba { fn to_u8_array_no_alpha(self) -> [u8; 3] { unimplemented!() } - + fn from_u8_array(_color: [u8; 4]) -> Self { unimplemented!() } - + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { unimplemented!() } diff --git a/crates/bevy_color/src/laba.rs b/crates/bevy_color/src/laba.rs index 521daf68c9247..8c1201e5526fd 100644 --- a/crates/bevy_color/src/laba.rs +++ b/crates/bevy_color/src/laba.rs @@ -212,11 +212,11 @@ impl ColorToComponents for Laba { fn to_u8_array_no_alpha(self) -> [u8; 3] { unimplemented!() } - + fn from_u8_array(_color: [u8; 4]) -> Self { unimplemented!() } - + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { unimplemented!() } diff --git a/crates/bevy_color/src/lcha.rs b/crates/bevy_color/src/lcha.rs index ce1078776f4d0..d08128e410402 100644 --- a/crates/bevy_color/src/lcha.rs +++ b/crates/bevy_color/src/lcha.rs @@ -251,11 +251,11 @@ impl ColorToComponents for Lcha { fn to_u8_array_no_alpha(self) -> [u8; 3] { unimplemented!() } - + fn from_u8_array(_color: [u8; 4]) -> Self { unimplemented!() } - + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { unimplemented!() } diff --git a/crates/bevy_color/src/linear_rgba.rs b/crates/bevy_color/src/linear_rgba.rs index 79ecd73224bfc..3cb43a9126068 100644 --- a/crates/bevy_color/src/linear_rgba.rs +++ b/crates/bevy_color/src/linear_rgba.rs @@ -298,7 +298,8 @@ impl ColorToComponents for LinearRgba { } fn to_u8_array(self) -> [u8; 4] { - [self.red, self.green, self.blue, self.alpha].map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8) + [self.red, self.green, self.blue, self.alpha] + .map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8) } fn to_u8_array_no_alpha(self) -> [u8; 3] { diff --git a/crates/bevy_color/src/oklaba.rs b/crates/bevy_color/src/oklaba.rs index bcafe3fbd9619..2183095febedd 100644 --- a/crates/bevy_color/src/oklaba.rs +++ b/crates/bevy_color/src/oklaba.rs @@ -213,19 +213,19 @@ impl ColorToComponents for Oklaba { alpha: 1.0, } } - + fn to_u8_array(self) -> [u8; 4] { unimplemented!() } - + fn to_u8_array_no_alpha(self) -> [u8; 3] { unimplemented!() } - + fn from_u8_array(_color: [u8; 4]) -> Self { unimplemented!() } - + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { unimplemented!() } diff --git a/crates/bevy_color/src/oklcha.rs b/crates/bevy_color/src/oklcha.rs index 24a3eab5cdb02..78eb2c74bbef8 100644 --- a/crates/bevy_color/src/oklcha.rs +++ b/crates/bevy_color/src/oklcha.rs @@ -248,19 +248,19 @@ impl ColorToComponents for Oklcha { alpha: 1.0, } } - + fn to_u8_array(self) -> [u8; 4] { unimplemented!() } - + fn to_u8_array_no_alpha(self) -> [u8; 3] { unimplemented!() } - + fn from_u8_array(_color: [u8; 4]) -> Self { unimplemented!() } - + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { unimplemented!() } diff --git a/crates/bevy_color/src/srgba.rs b/crates/bevy_color/src/srgba.rs index fc78b67192b9e..8e7ab808c2bf0 100644 --- a/crates/bevy_color/src/srgba.rs +++ b/crates/bevy_color/src/srgba.rs @@ -365,7 +365,8 @@ impl ColorToComponents for Srgba { } fn to_u8_array(self) -> [u8; 4] { - [self.red, self.green, self.blue, self.alpha].map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8) + [self.red, self.green, self.blue, self.alpha] + .map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8) } fn to_u8_array_no_alpha(self) -> [u8; 3] { diff --git a/crates/bevy_color/src/xyza.rs b/crates/bevy_color/src/xyza.rs index 7c7656225c00e..ea4e49c8795cb 100644 --- a/crates/bevy_color/src/xyza.rs +++ b/crates/bevy_color/src/xyza.rs @@ -201,19 +201,19 @@ impl ColorToComponents for Xyza { alpha: 1.0, } } - + fn to_u8_array(self) -> [u8; 4] { unimplemented!() } - + fn to_u8_array_no_alpha(self) -> [u8; 3] { unimplemented!() } - + fn from_u8_array(_color: [u8; 4]) -> Self { unimplemented!() } - + fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { unimplemented!() } diff --git a/crates/bevy_pbr/src/fog.rs b/crates/bevy_pbr/src/fog.rs index 8ea4eadb59516..5567783274f00 100644 --- a/crates/bevy_pbr/src/fog.rs +++ b/crates/bevy_pbr/src/fog.rs @@ -1,4 +1,4 @@ -use bevy_color::{Color, LinearRgba, ColorToComponents}; +use bevy_color::{Color, ColorToComponents, LinearRgba}; use bevy_ecs::prelude::*; use bevy_math::Vec3; use bevy_reflect::{std_traits::ReflectDefault, Reflect}; diff --git a/crates/bevy_pbr/src/volumetric_fog/mod.rs b/crates/bevy_pbr/src/volumetric_fog/mod.rs index 9ff0a5d45a616..170d38cb21f23 100644 --- a/crates/bevy_pbr/src/volumetric_fog/mod.rs +++ b/crates/bevy_pbr/src/volumetric_fog/mod.rs @@ -31,7 +31,7 @@ use bevy_app::{App, Plugin}; use bevy_asset::{load_internal_asset, Handle}; -use bevy_color::{ColorToComponents, Color}; +use bevy_color::{Color, ColorToComponents}; use bevy_core_pipeline::{ core_3d::{ graph::{Core3d, Node3d}, diff --git a/crates/bevy_sprite/src/mesh2d/color_material.rs b/crates/bevy_sprite/src/mesh2d/color_material.rs index 71e45dd844338..78061ce06d0c6 100644 --- a/crates/bevy_sprite/src/mesh2d/color_material.rs +++ b/crates/bevy_sprite/src/mesh2d/color_material.rs @@ -1,7 +1,7 @@ use crate::{Material2d, Material2dPlugin, MaterialMesh2dBundle}; use bevy_app::{App, Plugin}; use bevy_asset::{load_internal_asset, Asset, AssetApp, Assets, Handle}; -use bevy_color::{Color, LinearRgba, ColorToComponents}; +use bevy_color::{Color, ColorToComponents, LinearRgba}; use bevy_math::Vec4; use bevy_reflect::prelude::*; use bevy_render::{ diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index 410f3aa07d439..d05c0b541e00e 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -2,7 +2,7 @@ mod pipeline; mod render_pass; mod ui_material_pipeline; -use bevy_color::{Alpha, LinearRgba, ColorToComponents}; +use bevy_color::{Alpha, ColorToComponents, LinearRgba}; use bevy_core_pipeline::core_2d::graph::{Core2d, Node2d}; use bevy_core_pipeline::core_3d::graph::{Core3d, Node3d}; use bevy_core_pipeline::{core_2d::Camera2d, core_3d::Camera3d}; From 5207210141a68a35f8e032f4d9470b0c96936dc7 Mon Sep 17 00:00:00 2001 From: John Payne <20407779+johngpayne@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:09:32 +0100 Subject: [PATCH 4/4] New trait ColorToPacked for the u8 conversion functions, means all the other color types dont need unimplemented versions. --- crates/bevy_color/src/color_ops.rs | 12 ++++++++---- crates/bevy_color/src/hsla.rs | 16 ---------------- crates/bevy_color/src/hsva.rs | 16 ---------------- crates/bevy_color/src/hwba.rs | 16 ---------------- crates/bevy_color/src/laba.rs | 16 ---------------- crates/bevy_color/src/lcha.rs | 16 ---------------- crates/bevy_color/src/linear_rgba.rs | 4 +++- crates/bevy_color/src/oklaba.rs | 16 ---------------- crates/bevy_color/src/oklcha.rs | 16 ---------------- crates/bevy_color/src/srgba.rs | 6 ++++-- crates/bevy_color/src/xyza.rs | 16 ---------------- 11 files changed, 15 insertions(+), 135 deletions(-) diff --git a/crates/bevy_color/src/color_ops.rs b/crates/bevy_color/src/color_ops.rs index 4f04e962b873e..32fdb83ba2ede 100644 --- a/crates/bevy_color/src/color_ops.rs +++ b/crates/bevy_color/src/color_ops.rs @@ -105,10 +105,6 @@ pub trait ColorToComponents { fn to_vec4(self) -> Vec4; /// Convert to a Vec3 fn to_vec3(self) -> Vec3; - /// Convert to [u8; 4] where that makes sense (Srgba is most relevant) - fn to_u8_array(self) -> [u8; 4]; - /// Convert to [u8; 3] where that makes sense (Srgba is most relevant) - fn to_u8_array_no_alpha(self) -> [u8; 3]; /// Convert from an f32 array fn from_f32_array(color: [f32; 4]) -> Self; /// Convert from an f32 array without the alpha value @@ -117,6 +113,14 @@ pub trait ColorToComponents { fn from_vec4(color: Vec4) -> Self; /// Convert from a Vec3 fn from_vec3(color: Vec3) -> Self; +} + +/// Trait with methods for converting colors to packed non-color types +pub trait ColorToPacked { + /// Convert to [u8; 4] where that makes sense (Srgba is most relevant) + fn to_u8_array(self) -> [u8; 4]; + /// Convert to [u8; 3] where that makes sense (Srgba is most relevant) + fn to_u8_array_no_alpha(self) -> [u8; 3]; /// Convert from [u8; 4] where that makes sense (Srgba is most relevant) fn from_u8_array(color: [u8; 4]) -> Self; /// Convert to [u8; 3] where that makes sense (Srgba is most relevant) diff --git a/crates/bevy_color/src/hsla.rs b/crates/bevy_color/src/hsla.rs index 28b0bc8c5e1aa..1f49e4875941f 100644 --- a/crates/bevy_color/src/hsla.rs +++ b/crates/bevy_color/src/hsla.rs @@ -235,22 +235,6 @@ impl ColorToComponents for Hsla { alpha: 1.0, } } - - fn to_u8_array(self) -> [u8; 4] { - unimplemented!() - } - - fn to_u8_array_no_alpha(self) -> [u8; 3] { - unimplemented!() - } - - fn from_u8_array(_color: [u8; 4]) -> Self { - unimplemented!() - } - - fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { - unimplemented!() - } } impl From for Hsva { diff --git a/crates/bevy_color/src/hsva.rs b/crates/bevy_color/src/hsva.rs index d78340a013f0b..cf89ea1013976 100644 --- a/crates/bevy_color/src/hsva.rs +++ b/crates/bevy_color/src/hsva.rs @@ -214,22 +214,6 @@ impl ColorToComponents for Hsva { alpha: 1.0, } } - - fn to_u8_array(self) -> [u8; 4] { - unimplemented!() - } - - fn to_u8_array_no_alpha(self) -> [u8; 3] { - unimplemented!() - } - - fn from_u8_array(_color: [u8; 4]) -> Self { - unimplemented!() - } - - fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { - unimplemented!() - } } // Derived Conversions diff --git a/crates/bevy_color/src/hwba.rs b/crates/bevy_color/src/hwba.rs index a3b6b1922ea34..094b363f5b6a5 100644 --- a/crates/bevy_color/src/hwba.rs +++ b/crates/bevy_color/src/hwba.rs @@ -184,22 +184,6 @@ impl ColorToComponents for Hwba { alpha: 1.0, } } - - fn to_u8_array(self) -> [u8; 4] { - unimplemented!() - } - - fn to_u8_array_no_alpha(self) -> [u8; 3] { - unimplemented!() - } - - fn from_u8_array(_color: [u8; 4]) -> Self { - unimplemented!() - } - - fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { - unimplemented!() - } } impl From for Hwba { diff --git a/crates/bevy_color/src/laba.rs b/crates/bevy_color/src/laba.rs index 8c1201e5526fd..35943aa3b903f 100644 --- a/crates/bevy_color/src/laba.rs +++ b/crates/bevy_color/src/laba.rs @@ -204,22 +204,6 @@ impl ColorToComponents for Laba { alpha: 1.0, } } - - fn to_u8_array(self) -> [u8; 4] { - unimplemented!() - } - - fn to_u8_array_no_alpha(self) -> [u8; 3] { - unimplemented!() - } - - fn from_u8_array(_color: [u8; 4]) -> Self { - unimplemented!() - } - - fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { - unimplemented!() - } } impl From for Xyza { diff --git a/crates/bevy_color/src/lcha.rs b/crates/bevy_color/src/lcha.rs index d08128e410402..ddb039f194862 100644 --- a/crates/bevy_color/src/lcha.rs +++ b/crates/bevy_color/src/lcha.rs @@ -243,22 +243,6 @@ impl ColorToComponents for Lcha { alpha: 1.0, } } - - fn to_u8_array(self) -> [u8; 4] { - unimplemented!() - } - - fn to_u8_array_no_alpha(self) -> [u8; 3] { - unimplemented!() - } - - fn from_u8_array(_color: [u8; 4]) -> Self { - unimplemented!() - } - - fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { - unimplemented!() - } } impl From for Laba { diff --git a/crates/bevy_color/src/linear_rgba.rs b/crates/bevy_color/src/linear_rgba.rs index 3cb43a9126068..2dafa15eed798 100644 --- a/crates/bevy_color/src/linear_rgba.rs +++ b/crates/bevy_color/src/linear_rgba.rs @@ -1,6 +1,6 @@ use crate::{ color_difference::EuclideanDistance, impl_componentwise_vector_space, Alpha, ColorToComponents, - Gray, Luminance, Mix, StandardColor, + ColorToPacked, Gray, Luminance, Mix, StandardColor, }; use bevy_math::{Vec3, Vec4}; use bevy_reflect::prelude::*; @@ -296,7 +296,9 @@ impl ColorToComponents for LinearRgba { alpha: 1.0, } } +} +impl ColorToPacked for LinearRgba { fn to_u8_array(self) -> [u8; 4] { [self.red, self.green, self.blue, self.alpha] .map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8) diff --git a/crates/bevy_color/src/oklaba.rs b/crates/bevy_color/src/oklaba.rs index 2183095febedd..ec725adbdf6ba 100644 --- a/crates/bevy_color/src/oklaba.rs +++ b/crates/bevy_color/src/oklaba.rs @@ -213,22 +213,6 @@ impl ColorToComponents for Oklaba { alpha: 1.0, } } - - fn to_u8_array(self) -> [u8; 4] { - unimplemented!() - } - - fn to_u8_array_no_alpha(self) -> [u8; 3] { - unimplemented!() - } - - fn from_u8_array(_color: [u8; 4]) -> Self { - unimplemented!() - } - - fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { - unimplemented!() - } } #[allow(clippy::excessive_precision)] diff --git a/crates/bevy_color/src/oklcha.rs b/crates/bevy_color/src/oklcha.rs index 78eb2c74bbef8..72b178fdc972b 100644 --- a/crates/bevy_color/src/oklcha.rs +++ b/crates/bevy_color/src/oklcha.rs @@ -248,22 +248,6 @@ impl ColorToComponents for Oklcha { alpha: 1.0, } } - - fn to_u8_array(self) -> [u8; 4] { - unimplemented!() - } - - fn to_u8_array_no_alpha(self) -> [u8; 3] { - unimplemented!() - } - - fn from_u8_array(_color: [u8; 4]) -> Self { - unimplemented!() - } - - fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { - unimplemented!() - } } impl From for Oklcha { diff --git a/crates/bevy_color/src/srgba.rs b/crates/bevy_color/src/srgba.rs index 8e7ab808c2bf0..f8396a93944ec 100644 --- a/crates/bevy_color/src/srgba.rs +++ b/crates/bevy_color/src/srgba.rs @@ -1,7 +1,7 @@ use crate::color_difference::EuclideanDistance; use crate::{ - impl_componentwise_vector_space, Alpha, ColorToComponents, Gray, LinearRgba, Luminance, Mix, - StandardColor, Xyza, + impl_componentwise_vector_space, Alpha, ColorToComponents, ColorToPacked, Gray, LinearRgba, + Luminance, Mix, StandardColor, Xyza, }; use bevy_math::{Vec3, Vec4}; use bevy_reflect::prelude::*; @@ -363,7 +363,9 @@ impl ColorToComponents for Srgba { alpha: 1.0, } } +} +impl ColorToPacked for Srgba { fn to_u8_array(self) -> [u8; 4] { [self.red, self.green, self.blue, self.alpha] .map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8) diff --git a/crates/bevy_color/src/xyza.rs b/crates/bevy_color/src/xyza.rs index ea4e49c8795cb..ea20a2ee72fe4 100644 --- a/crates/bevy_color/src/xyza.rs +++ b/crates/bevy_color/src/xyza.rs @@ -201,22 +201,6 @@ impl ColorToComponents for Xyza { alpha: 1.0, } } - - fn to_u8_array(self) -> [u8; 4] { - unimplemented!() - } - - fn to_u8_array_no_alpha(self) -> [u8; 3] { - unimplemented!() - } - - fn from_u8_array(_color: [u8; 4]) -> Self { - unimplemented!() - } - - fn from_u8_array_no_alpha(_color: [u8; 3]) -> Self { - unimplemented!() - } } impl From for Xyza {