From 82cf8d2d12187417f339d471e2b7a57fd97f6251 Mon Sep 17 00:00:00 2001 From: Joshua Date: Mon, 31 Jan 2022 22:06:42 +0200 Subject: [PATCH 1/3] Macro for easy colour generation --- core/src/color.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/core/src/color.rs b/core/src/color.rs index c66ee97cb0..73104fc94f 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 for Color { From b8401c61a1a9966adf83e520cb2bb2758366459a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 23 Sep 2022 20:10:16 +0200 Subject: [PATCH 2/3] Export `color!` macro in `iced` and `iced_native` --- native/src/lib.rs | 4 ++-- src/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/native/src/lib.rs b/native/src/lib.rs index 131739011c..02269265f9 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -80,8 +80,8 @@ mod debug; pub use iced_core::alignment; pub use iced_core::time; pub use iced_core::{ - Alignment, Background, Color, ContentFit, Font, Length, Padding, Point, - Rectangle, Size, Vector, + color, Alignment, Background, Color, ContentFit, Font, Length, Padding, + Point, Rectangle, Size, Vector, }; pub use iced_futures::{executor, futures}; pub use iced_style::application; diff --git a/src/lib.rs b/src/lib.rs index 4276f86aa9..8209952f2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -211,8 +211,8 @@ pub use theme::Theme; pub use runtime::alignment; pub use runtime::futures; pub use runtime::{ - Alignment, Background, Color, Command, ContentFit, Font, Length, Padding, - Point, Rectangle, Size, Vector, + color, Alignment, Background, Color, Command, ContentFit, Font, Length, + Padding, Point, Rectangle, Size, Vector, }; #[cfg(feature = "system")] From be5964db83f5e33cd80b93274d9f26117bbbe128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 23 Sep 2022 20:11:43 +0200 Subject: [PATCH 3/3] Write documentation for `color!` macro --- core/src/color.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/src/color.rs b/core/src/color.rs index 73104fc94f..212c12148a 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -134,6 +134,8 @@ impl From<[f32; 4]> for Color { } } +/// Creates a [`Color`] with shorter and cleaner syntax. +/// /// # Examples /// /// ```