Merge pull request #1227 from LordRatte/feature-colour-macro

`color!` macro
This commit is contained in:
Héctor Ramón 2022-09-23 20:26:53 +02:00 committed by GitHub
commit 3c9af1eb31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 4 deletions

View file

@ -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 {

View file

@ -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;

View file

@ -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")]