@@ -16,10 +16,12 @@ pub struct Color {
16
16
pub a : f32 ,
17
17
}
18
18
19
- /// Build a color from 4 components of 0..255 values
20
- /// This is a temporary solution and going to be replaced with const fn,
21
- /// waiting for [this issue](https://github.com/rust-lang/rust/issues/57241) to be resolved.
19
+ /// Build a color from 4 components of 0..255 values. Use `Color::from_rgba` directly instead.
20
+ ///
21
+ /// This was a workaround because floating point arithmetic in const was not stable yet.
22
+ /// It should not be used anymore.
22
23
#[ macro_export]
24
+ #[ deprecated( note = "use `Color::from_rgba` instead" ) ]
23
25
macro_rules! color_u8 {
24
26
( $r: expr, $g: expr, $b: expr, $a: expr) => {
25
27
Color :: new(
@@ -33,14 +35,17 @@ macro_rules! color_u8 {
33
35
34
36
#[ test]
35
37
fn color_from_bytes ( ) {
36
- assert_eq ! ( Color :: new( 1.0 , 0.0 , 0.0 , 1.0 ) , color_u8!( 255 , 0 , 0 , 255 ) ) ;
38
+ assert_eq ! (
39
+ Color :: new( 1.0 , 0.0 , 0.0 , 1.0 ) ,
40
+ Color :: from_rgba( 255 , 0 , 0 , 255 )
41
+ ) ;
37
42
assert_eq ! (
38
43
Color :: new( 1.0 , 0.5 , 0.0 , 1.0 ) ,
39
- color_u8! ( 255 , 127.5 , 0 , 255 )
44
+ Color :: from_rgba_f32 ( 255.0 , 127.5 , 0.0 , 255.0 )
40
45
) ;
41
46
assert_eq ! (
42
47
Color :: new( 0.0 , 1.0 , 0.5 , 1.0 ) ,
43
- color_u8! ( 0 , 255 , 127.5 , 255 )
48
+ Color :: from_rgba_f32 ( 0.0 , 255.0 , 127.5 , 255.0 )
44
49
) ;
45
50
}
46
51
@@ -101,9 +106,8 @@ impl Color {
101
106
}
102
107
103
108
/// Build a color from 4 components between 0 and 255.
104
- /// Unfortunately it can't be const fn due to [this issue](https://github.com/rust-lang/rust/issues/57241).
105
- /// When const version is needed "color_u8" macro may be a workaround.
106
- pub fn from_rgba ( r : u8 , g : u8 , b : u8 , a : u8 ) -> Color {
109
+ /// If the values have to be floats, use `Color::from_rgba_f32` instead.
110
+ pub const fn from_rgba ( r : u8 , g : u8 , b : u8 , a : u8 ) -> Color {
107
111
Color :: new (
108
112
r as f32 / 255. ,
109
113
g as f32 / 255. ,
@@ -112,6 +116,11 @@ impl Color {
112
116
)
113
117
}
114
118
119
+ /// Build a color from 4 `f32` components between 0.0 and 255.0.
120
+ pub const fn from_rgba_f32 ( r : f32 , g : f32 , b : f32 , a : f32 ) -> Color {
121
+ Color :: new ( r / 255. , g / 255. , b / 255. , a / 255. )
122
+ }
123
+
115
124
/// Build a color from a hexadecimal u32
116
125
///
117
126
/// # Example
0 commit comments