Skip to content

Commit 2c553f4

Browse files
committed
add predefined color consts and make as many constructors const as possible
1 parent ba55d47 commit 2c553f4

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/color/model.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,19 @@ pub struct Color {
3535
pub a: u8,
3636
}
3737

38-
impl Default for Color {
39-
fn default() -> Self {
40-
Self {
41-
r: 0,
42-
g: 0,
43-
b: 0,
44-
a: 255,
45-
}
38+
impl Color {
39+
pub const TRANSPARENT: Self = Self::new(0, 0, 0, 0);
40+
pub const BLACK: Self = Self::new(0, 0, 0, 255);
41+
pub const RED: Self = Self::new(255, 0, 0, 255);
42+
pub const GREEN: Self = Self::new(0, 255, 0, 255);
43+
pub const BLUE: Self = Self::new(0, 0, 255, 255);
44+
pub const WHITE: Self = Self::new(255, 255, 255, 255);
45+
46+
#[inline]
47+
pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
48+
Self { r, g, b, a }
4649
}
47-
}
4850

49-
impl Color {
5051
// Linear interpolation in sRGB space; use `lerp_linear` for perceptual correctness.
5152
#[must_use]
5253
#[inline]
@@ -186,6 +187,10 @@ impl Color {
186187
#[must_use]
187188
#[inline]
188189
pub fn over_srgb_fast(self, mut dst: Color) -> Color {
190+
if dst.a == 0 {
191+
dst = self;
192+
}
193+
189194
let sa = self.a as ColorFloat / 255.0;
190195
if sa <= 0.0 {
191196
return dst;
@@ -250,7 +255,7 @@ impl Color {
250255

251256
#[must_use]
252257
#[inline]
253-
pub fn with_alpha(self, a: u8) -> Self {
258+
pub const fn with_alpha(self, a: u8) -> Self {
254259
Self {
255260
r: self.r,
256261
g: self.g,
@@ -260,7 +265,7 @@ impl Color {
260265
}
261266

262267
#[inline]
263-
pub fn from_rgb(rgb: [u8; 3]) -> Self {
268+
pub const fn from_rgb(rgb: [u8; 3]) -> Self {
264269
Self {
265270
r: rgb[0],
266271
g: rgb[1],
@@ -271,12 +276,12 @@ impl Color {
271276

272277
#[must_use]
273278
#[inline]
274-
pub fn into_rgb(self) -> [u8; 3] {
279+
pub const fn into_rgb(self) -> [u8; 3] {
275280
[self.r, self.g, self.b]
276281
}
277282

278283
#[inline]
279-
pub fn from_rgba(rgba: [u8; 4]) -> Self {
284+
pub const fn from_rgba(rgba: [u8; 4]) -> Self {
280285
Self {
281286
r: rgba[0],
282287
g: rgba[1],
@@ -287,7 +292,7 @@ impl Color {
287292

288293
#[must_use]
289294
#[inline]
290-
pub fn into_rgba(self) -> [u8; 4] {
295+
pub const fn into_rgba(self) -> [u8; 4] {
291296
[self.r, self.g, self.b, self.a]
292297
}
293298

@@ -695,12 +700,18 @@ impl Color {
695700
}
696701
}
697702

703+
impl Default for Color {
704+
fn default() -> Self {
705+
Color::new(0, 0, 0, 255)
706+
}
707+
}
708+
698709
impl fmt::Display for Color {
699710
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
700711
// default to RGBA hex for lossless stringification
701712
write!(
702713
f,
703-
"#{:02x}{:02x}{:02x}{:02x}",
714+
"#{:02X}{:02X}{:02X}{:02X}",
704715
self.r, self.g, self.b, self.a
705716
)
706717
}

src/color/parse.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ pub fn parse_color(mut s: &str) -> Result<Color, ColorParseError> {
207207
// HSL: hsl(210 50% 40% / 0.7)
208208
// Plan: add a small tokenizer that accepts commas or spaces, and an optional / alpha token.
209209
// We already have HSL converters, so once the parser extracts (h, s%, l%, a?), we can call from_hsl.
210+
// Also allow for things like rgb(+255, +255, +255) (CSS allowed)
210211
let lower = s.to_ascii_lowercase();
211212
if let Some(args) = lower.strip_prefix("rgb(").and_then(|x| x.strip_suffix(')')) {
212213
return parse_css_rgb(args);

0 commit comments

Comments
 (0)