Make all Color constructors const 🎉
This commit is contained in:
parent
d9a454ac4d
commit
ce07acf6fe
3 changed files with 15 additions and 44 deletions
|
|
@ -42,22 +42,18 @@ impl Color {
|
||||||
///
|
///
|
||||||
/// In debug mode, it will panic if the values are not in the correct
|
/// In debug mode, it will panic if the values are not in the correct
|
||||||
/// range: 0.0 - 1.0
|
/// range: 0.0 - 1.0
|
||||||
pub fn new(r: f32, g: f32, b: f32, a: f32) -> Color {
|
const fn new(r: f32, g: f32, b: f32, a: f32) -> Color {
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
(0.0..=1.0).contains(&r),
|
r >= 0.0 && r <= 1.0,
|
||||||
"Red component must be on [0, 1]"
|
"Red component must be in [0, 1] range."
|
||||||
);
|
);
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
(0.0..=1.0).contains(&g),
|
g >= 0.0 && g <= 1.0,
|
||||||
"Green component must be on [0, 1]"
|
"Green component must be in [0, 1] range."
|
||||||
);
|
);
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
(0.0..=1.0).contains(&b),
|
b >= 0.0 && b <= 1.0,
|
||||||
"Blue component must be on [0, 1]"
|
"Blue component must be in [0, 1] range."
|
||||||
);
|
|
||||||
debug_assert!(
|
|
||||||
(0.0..=1.0).contains(&a),
|
|
||||||
"Alpha component must be on [0, 1]"
|
|
||||||
);
|
);
|
||||||
|
|
||||||
Color { r, g, b, a }
|
Color { r, g, b, a }
|
||||||
|
|
@ -70,7 +66,7 @@ impl Color {
|
||||||
|
|
||||||
/// Creates a [`Color`] from its RGBA components.
|
/// Creates a [`Color`] from its RGBA components.
|
||||||
pub const fn from_rgba(r: f32, g: f32, b: f32, a: f32) -> Color {
|
pub const fn from_rgba(r: f32, g: f32, b: f32, a: f32) -> Color {
|
||||||
Color { r, g, b, a }
|
Color::new(r, g, b, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a [`Color`] from its RGB8 components.
|
/// Creates a [`Color`] from its RGB8 components.
|
||||||
|
|
@ -80,12 +76,7 @@ impl Color {
|
||||||
|
|
||||||
/// Creates a [`Color`] from its RGB8 components and an alpha value.
|
/// Creates a [`Color`] from its RGB8 components and an alpha value.
|
||||||
pub const fn from_rgba8(r: u8, g: u8, b: u8, a: f32) -> Color {
|
pub const fn from_rgba8(r: u8, g: u8, b: u8, a: f32) -> Color {
|
||||||
Color {
|
Color::new(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, a)
|
||||||
r: r as f32 / 255.0,
|
|
||||||
g: g as f32 / 255.0,
|
|
||||||
b: b as f32 / 255.0,
|
|
||||||
a,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a [`Color`] from its linear RGBA components.
|
/// Creates a [`Color`] from its linear RGBA components.
|
||||||
|
|
@ -234,30 +225,10 @@ impl From<[f32; 4]> for Color {
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! color {
|
macro_rules! color {
|
||||||
($r:expr, $g:expr, $b:expr) => {
|
($r:expr, $g:expr, $b:expr) => {
|
||||||
$crate::color!($r, $g, $b, 1.0)
|
$crate::Color::from_rgb8($r, $g, $b)
|
||||||
};
|
};
|
||||||
($r:expr, $g:expr, $b:expr, $a:expr) => {{
|
($r:expr, $g:expr, $b:expr, $a:expr) => {{
|
||||||
let r = $r as f32 / 255.0;
|
$crate::Color::from_rgba8($r, $g, $b, $a)
|
||||||
let g = $g as f32 / 255.0;
|
|
||||||
let b = $b as f32 / 255.0;
|
|
||||||
|
|
||||||
#[allow(clippy::manual_range_contains)]
|
|
||||||
{
|
|
||||||
debug_assert!(
|
|
||||||
r >= 0.0 && r <= 1.0,
|
|
||||||
"R channel must be in [0, 255] range."
|
|
||||||
);
|
|
||||||
debug_assert!(
|
|
||||||
g >= 0.0 && g <= 1.0,
|
|
||||||
"G channel must be in [0, 255] range."
|
|
||||||
);
|
|
||||||
debug_assert!(
|
|
||||||
b >= 0.0 && b <= 1.0,
|
|
||||||
"B channel must be in [0, 255] range."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$crate::Color { r, g, b, a: $a }
|
|
||||||
}};
|
}};
|
||||||
($hex:expr) => {{
|
($hex:expr) => {{
|
||||||
$crate::color!($hex, 1.0)
|
$crate::color!($hex, 1.0)
|
||||||
|
|
@ -271,7 +242,7 @@ macro_rules! color {
|
||||||
let g = (hex & 0xff00) >> 8;
|
let g = (hex & 0xff00) >> 8;
|
||||||
let b = (hex & 0xff);
|
let b = (hex & 0xff);
|
||||||
|
|
||||||
$crate::color!(r, g, b, $a)
|
$crate::color!(r as u8, g as u8, b as u8, $a)
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use iced::theme;
|
||||||
use iced::widget::{
|
use iced::widget::{
|
||||||
checkbox, column, container, horizontal_space, row, slider, text,
|
checkbox, column, container, horizontal_space, row, slider, text,
|
||||||
};
|
};
|
||||||
use iced::{Center, Color, Element, Fill, Radians, Theme};
|
use iced::{color, Center, Color, Element, Fill, Radians, Theme};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
@ -34,7 +34,7 @@ impl Gradient {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
start: Color::WHITE,
|
start: Color::WHITE,
|
||||||
end: Color::new(0.0, 0.0, 1.0, 1.0),
|
end: color!(0x0000ff),
|
||||||
angle: Radians(0.0),
|
angle: Radians(0.0),
|
||||||
transparent: false,
|
transparent: false,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -414,7 +414,7 @@ impl Renderer {
|
||||||
renderer.fill_text(
|
renderer.fill_text(
|
||||||
text.clone(),
|
text.clone(),
|
||||||
Point::new(11.0, 11.0 + 25.0 * i as f32),
|
Point::new(11.0, 11.0 + 25.0 * i as f32),
|
||||||
Color::new(0.9, 0.9, 0.9, 1.0),
|
Color::from_rgba(0.9, 0.9, 0.9, 1.0),
|
||||||
Rectangle::with_size(Size::INFINITY),
|
Rectangle::with_size(Size::INFINITY),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue