Drop short-hand notation support for color! macro

We'd need to use `stringify!` and `str::len` to properly
support the short-hand notation; however, we want the
macro to work in `const` contexts.
This commit is contained in:
Héctor Ramón Jiménez 2024-09-20 17:27:43 +02:00
parent b78b8bc8e3
commit 2a547ae372
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -229,13 +229,12 @@ impl From<[f32; 4]> for Color {
/// assert_eq!(color!(0, 0, 0, 0.0), Color::TRANSPARENT); /// assert_eq!(color!(0, 0, 0, 0.0), Color::TRANSPARENT);
/// assert_eq!(color!(0xffffff), Color::from_rgb(1.0, 1.0, 1.0)); /// assert_eq!(color!(0xffffff), Color::from_rgb(1.0, 1.0, 1.0));
/// assert_eq!(color!(0xffffff, 0.), Color::from_rgba(1.0, 1.0, 1.0, 0.0)); /// assert_eq!(color!(0xffffff, 0.), Color::from_rgba(1.0, 1.0, 1.0, 0.0));
/// assert_eq!(color!(0x123), Color::from_rgba8(0x11, 0x22, 0x33, 1.0)); /// assert_eq!(color!(0x0000ff), Color::from_rgba(0.0, 0.0, 1.0, 1.0));
/// assert_eq!(color!(0x123), color!(0x112233));
/// ``` /// ```
#[macro_export] #[macro_export]
macro_rules! color { macro_rules! color {
($r:expr, $g:expr, $b:expr) => { ($r:expr, $g:expr, $b:expr) => {
color!($r, $g, $b, 1.0) $crate::color!($r, $g, $b, 1.0)
}; };
($r:expr, $g:expr, $b:expr, $a:expr) => {{ ($r:expr, $g:expr, $b:expr, $a:expr) => {{
let r = $r as f32 / 255.0; let r = $r as f32 / 255.0;
@ -261,29 +260,18 @@ macro_rules! color {
$crate::Color { r, g, b, a: $a } $crate::Color { r, g, b, a: $a }
}}; }};
($hex:expr) => {{ ($hex:expr) => {{
color!($hex, 1.0) $crate::color!($hex, 1.0)
}}; }};
($hex:expr, $a:expr) => {{ ($hex:expr, $a:expr) => {{
let hex = $hex as u32; let hex = $hex as u32;
if hex <= 0xfff { debug_assert!(hex <= 0xffffff, "color! value must not exceed 0xffffff");
let r = (hex & 0xf00) >> 8;
let g = (hex & 0x0f0) >> 4;
let b = (hex & 0x00f);
color!((r << 4 | r), (g << 4 | g), (b << 4 | b), $a) let r = (hex & 0xff0000) >> 16;
} else { let g = (hex & 0xff00) >> 8;
debug_assert!( let b = (hex & 0xff);
hex <= 0xffffff,
"color! value must not exceed 0xffffff"
);
let r = (hex & 0xff0000) >> 16; $crate::color!(r, g, b, $a)
let g = (hex & 0xff00) >> 8;
let b = (hex & 0xff);
color!(r, g, b, $a)
}
}}; }};
} }