Skip to content

Commit da1726b

Browse files
authored
Merge pull request #2749 from rhysd/const-from-rgb8
Make `Color::from_rgb8` and `Color::from_rgba8` const
2 parents 3dc6014 + 81f05f0 commit da1726b

File tree

6 files changed

+28
-93
lines changed

6 files changed

+28
-93
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
matrix:
1010
os: [ubuntu-latest, windows-latest, macOS-latest]
11-
rust: [stable, beta, "1.81"]
11+
rust: [stable, beta, "1.82"]
1212
steps:
1313
- uses: hecrj/setup-rust-action@v2
1414
with:

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ repository = "https://github.com/iced-rs/iced"
128128
homepage = "https://iced.rs"
129129
categories = ["gui"]
130130
keywords = ["gui", "ui", "graphics", "interface", "widgets"]
131-
rust-version = "1.81"
131+
rust-version = "1.82"
132132

133133
[workspace.dependencies]
134134
iced = { version = "0.14.0-dev", path = "." }

core/src/color.rs

+14-43
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,18 @@ impl Color {
4242
///
4343
/// In debug mode, it will panic if the values are not in the correct
4444
/// range: 0.0 - 1.0
45-
pub fn new(r: f32, g: f32, b: f32, a: f32) -> Color {
45+
const fn new(r: f32, g: f32, b: f32, a: f32) -> Color {
4646
debug_assert!(
47-
(0.0..=1.0).contains(&r),
48-
"Red component must be on [0, 1]"
47+
r >= 0.0 && r <= 1.0,
48+
"Red component must be in [0, 1] range."
4949
);
5050
debug_assert!(
51-
(0.0..=1.0).contains(&g),
52-
"Green component must be on [0, 1]"
51+
g >= 0.0 && g <= 1.0,
52+
"Green component must be in [0, 1] range."
5353
);
5454
debug_assert!(
55-
(0.0..=1.0).contains(&b),
56-
"Blue component must be on [0, 1]"
57-
);
58-
debug_assert!(
59-
(0.0..=1.0).contains(&a),
60-
"Alpha component must be on [0, 1]"
55+
b >= 0.0 && b <= 1.0,
56+
"Blue component must be in [0, 1] range."
6157
);
6258

6359
Color { r, g, b, a }
@@ -70,22 +66,17 @@ impl Color {
7066

7167
/// Creates a [`Color`] from its RGBA components.
7268
pub const fn from_rgba(r: f32, g: f32, b: f32, a: f32) -> Color {
73-
Color { r, g, b, a }
69+
Color::new(r, g, b, a)
7470
}
7571

7672
/// Creates a [`Color`] from its RGB8 components.
77-
pub fn from_rgb8(r: u8, g: u8, b: u8) -> Color {
73+
pub const fn from_rgb8(r: u8, g: u8, b: u8) -> Color {
7874
Color::from_rgba8(r, g, b, 1.0)
7975
}
8076

8177
/// Creates a [`Color`] from its RGB8 components and an alpha value.
82-
pub fn from_rgba8(r: u8, g: u8, b: u8, a: f32) -> Color {
83-
Color {
84-
r: f32::from(r) / 255.0,
85-
g: f32::from(g) / 255.0,
86-
b: f32::from(b) / 255.0,
87-
a,
88-
}
78+
pub const fn from_rgba8(r: u8, g: u8, b: u8, a: f32) -> Color {
79+
Color::new(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, a)
8980
}
9081

9182
/// Creates a [`Color`] from its linear RGBA components.
@@ -234,30 +225,10 @@ impl From<[f32; 4]> for Color {
234225
#[macro_export]
235226
macro_rules! color {
236227
($r:expr, $g:expr, $b:expr) => {
237-
$crate::color!($r, $g, $b, 1.0)
228+
$crate::Color::from_rgb8($r, $g, $b)
238229
};
239230
($r:expr, $g:expr, $b:expr, $a:expr) => {{
240-
let r = $r as f32 / 255.0;
241-
let g = $g as f32 / 255.0;
242-
let b = $b as f32 / 255.0;
243-
244-
#[allow(clippy::manual_range_contains)]
245-
{
246-
debug_assert!(
247-
r >= 0.0 && r <= 1.0,
248-
"R channel must be in [0, 255] range."
249-
);
250-
debug_assert!(
251-
g >= 0.0 && g <= 1.0,
252-
"G channel must be in [0, 255] range."
253-
);
254-
debug_assert!(
255-
b >= 0.0 && b <= 1.0,
256-
"B channel must be in [0, 255] range."
257-
);
258-
}
259-
260-
$crate::Color { r, g, b, a: $a }
231+
$crate::Color::from_rgba8($r, $g, $b, $a)
261232
}};
262233
($hex:expr) => {{
263234
$crate::color!($hex, 1.0)
@@ -271,7 +242,7 @@ macro_rules! color {
271242
let g = (hex & 0xff00) >> 8;
272243
let b = (hex & 0xff);
273244

274-
$crate::color!(r, g, b, $a)
245+
$crate::color!(r as u8, g as u8, b as u8, $a)
275246
}};
276247
}
277248

core/src/theme/palette.rs

+9-45
Original file line numberDiff line numberDiff line change
@@ -29,56 +29,20 @@ impl Palette {
2929
pub const LIGHT: Self = Self {
3030
background: Color::WHITE,
3131
text: Color::BLACK,
32-
primary: Color::from_rgb(
33-
0x5E as f32 / 255.0,
34-
0x7C as f32 / 255.0,
35-
0xE2 as f32 / 255.0,
36-
),
37-
success: Color::from_rgb(
38-
0x12 as f32 / 255.0,
39-
0x66 as f32 / 255.0,
40-
0x4F as f32 / 255.0,
41-
),
42-
warning: Color::from_rgb(
43-
0xFF as f32 / 255.0,
44-
0xC1 as f32 / 255.0,
45-
0x4E as f32 / 255.0,
46-
),
47-
danger: Color::from_rgb(
48-
0xC3 as f32 / 255.0,
49-
0x42 as f32 / 255.0,
50-
0x3F as f32 / 255.0,
51-
),
32+
primary: color!(0x5e7ce2),
33+
success: color!(0x12664f),
34+
warning: color!(0xffc14e),
35+
danger: color!(0xc3423f),
5236
};
5337

5438
/// The built-in dark variant of a [`Palette`].
5539
pub const DARK: Self = Self {
56-
background: Color::from_rgb(
57-
0x20 as f32 / 255.0,
58-
0x22 as f32 / 255.0,
59-
0x25 as f32 / 255.0,
60-
),
40+
background: color!(0x202225),
6141
text: Color::from_rgb(0.90, 0.90, 0.90),
62-
primary: Color::from_rgb(
63-
0x5E as f32 / 255.0,
64-
0x7C as f32 / 255.0,
65-
0xE2 as f32 / 255.0,
66-
),
67-
success: Color::from_rgb(
68-
0x12 as f32 / 255.0,
69-
0x66 as f32 / 255.0,
70-
0x4F as f32 / 255.0,
71-
),
72-
warning: Color::from_rgb(
73-
0xFF as f32 / 255.0,
74-
0xC1 as f32 / 255.0,
75-
0x4E as f32 / 255.0,
76-
),
77-
danger: Color::from_rgb(
78-
0xC3 as f32 / 255.0,
79-
0x42 as f32 / 255.0,
80-
0x3F as f32 / 255.0,
81-
),
42+
primary: color!(0x5e7ce2),
43+
success: color!(0x12664f),
44+
warning: color!(0xffc14e),
45+
danger: color!(0xc3423f),
8246
};
8347

8448
/// The built-in [Dracula] variant of a [`Palette`].

examples/gradient/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use iced::theme;
33
use iced::widget::{
44
checkbox, column, container, horizontal_space, row, slider, text,
55
};
6-
use iced::{Center, Color, Element, Fill, Radians, Theme};
6+
use iced::{color, Center, Color, Element, Fill, Radians, Theme};
77

88
pub fn main() -> iced::Result {
99
tracing_subscriber::fmt::init();
@@ -34,7 +34,7 @@ impl Gradient {
3434
fn new() -> Self {
3535
Self {
3636
start: Color::WHITE,
37-
end: Color::new(0.0, 0.0, 1.0, 1.0),
37+
end: color!(0x0000ff),
3838
angle: Radians(0.0),
3939
transparent: false,
4040
}

wgpu/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl Renderer {
423423
renderer.fill_text(
424424
text.clone(),
425425
Point::new(11.0, 11.0 + 25.0 * i as f32),
426-
Color::new(0.9, 0.9, 0.9, 1.0),
426+
Color::from_rgba(0.9, 0.9, 0.9, 1.0),
427427
Rectangle::with_size(Size::INFINITY),
428428
);
429429

0 commit comments

Comments
 (0)