Skip to content

Add Color::srgb_u32 #14454

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
40 changes: 40 additions & 0 deletions crates/bevy_color/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,46 @@ impl Color {
})
}

/// Creates a new [`Color`] object storing a [`Srgba`] color from a [`u32`] value with an alpha of 1.0.
///
/// For example, a value of `0x000000` results in black, and a value of `0xff0000` results in red.
///
/// # Examples
///
/// ```
/// # use bevy_color::Color;
/// let black = Color::srgb_u32(0x000000);
/// let red = Color::srgb_u32(0xff0000);
/// ```
pub fn srgb_u32(color: u32) -> Self {
Self::Srgba(Srgba {
Copy link
Member

Choose a reason for hiding this comment

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

This should call an equivalent public method on Srgba :)

Copy link
Member

Choose a reason for hiding this comment

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

I still want this comment resolved :)

red: ((color >> 16) & 0xff) as f32 / 255.,
green: ((color >> 8) & 0xff) as f32 / 255.,
blue: (color & 0xff) as f32 / 255.,
alpha: 1.0,
})
}

/// Creates a new [`Color`] object storing a [`Srgba`] color from a [`u32`] value with the alpha value extracted from the input.
///
/// For example, a value of `0x000000ff` results in black with full opacity, and a value of `0xff000080` results in red with half opacity.
///
/// # Examples
///
/// ```
/// # use bevy_color::Color;
/// let black = Color::srgba_u32(0x000000ff);
/// let semi_transparent_red = Color::srgba_u32(0xff000080);
/// ```
pub fn srgba_u32(color: u32) -> Self {
Self::Srgba(Srgba {
red: ((color >> 24) & 0xff) as f32 / 255.,
green: ((color >> 16) & 0xff) as f32 / 255.,
blue: ((color >> 8) & 0xff) as f32 / 255.,
alpha: (color & 0xff) as f32 / 255.,
})
}

#[deprecated = "Use Color::linear_rgba instead."]
/// Creates a new [`Color`] object storing a [`LinearRgba`] color.
pub const fn rbga_linear(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
Expand Down