@@ -41,12 +41,27 @@ impl Rgba8 {
41
41
[ self . r , self . g , self . b , self . a ]
42
42
}
43
43
44
- /// Returns the color as a little endian packed value, with `a` as the
45
- /// most significant byte and `r` the least.
44
+ /// Convert the `[u8; 4]` byte array into an `Rgba8` color.
45
+ ///
46
+ /// The color values must be given in the order `[r, g, b, a]`.
47
+ #[ must_use]
48
+ pub const fn from_u8_array ( [ r, g, b, a] : [ u8 ; 4 ] ) -> Self {
49
+ Self { r, g, b, a }
50
+ }
51
+
52
+ /// Returns the color as a little-endian packed value, with `r` the least significant byte and
53
+ /// `a` the most significant.
46
54
#[ must_use]
47
55
pub const fn to_u32 ( self ) -> u32 {
48
56
u32:: from_ne_bytes ( self . to_u8_array ( ) )
49
57
}
58
+
59
+ /// Interpret the little-endian packed value as a color, with `r` the least significant byte
60
+ /// and `a` the most significant.
61
+ #[ must_use]
62
+ pub const fn from_u32 ( packed_bytes : u32 ) -> Self {
63
+ Self :: from_u8_array ( u32:: to_ne_bytes ( packed_bytes) )
64
+ }
50
65
}
51
66
52
67
impl From < Rgba8 > for AlphaColor < Srgb > {
@@ -90,12 +105,27 @@ impl PremulRgba8 {
90
105
[ self . r , self . g , self . b , self . a ]
91
106
}
92
107
93
- /// Returns the color as a little endian packed value, with `a` as the
94
- /// most significant byte and `r` the least.
108
+ /// Convert the `[u8; 4]` byte array into a `PremulRgba8` color.
109
+ ///
110
+ /// The color values must be given in the order `[r, g, b, a]`.
111
+ #[ must_use]
112
+ pub const fn from_u8_array ( [ r, g, b, a] : [ u8 ; 4 ] ) -> Self {
113
+ Self { r, g, b, a }
114
+ }
115
+
116
+ /// Returns the color as a little-endian packed value, with `r` the least significant byte and
117
+ /// `a` the most significant.
95
118
#[ must_use]
96
119
pub const fn to_u32 ( self ) -> u32 {
97
120
u32:: from_ne_bytes ( self . to_u8_array ( ) )
98
121
}
122
+
123
+ /// Interpret the little-endian packed value as a color, with `r` the least significant byte
124
+ /// and `a` the most significant.
125
+ #[ must_use]
126
+ pub const fn from_u32 ( packed_bytes : u32 ) -> Self {
127
+ Self :: from_u8_array ( u32:: to_ne_bytes ( packed_bytes) )
128
+ }
99
129
}
100
130
101
131
/// This is deprecated and will be removed in 0.3.0.
@@ -134,6 +164,25 @@ mod tests {
134
164
assert_eq ! ( 0xffccbbaa_u32 . to_le( ) , p. to_u32( ) ) ;
135
165
}
136
166
167
+ #[ test]
168
+ fn from_u32 ( ) {
169
+ let c = Rgba8 {
170
+ r : 1 ,
171
+ g : 2 ,
172
+ b : 3 ,
173
+ a : 4 ,
174
+ } ;
175
+ assert_eq ! ( Rgba8 :: from_u32( 0x04030201_u32 . to_le( ) ) , c) ;
176
+
177
+ let p = PremulRgba8 {
178
+ r : 0xaa ,
179
+ g : 0xbb ,
180
+ b : 0xcc ,
181
+ a : 0xff ,
182
+ } ;
183
+ assert_eq ! ( PremulRgba8 :: from_u32( 0xffccbbaa_u32 . to_le( ) ) , p) ;
184
+ }
185
+
137
186
#[ test]
138
187
#[ cfg( feature = "bytemuck" ) ]
139
188
fn bytemuck_to_u32 ( ) {
@@ -153,4 +202,14 @@ mod tests {
153
202
} ;
154
203
assert_eq ! ( p. to_u32( ) , bytemuck:: cast( p) ) ;
155
204
}
205
+
206
+ #[ test]
207
+ #[ cfg( feature = "bytemuck" ) ]
208
+ fn bytemuck_from_u32 ( ) {
209
+ let c = 0x04030201_u32 . to_le ( ) ;
210
+ assert_eq ! ( Rgba8 :: from_u32( c) , bytemuck:: cast( c) ) ;
211
+
212
+ let p = 0xffccbbaa_u32 . to_le ( ) ;
213
+ assert_eq ! ( PremulRgba8 :: from_u32( p) , bytemuck:: cast( p) ) ;
214
+ }
156
215
}
0 commit comments