Add short-hand notation support for color! macro
Co-authored-by: edwloef <edwin.frank.loeffler@gmail.com>
This commit is contained in:
parent
02c551259a
commit
4cef837e45
1 changed files with 19 additions and 3 deletions
|
|
@ -287,9 +287,18 @@ macro_rules! color {
|
||||||
$crate::Color::from_rgb8($r, $g, $b)
|
$crate::Color::from_rgb8($r, $g, $b)
|
||||||
};
|
};
|
||||||
($r:expr, $g:expr, $b:expr, $a:expr) => {{ $crate::Color::from_rgba8($r, $g, $b, $a) }};
|
($r:expr, $g:expr, $b:expr, $a:expr) => {{ $crate::Color::from_rgba8($r, $g, $b, $a) }};
|
||||||
($hex:expr) => {{ $crate::color!($hex, 1.0) }};
|
($hex:literal) => {{ $crate::color!($hex, 1.0) }};
|
||||||
($hex:expr, $a:expr) => {{
|
($hex:literal, $a:expr) => {{
|
||||||
let hex = $hex as u32;
|
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");
|
debug_assert!(hex <= 0xffffff, "color! value must not exceed 0xffffff");
|
||||||
|
|
||||||
|
|
@ -324,4 +333,11 @@ mod tests {
|
||||||
|
|
||||||
assert!("invalid".parse::<Color>().is_err());
|
assert!("invalid".parse::<Color>().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const SHORTHAND: Color = color!(0x123);
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn shorthand_notation() {
|
||||||
|
assert_eq!(SHORTHAND, Color::from_rgb8(0x11, 0x22, 0x33));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue