From 8930cfcdd49e926e039be282f7551f9b561159b1 Mon Sep 17 00:00:00 2001 From: bird Date: Mon, 8 May 2023 18:36:46 +0200 Subject: [PATCH] conversions between [u8; 4] and Color (#8564) # Objective - Fixes #8563 ## Solution ~~- Implement From for [u8; 4]~~ ~~- also implement From<[u8; 4]> for Color because why not.~~ - implement method `as_rgba_u8` in Color --------- Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com> --- crates/bevy_render/src/color/mod.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index d66a455cba..b5f1fb9198 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -683,6 +683,17 @@ impl Color { } } + /// Converts a `Color` to a `[u8; 4]` from sRGB colorspace + pub fn as_rgba_u8(&self) -> [u8; 4] { + let [r, g, b, a] = self.as_rgba_f32(); + [ + (r * u8::MAX as f32) as u8, + (g * u8::MAX as f32) as u8, + (b * u8::MAX as f32) as u8, + (a * u8::MAX as f32) as u8, + ] + } + /// Converts a `Color` to a `[f32; 4]` from sRGB colorspace pub fn as_rgba_f32(self: Color) -> [f32; 4] { match self {