Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add conversions from Color to u32 #4088

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 87 additions & 1 deletion crates/bevy_render/src/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -487,6 +487,92 @@ impl Color {
} => [hue, saturation, lightness, alpha],
}
}

/// Converts a `Color` to a `u32` from sRGB colorspace.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it makes sense to describe how the colour is converted to u32 (each channel as a u8, order as alpha-blue-green-red) or is it standard enough to do it that way?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be described. Perhaps both endianness (it wasn’t obvious to me a while ago that GPUs were generally little endian) and order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I change the comment to the following?
Converts Color to a u32 from sRGB colorspace, mapping the RGBA channels in this order to a little-endian byte array (meaning A will be the most significant byte and R the least significant).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A will be the most significant byte

that took me two reads to understand, with "will" being also a noun 😄

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 RGB 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 {
Expand Down