1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use crate::Error;
/// A packed RGB color stored as `0xRRGGBB`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Rgb(u32);
impl Rgb {
/// Creates an RGB color from a packed `0xRRGGBB` value.
#[must_use]
pub const fn from_hex(value: u32) -> Self {
Self(value & 0x00FF_FFFF)
}
/// Parses a `#RRGGBB` hex color.
///
/// # Errors
///
/// Returns [`Error::InvalidHexColor`] if `input` is not exactly a
/// six-digit RGB hex color with a leading `#`.
pub fn parse_hex(input: &str) -> Result<Self, Error> {
let Some(hex) = input.strip_prefix('#') else {
return Err(Error::InvalidHexColor {
input: input.to_owned(),
});
};
if hex.len() != 6 || !hex.bytes().all(|byte| byte.is_ascii_hexdigit()) {
return Err(Error::InvalidHexColor {
input: input.to_owned(),
});
}
u32::from_str_radix(hex, 16)
.map(Self::from_hex)
.map_err(|_| Error::InvalidHexColor {
input: input.to_owned(),
})
}
/// Returns the red channel.
#[must_use]
pub const fn r(self) -> u8 {
self.0.to_be_bytes()[1]
}
/// Returns the green channel.
#[must_use]
pub const fn g(self) -> u8 {
self.0.to_be_bytes()[2]
}
/// Returns the blue channel.
#[must_use]
pub const fn b(self) -> u8 {
self.0.to_be_bytes()[3]
}
/// Returns the packed `0xRRGGBB` value.
#[must_use]
pub const fn to_u32(self) -> u32 {
self.0
}
/// Formats the color as `#RRGGBB`.
#[must_use]
pub fn to_hex_string(self) -> String {
format!("#{:06X}", self.0)
}
/// Returns this RGB color with an 8-bit alpha channel.
#[must_use]
pub const fn with_alpha_u8(self, alpha: u8) -> Rgba {
Rgba::from_rgb_alpha(self, alpha)
}
/// Returns this RGB color with a floating-point alpha channel.
///
/// `alpha` must be finite and in the inclusive range `0.0..=1.0`.
///
/// # Errors
///
/// Returns [`Error::InvalidAlpha`] when `alpha` is outside the valid range
/// or is not finite.
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
pub fn with_alpha(self, alpha: f32) -> Result<Rgba, Error> {
if !alpha.is_finite() || !(0.0..=1.0).contains(&alpha) {
return Err(Error::InvalidAlpha { alpha });
}
Ok(self.with_alpha_u8((alpha * 255.0).round() as u8))
}
}
/// A packed RGBA color stored as `0xRRGGBBAA`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Rgba(u32);
impl Rgba {
/// Creates an RGBA color from a packed `0xRRGGBBAA` value.
#[must_use]
pub const fn from_hex(value: u32) -> Self {
Self(value)
}
/// Creates an RGBA color from RGB and 8-bit alpha components.
#[must_use]
pub const fn from_rgb_alpha(rgb: Rgb, alpha: u8) -> Self {
Self((rgb.to_u32() << 8) | alpha as u32)
}
/// Parses a `#RRGGBBAA` hex color.
///
/// # Errors
///
/// Returns [`Error::InvalidHexColor`] if `input` is not exactly an
/// eight-digit RGBA hex color with a leading `#`.
pub fn parse_hex(input: &str) -> Result<Self, Error> {
let Some(hex) = input.strip_prefix('#') else {
return Err(Error::InvalidHexColor {
input: input.to_owned(),
});
};
if hex.len() != 8 || !hex.bytes().all(|byte| byte.is_ascii_hexdigit()) {
return Err(Error::InvalidHexColor {
input: input.to_owned(),
});
}
u32::from_str_radix(hex, 16)
.map(Self::from_hex)
.map_err(|_| Error::InvalidHexColor {
input: input.to_owned(),
})
}
/// Returns the red channel.
#[must_use]
pub const fn r(self) -> u8 {
self.0.to_be_bytes()[0]
}
/// Returns the green channel.
#[must_use]
pub const fn g(self) -> u8 {
self.0.to_be_bytes()[1]
}
/// Returns the blue channel.
#[must_use]
pub const fn b(self) -> u8 {
self.0.to_be_bytes()[2]
}
/// Returns the alpha channel.
#[must_use]
pub const fn a(self) -> u8 {
self.0.to_be_bytes()[3]
}
/// Returns the packed `0xRRGGBBAA` value.
#[must_use]
pub const fn to_u32(self) -> u32 {
self.0
}
/// Formats the color as `#RRGGBBAA`.
#[must_use]
pub fn to_hex_string(self) -> String {
format!("#{:08X}", self.0)
}
}