From 0c0125c56d82d72cd78cdd7b87632ab8234293af Mon Sep 17 00:00:00 2001 From: Azorlogh Date: Thu, 3 Mar 2022 16:10:23 +0100 Subject: [PATCH 1/3] add Color::as_rgba_u32 and Color::as_linear_rgba_u32 --- crates/bevy_render/src/color/mod.rs | 86 +++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index bdfe03dd44d77..2aedfe099a025 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -487,6 +487,92 @@ impl Color { } => [hue, saturation, lightness, alpha], } } + + /// Converts a `Color` to a `u32` from sRGB colorspace. + pub fn as_rgba_u32(self: Color) -> u32 { + match self { + Color::Rgba { + red, + green, + blue, + alpha, + } => u32::from_le_bytes([ + (red * 255.0) as u8, + (green * 255.0) as u8, + (blue * 255.0) as u8, + (alpha * 255.0) as u8, + ]), + Color::RgbaLinear { + red, + green, + blue, + alpha, + } => u32::from_le_bytes([ + (red.linear_to_nonlinear_srgb() * 255.0) as u8, + (green.linear_to_nonlinear_srgb() * 255.0) as u8, + (blue.linear_to_nonlinear_srgb() * 255.0) as u8, + (alpha * 255.0) as u8, + ]), + Color::Hsla { + hue, + saturation, + lightness, + alpha, + } => { + let [red, green, blue] = + HslRepresentation::hsl_to_nonlinear_srgb(hue, saturation, lightness); + u32::from_le_bytes([ + (red * 255.0) as u8, + (green * 255.0) as u8, + (blue * 255.0) as u8, + (alpha * 255.0) as u8, + ]) + } + } + } + + /// Converts a `Color` to a `u32` from linear RBG colorspace + pub fn as_linear_rgba_u32(self: Color) -> u32 { + match self { + Color::Rgba { + red, + green, + blue, + alpha, + } => u32::from_le_bytes([ + (red.nonlinear_to_linear_srgb() * 255.0) as u8, + (green.nonlinear_to_linear_srgb() * 255.0) as u8, + (blue.nonlinear_to_linear_srgb() * 255.0) as u8, + (alpha * 255.0) as u8, + ]), + Color::RgbaLinear { + red, + green, + blue, + alpha, + } => u32::from_le_bytes([ + (red * 255.0) as u8, + (green * 255.0) as u8, + (blue * 255.0) as u8, + (alpha * 255.0) as u8, + ]), + Color::Hsla { + hue, + saturation, + lightness, + alpha, + } => { + let [red, green, blue] = + HslRepresentation::hsl_to_nonlinear_srgb(hue, saturation, lightness); + u32::from_le_bytes([ + (red.nonlinear_to_linear_srgb() * 255.0) as u8, + (green.nonlinear_to_linear_srgb() * 255.0) as u8, + (blue.nonlinear_to_linear_srgb() * 255.0) as u8, + (alpha * 255.0) as u8, + ]) + } + } + } } impl Default for Color { From d8b1508a90df0363261a30c590fc50a774d8294f Mon Sep 17 00:00:00 2001 From: Azorlogh Date: Fri, 4 Mar 2022 09:24:17 +0100 Subject: [PATCH 2/3] fix comment typo --- crates/bevy_render/src/color/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index 2aedfe099a025..3f1eac3adabfe 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -414,7 +414,7 @@ impl Color { } } - /// Converts a `Color` to a `[f32; 4]` from linear RBG colorspace + /// Converts a `Color` to a `[f32; 4]` from linear RGB colorspace #[inline] pub fn as_linear_rgba_f32(self: Color) -> [f32; 4] { match self { @@ -531,7 +531,7 @@ impl Color { } } - /// Converts a `Color` to a `u32` from linear RBG colorspace + /// Converts a `Color` to a `u32` from linear RGB colorspace pub fn as_linear_rgba_u32(self: Color) -> u32 { match self { Color::Rgba { From a64ec668e784c6c0776a946a9ef179e099fea83d Mon Sep 17 00:00:00 2001 From: Azorlogh Date: Mon, 7 Mar 2022 09:52:20 +0100 Subject: [PATCH 3/3] improve comment --- crates/bevy_render/src/color/mod.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index 3f1eac3adabfe..834f2b3aae978 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -488,7 +488,10 @@ impl Color { } } - /// Converts a `Color` to a `u32` from sRGB colorspace. + /// Converts Color to a u32 from sRGB colorspace. + /// + /// 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_rgba_u32(self: Color) -> u32 { match self { Color::Rgba { @@ -531,7 +534,10 @@ impl Color { } } - /// Converts a `Color` to a `u32` from linear RGB colorspace + /// Converts Color to a u32 from linear RGB colorspace. + /// + /// 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_linear_rgba_u32(self: Color) -> u32 { match self { Color::Rgba {