diff --git a/crates/bevy_color/src/color.rs b/crates/bevy_color/src/color.rs index d2e4cb792187c..e9a2419efd1c5 100644 --- a/crates/bevy_color/src/color.rs +++ b/crates/bevy_color/src/color.rs @@ -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 { + 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 {