Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate the color_u8! macro and make methods const #818

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
pub a: f32,
}

/// Build a color from 4 components of 0..255 values
/// This is a temporary solution and going to be replaced with const fn,
/// waiting for [this issue](https:/rust-lang/rust/issues/57241) to be resolved.
/// Build a color from 4 components of 0..255 values. Use `Color::from_rgba` directly instead.
///
/// This was a workaround because floating point arithmetic in const was not stable yet.
/// It should not be used anymore.
#[macro_export]
#[deprecated(note = "use `Color::from_rgba` instead")]
macro_rules! color_u8 {
($r:expr, $g:expr, $b:expr, $a:expr) => {
Color::new(
Expand All @@ -33,14 +35,17 @@

#[test]
fn color_from_bytes() {
assert_eq!(Color::new(1.0, 0.0, 0.0, 1.0), color_u8!(255, 0, 0, 255));
assert_eq!(
Color::new(1.0, 0.0, 0.0, 1.0),
Color::from_rgba(255, 0, 0, 255)
);
assert_eq!(
Color::new(1.0, 0.5, 0.0, 1.0),
color_u8!(255, 127.5, 0, 255)
Color::from_rgba_f32(255.0, 127.5, 0.0, 255.0)
);
assert_eq!(
Color::new(0.0, 1.0, 0.5, 1.0),
color_u8!(0, 255, 127.5, 255)
Color::from_rgba_f32(0.0, 255.0, 127.5, 255.0)
);
}

Expand Down Expand Up @@ -101,17 +106,21 @@
}

/// Build a color from 4 components between 0 and 255.
/// Unfortunately it can't be const fn due to [this issue](https:/rust-lang/rust/issues/57241).
/// When const version is needed "color_u8" macro may be a workaround.
pub fn from_rgba(r: u8, g: u8, b: u8, a: u8) -> Color {
/// If the values have to be floats, use `Color::from_rgba_f32` instead.
pub const fn from_rgba(r: u8, g: u8, b: u8, a: u8) -> Color {
Color::new(
r as f32 / 255.,

Check failure on line 112 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

floating point arithmetic is not allowed in constant functions

Check failure on line 112 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

floating point arithmetic is not allowed in constant functions

Check failure on line 112 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 112 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 112 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

floating point arithmetic is not allowed in constant functions
g as f32 / 255.,

Check failure on line 113 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

floating point arithmetic is not allowed in constant functions

Check failure on line 113 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

floating point arithmetic is not allowed in constant functions

Check failure on line 113 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 113 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 113 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

floating point arithmetic is not allowed in constant functions
b as f32 / 255.,

Check failure on line 114 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

floating point arithmetic is not allowed in constant functions

Check failure on line 114 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

floating point arithmetic is not allowed in constant functions

Check failure on line 114 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 114 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 114 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

floating point arithmetic is not allowed in constant functions
a as f32 / 255.,

Check failure on line 115 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

floating point arithmetic is not allowed in constant functions

Check failure on line 115 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

floating point arithmetic is not allowed in constant functions

Check failure on line 115 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 115 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 115 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

floating point arithmetic is not allowed in constant functions
)
}

/// Build a color from 4 `f32` components between 0.0 and 255.0.
pub const fn from_rgba_f32(r: f32, g: f32, b: f32, a: f32) -> Color {
Color::new(r / 255., g / 255., b / 255., a / 255.)

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

floating point arithmetic is not allowed in constant functions

Check failure on line 121 in src/color.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

floating point arithmetic is not allowed in constant functions
}

/// Build a color from a hexadecimal u32
///
/// # Example
Expand Down
Loading