conversions between [u8; 4] and Color (#8564)

# 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 <49806985+MrGVSV@users.noreply.github.com>
This commit is contained in:
bird 2023-05-08 18:36:46 +02:00 committed by GitHub
parent e0a94abf1c
commit 8930cfcdd4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 {