diff options
author | 2022-01-31 22:06:42 +0200 | |
---|---|---|
committer | 2022-09-23 20:09:03 +0200 | |
commit | 82cf8d2d12187417f339d471e2b7a57fd97f6251 (patch) | |
tree | 5623da5a3d2fe001e0a705d88c03d3f27f3f552b /core | |
parent | 489a85518b4b533d92f59b468634cec5194ff567 (diff) | |
download | iced-82cf8d2d12187417f339d471e2b7a57fd97f6251.tar.gz iced-82cf8d2d12187417f339d471e2b7a57fd97f6251.tar.bz2 iced-82cf8d2d12187417f339d471e2b7a57fd97f6251.zip |
Macro for easy colour generation
Diffstat (limited to '')
-rw-r--r-- | core/src/color.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/core/src/color.rs b/core/src/color.rs index c66ee97c..73104fc9 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -134,6 +134,39 @@ impl From<[f32; 4]> for Color { } } +/// # Examples +/// +/// ``` +/// # use iced_core::{Color, color}; +/// assert_eq!(color!(0, 0, 0), Color::from_rgb(0., 0., 0.)); +/// assert_eq!(color!(0, 0, 0, 0.), Color::from_rgba(0., 0., 0., 0.)); +/// assert_eq!(color!(0xffffff), Color::from_rgb(1., 1., 1.)); +/// assert_eq!(color!(0xffffff, 0.), Color::from_rgba(1., 1., 1., 0.)); +/// ``` +#[macro_export] +macro_rules! color { + ($r:expr, $g:expr, $b:expr) => { + Color::from_rgb8($r, $g, $b) + }; + ($r:expr, $g:expr, $b:expr, $a:expr) => { + Color::from_rgba8($r, $g, $b, $a) + }; + ($hex:expr) => {{ + let hex = $hex as u32; + let r = (hex & 0xff0000) >> 16; + let g = (hex & 0xff00) >> 8; + let b = (hex & 0xff); + Color::from_rgb8(r as u8, g as u8, b as u8) + }}; + ($hex:expr, $a:expr) => {{ + let hex = $hex as u32; + let r = (hex & 0xff0000) >> 16; + let g = (hex & 0xff00) >> 8; + let b = (hex & 0xff); + Color::from_rgba8(r as u8, g as u8, b as u8, $a) + }}; +} + #[cfg(feature = "palette")] /// Converts from palette's `Srgba` type to a [`Color`]. impl From<Srgba> for Color { |