Skip to content

Commit 96f782b

Browse files
committed
deprecate the color_u8! macro
1 parent 2e48cf9 commit 96f782b

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/color.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ pub struct Color {
1616
pub a: f32,
1717
}
1818

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.
2223
#[macro_export]
24+
#[deprecated(note = "use `Color::from_rgba` instead")]
2325
macro_rules! color_u8 {
2426
($r:expr, $g:expr, $b:expr, $a:expr) => {
2527
Color::new(
@@ -33,14 +35,17 @@ macro_rules! color_u8 {
3335

3436
#[test]
3537
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+
);
3742
assert_eq!(
3843
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)
4045
);
4146
assert_eq!(
4247
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)
4449
);
4550
}
4651

@@ -101,9 +106,8 @@ impl Color {
101106
}
102107

103108
/// 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 {
107111
Color::new(
108112
r as f32 / 255.,
109113
g as f32 / 255.,
@@ -112,6 +116,11 @@ impl Color {
112116
)
113117
}
114118

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+
115124
/// Build a color from a hexadecimal u32
116125
///
117126
/// # Example

0 commit comments

Comments
 (0)