summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2022-09-23 20:26:53 +0200
committerLibravatar GitHub <noreply@github.com>2022-09-23 20:26:53 +0200
commit3c9af1eb31d8f5e4a6cfcc10aa41e48868f3d6eb (patch)
tree60d2698687b97503d96ecb147bc0ae6dc17a3d4e /core
parentbfd24c27db3d37d4fb772d1ea564c96936663c1d (diff)
parentbe5964db83f5e33cd80b93274d9f26117bbbe128 (diff)
downloadiced-3c9af1eb31d8f5e4a6cfcc10aa41e48868f3d6eb.tar.gz
iced-3c9af1eb31d8f5e4a6cfcc10aa41e48868f3d6eb.tar.bz2
iced-3c9af1eb31d8f5e4a6cfcc10aa41e48868f3d6eb.zip
Merge pull request #1227 from LordRatte/feature-colour-macro
`color!` macro
Diffstat (limited to '')
-rw-r--r--core/src/color.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/core/src/color.rs b/core/src/color.rs
index c66ee97c..212c1214 100644
--- a/core/src/color.rs
+++ b/core/src/color.rs
@@ -134,6 +134,41 @@ impl From<[f32; 4]> for Color {
}
}
+/// Creates a [`Color`] with shorter and cleaner syntax.
+///
+/// # 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 {