@@ -777,16 +777,24 @@ impl Rgb {
777777 Self { r, g, b }
778778 }
779779
780- /// Parses an RGB pixel from a hex value .
780+ /// Parses an RGB pixel from a hex string .
781781 ///
782782 /// The hex value can be in one of the following formats:
783783 /// - RRGGBB
784784 /// - RGB
785785 ///
786- /// These can be optionally padded with #, for example "#FF0000" is the same as as "FF0000".
786+ /// These can be optionally padded with #, for example "#FF0000" is the same as "FF0000".
787+ ///
788+ /// # Note
789+ /// This is a relatively expensive operation. Use [`Rgb::from_u32`] if you know the hex value
790+ /// (i.e. if you are defining a constant), since you can write a `u32` using an integer literal
791+ /// of radix 16, e.g. `0xff0000`.
787792 ///
788793 /// # Errors
789794 /// * Received a malformed hex code.
795+ ///
796+ /// # See Also
797+ /// * [`Rgb::from_u32`] for parsing from an integer (better for known hex values).
790798 pub fn from_hex ( hex : impl AsRef < str > ) -> Result < Self > {
791799 let hex = hex. as_ref ( ) ;
792800
@@ -822,6 +830,26 @@ impl Rgb {
822830 } )
823831 }
824832
833+ /// Resolves the RGB pixel from a 32-bit integer.
834+ ///
835+ /// Note that RGB pixels are inherently 24-bits, so the leading 8 bits of the integer are
836+ /// ignored (they are "padding bits").
837+ ///
838+ /// # Examples
839+ /// ```
840+ /// # use ril::prelude::*;
841+ /// const RED: Rgb = Rgb::from_u32(0xff0000);
842+ /// assert_eq!(RED, Rgb::new(255, 0, 0));
843+ /// ```
844+ #[ must_use]
845+ pub const fn from_u32 ( value : u32 ) -> Self {
846+ Self {
847+ r : ( value >> 16 ) as u8 ,
848+ g : ( value >> 8 ) as u8 ,
849+ b : value as u8 ,
850+ }
851+ }
852+
825853 /// Creates a completely black pixel.
826854 #[ must_use]
827855 pub const fn black ( ) -> Self {
@@ -1020,7 +1048,7 @@ impl Rgba {
10201048 Self :: new ( r, g, b, 255 )
10211049 }
10221050
1023- /// Parses an RGBA pixel from a hex value .
1051+ /// Parses an RGBA pixel from a hex string .
10241052 ///
10251053 /// The hex value can be in one of the following formats:
10261054 /// - RRGGBBAA
@@ -1030,8 +1058,16 @@ impl Rgba {
10301058 ///
10311059 /// These can be optionally padded with #, for example "#FF0000" is the same as as "FF0000".
10321060 ///
1061+ /// # Note
1062+ /// This is a relatively expensive operation. Use [`Rgba::from_u32`] if you know the hex value
1063+ /// (i.e. if you are defining a constant), since you can write a `u32` using an integer literal
1064+ /// of radix 16, e.g. `0xff0000ff`.
1065+ ///
10331066 /// # Errors
10341067 /// * Received a malformed hex code.
1068+ ///
1069+ /// # See Also
1070+ /// * [`Rgba::from_u32`] for parsing from an integer (better for known hex values).
10351071 pub fn from_hex ( hex : & str ) -> Result < Self > {
10361072 let hex = hex. strip_prefix ( '#' ) . unwrap_or ( hex) ;
10371073
@@ -1064,6 +1100,27 @@ impl Rgba {
10641100 }
10651101 }
10661102
1103+ /// Resolves the RGBA pixel from a 32-bit integer (0xRRGGBBAA).
1104+ ///
1105+ /// Consider using [`Rgb::from_u32`] and then [`TrueColor::into_rgba`] if you have a
1106+ /// conventional 24-bit RGB integer, as this method will not properly handle that.
1107+ ///
1108+ /// # Examples
1109+ /// ```
1110+ /// # use ril::prelude::*;
1111+ /// const RED: Rgba = Rgba::from_u32(0xff0000ff);
1112+ /// assert_eq!(RED, Rgba::new(255, 0, 0, 255));
1113+ /// ```
1114+ #[ must_use]
1115+ pub const fn from_u32 ( value : u32 ) -> Self {
1116+ Self {
1117+ r : ( value >> 24 ) as u8 ,
1118+ g : ( value >> 16 ) as u8 ,
1119+ b : ( value >> 8 ) as u8 ,
1120+ a : value as u8 ,
1121+ }
1122+ }
1123+
10671124 /// Creates a completely transparent pixel.
10681125 #[ must_use]
10691126 pub const fn transparent ( ) -> Self {
0 commit comments