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

Closed
wants to merge 1 commit into from
Closed
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
@@ -16,10 +16,12 @@ pub struct Color {
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://github.com/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(
@@ -33,14 +35,17 @@ macro_rules! color_u8 {

#[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)
);
}

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

/// Build a color from 4 components between 0 and 255.
/// Unfortunately it can't be const fn due to [this issue](https://github.com/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.,
g as f32 / 255.,
@@ -112,6 +116,11 @@ impl Color {
)
}

/// 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.)
}

/// Build a color from a hexadecimal u32
///
/// # Example

Unchanged files with check annotations Beta

last_frame_time: f64,
frame_time: f64,
#[cfg(one_screenshot)]

Check warning on line 217 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 217 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 217 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 217 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 217 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`
counter: usize,
camera_stack: Vec<camera::CameraState>,
last_frame_time: miniquad::date::now(),
frame_time: 1. / 60.,
#[cfg(one_screenshot)]

Check warning on line 359 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 359 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 359 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 359 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 359 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`
counter: 0,
unwind: false,
recovery_future: None,
get_quad_context().commit_frame();
#[cfg(one_screenshot)]

Check warning on line 409 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 409 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 409 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 409 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 409 in src/lib.rs

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

unexpected `cfg` condition name: `one_screenshot`
{
get_context().counter += 1;
if get_context().counter == 3 {
pub use crate::logging::*;
pub use crate::color_u8;

Check warning on line 27 in src/prelude.rs

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

use of deprecated macro `color::color_u8`: use `Color::from_rgba` instead

Check warning on line 27 in src/prelude.rs

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

use of deprecated macro `color::color_u8`: use `Color::from_rgba` instead

Check warning on line 27 in src/prelude.rs

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

use of deprecated macro `color::color_u8`: use `Color::from_rgba` instead

Check warning on line 27 in src/prelude.rs

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

use of deprecated macro `color::color_u8`: use `Color::from_rgba` instead

Check warning on line 27 in src/prelude.rs

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

use of deprecated macro `color::color_u8`: use `Color::from_rgba` instead
pub use image::ImageFormat;