From 4cef837e459314a8e278366fc043754ee20173e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Thu, 4 Dec 2025 00:04:22 +0100 Subject: [PATCH] Add short-hand notation support for `color!` macro Co-authored-by: edwloef --- core/src/color.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/core/src/color.rs b/core/src/color.rs index 6a09c2fb..902ab5f3 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -287,9 +287,18 @@ macro_rules! color { $crate::Color::from_rgb8($r, $g, $b) }; ($r:expr, $g:expr, $b:expr, $a:expr) => {{ $crate::Color::from_rgba8($r, $g, $b, $a) }}; - ($hex:expr) => {{ $crate::color!($hex, 1.0) }}; - ($hex:expr, $a:expr) => {{ - let hex = $hex as u32; + ($hex:literal) => {{ $crate::color!($hex, 1.0) }}; + ($hex:literal, $a:expr) => {{ + let mut hex = $hex as u32; + + // Shorthand notation: 0x123 + if stringify!($hex).len() == 5 { + let r = hex & 0xF00; + let g = hex & 0xF0; + let b = hex & 0xF; + + hex = (r << 12) | (r << 8) | (g << 8) | (g << 4) | (b << 4) | b; + } debug_assert!(hex <= 0xffffff, "color! value must not exceed 0xffffff"); @@ -324,4 +333,11 @@ mod tests { assert!("invalid".parse::().is_err()); } + + const SHORTHAND: Color = color!(0x123); + + #[test] + fn shorthand_notation() { + assert_eq!(SHORTHAND, Color::from_rgb8(0x11, 0x22, 0x33)); + } }