Skip to content

Commit

Permalink
conversions between [u8; 4] and Color (#8564)
Browse files Browse the repository at this point in the history
# Objective

- Fixes #8563

## Solution

~~- Implement From<Color> 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 <[email protected]>
  • Loading branch information
atornity and MrGVSV authored May 8, 2023
1 parent e0a94ab commit 8930cfc
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions crates/bevy_render/src/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8930cfc

Please sign in to comment.