Skip to content

Commit 95af223

Browse files
committed
Do not pass Option for HSHL, because we never have to serialize with none.
1 parent f962676 commit 95af223

File tree

2 files changed

+31
-53
lines changed

2 files changed

+31
-53
lines changed

src/color.rs

+25-40
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,17 @@ impl ToCss for RGBA {
205205
#[derive(Clone, Copy, PartialEq, Debug)]
206206
pub struct Hsl {
207207
/// The hue component.
208-
pub hue: Option<f32>,
208+
pub hue: f32,
209209
/// The saturation component.
210-
pub saturation: Option<f32>,
210+
pub saturation: f32,
211211
/// The lightness component.
212-
pub lightness: Option<f32>,
212+
pub lightness: f32,
213213
/// The alpha component.
214-
pub alpha: Option<f32>,
214+
pub alpha: f32,
215215
}
216216

217217
impl Hsl {
218-
pub fn new(
219-
hue: Option<f32>,
220-
saturation: Option<f32>,
221-
lightness: Option<f32>,
222-
alpha: Option<f32>,
223-
) -> Self {
218+
pub fn new(hue: f32, saturation: f32, lightness: f32, alpha: f32) -> Self {
224219
Self {
225220
hue,
226221
saturation,
@@ -236,13 +231,9 @@ impl ToCss for Hsl {
236231
W: fmt::Write,
237232
{
238233
// HSL serializes to RGB, so we have to convert it.
239-
let (red, green, blue) = hsl_to_rgb(
240-
self.hue.unwrap_or(0.0) / 360.0,
241-
self.saturation.unwrap_or(0.0),
242-
self.lightness.unwrap_or(0.0),
243-
);
234+
let (red, green, blue) = hsl_to_rgb(self.hue / 360.0, self.saturation, self.lightness);
244235

245-
RGBA::from_floats(red, green, blue, self.alpha.unwrap_or(0.0)).to_css(dest)
236+
RGBA::from_floats(red, green, blue, self.alpha).to_css(dest)
246237
}
247238
}
248239

@@ -839,12 +830,7 @@ pub trait FromParsedColor {
839830
fn from_rgba(red: u8, green: u8, blue: u8, alpha: f32) -> Self;
840831

841832
/// Construct a new color from hue, saturation, lightness and alpha components.
842-
fn from_hsl(
843-
hue: Option<f32>,
844-
saturation: Option<f32>,
845-
lightness: Option<f32>,
846-
alpha: Option<f32>,
847-
) -> Self;
833+
fn from_hsl(hue: f32, saturation: f32, lightness: f32, alpha: f32) -> Self;
848834

849835
/// Construct a new color from hue, blackness, whiteness and alpha components.
850836
fn from_hwb(
@@ -929,12 +915,7 @@ impl FromParsedColor for Color {
929915
Color::Rgba(RGBA::new(red, green, blue, alpha))
930916
}
931917

932-
fn from_hsl(
933-
hue: Option<f32>,
934-
saturation: Option<f32>,
935-
lightness: Option<f32>,
936-
alpha: Option<f32>,
937-
) -> Self {
918+
fn from_hsl(hue: f32, saturation: f32, lightness: f32, alpha: f32) -> Self {
938919
Color::Hsl(Hsl::new(hue, saturation, lightness, alpha))
939920
}
940921

@@ -1371,29 +1352,33 @@ where
13711352
// the legacy syntax.
13721353
let is_legacy_syntax = maybe_hue.is_some() && arguments.try_parse(|p| p.expect_comma()).is_ok();
13731354

1374-
let saturation: Option<f32>;
1375-
let lightness: Option<f32>;
1355+
let saturation: f32;
1356+
let lightness: f32;
13761357

13771358
let alpha = if is_legacy_syntax {
1378-
saturation = Some(color_parser.parse_percentage(arguments)?);
1359+
saturation = color_parser.parse_percentage(arguments)?;
13791360
arguments.expect_comma()?;
1380-
lightness = Some(color_parser.parse_percentage(arguments)?);
1381-
Some(parse_legacy_alpha(color_parser, arguments)?)
1361+
lightness = color_parser.parse_percentage(arguments)?;
1362+
parse_legacy_alpha(color_parser, arguments)?
13821363
} else {
1383-
saturation = parse_none_or(arguments, |p| color_parser.parse_percentage(p))?;
1384-
lightness = parse_none_or(arguments, |p| color_parser.parse_percentage(p))?;
1364+
saturation = parse_none_or(arguments, |p| color_parser.parse_percentage(p))?.unwrap_or(0.0);
1365+
lightness = parse_none_or(arguments, |p| color_parser.parse_percentage(p))?.unwrap_or(0.0);
13851366

13861367
if !arguments.is_exhausted() {
13871368
arguments.expect_delim('/')?;
1388-
parse_none_or(arguments, |p| parse_alpha_component(color_parser, p))?
1369+
parse_none_or(arguments, |p| parse_alpha_component(color_parser, p))?.unwrap_or(0.0)
13891370
} else {
1390-
Some(OPAQUE)
1371+
OPAQUE
13911372
}
13921373
};
13931374

1394-
let hue = maybe_hue.map(|h| normalize_hue(h.degrees()));
1395-
let saturation = saturation.map(|s| s.clamp(0.0, 1.0));
1396-
let lightness = lightness.map(|s| s.clamp(0.0, 1.0));
1375+
let hue = if let Some(h) = maybe_hue {
1376+
normalize_hue(h.degrees())
1377+
} else {
1378+
0.0
1379+
};
1380+
let saturation = saturation.clamp(0.0, 1.0);
1381+
let lightness = lightness.clamp(0.0, 1.0);
13971382

13981383
Ok(P::Output::from_hsl(hue, saturation, lightness, alpha))
13991384
}

src/tests.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,7 @@ fn generic_parser() {
15191519
enum OutputType {
15201520
CurrentColor,
15211521
Rgba(u8, u8, u8, f32),
1522-
Hsl(Option<f32>, Option<f32>, Option<f32>, Option<f32>),
1522+
Hsl(f32, f32, f32, f32),
15231523
Hwb(Option<f32>, Option<f32>, Option<f32>, Option<f32>),
15241524
Lab(Option<f32>, Option<f32>, Option<f32>, Option<f32>),
15251525
Lch(Option<f32>, Option<f32>, Option<f32>, Option<f32>),
@@ -1543,12 +1543,7 @@ fn generic_parser() {
15431543
OutputType::Rgba(red, green, blue, alpha)
15441544
}
15451545

1546-
fn from_hsl(
1547-
hue: Option<f32>,
1548-
saturation: Option<f32>,
1549-
lightness: Option<f32>,
1550-
alpha: Option<f32>,
1551-
) -> Self {
1546+
fn from_hsl(hue: f32, saturation: f32, lightness: f32, alpha: f32) -> Self {
15521547
OutputType::Hsl(hue, saturation, lightness, alpha)
15531548
}
15541549

@@ -1619,17 +1614,15 @@ fn generic_parser() {
16191614
("rgb(1, 2, 3)", OutputType::Rgba(1, 2, 3, 1.0)),
16201615
("rgba(1, 2, 3, 0.4)", OutputType::Rgba(1, 2, 3, 0.4)),
16211616
("rgb(none none none / none)", OutputType::Rgba(0, 0, 0, 0.0)),
1617+
("rgb(1 none 3 / none)", OutputType::Rgba(1, 0, 3, 0.0)),
16221618
(
16231619
"hsla(45deg, 20%, 30%, 0.4)",
1624-
OutputType::Hsl(Some(45.0), Some(0.2), Some(0.3), Some(0.4)),
1625-
),
1626-
(
1627-
"hsl(45deg none none)",
1628-
OutputType::Hsl(Some(45.0), None, None, Some(1.0)),
1620+
OutputType::Hsl(45.0, 0.2, 0.3, 0.4),
16291621
),
1622+
("hsl(45deg none none)", OutputType::Hsl(45.0, 0.0, 0.0, 1.0)),
16301623
(
16311624
"hsl(none 10% none / none)",
1632-
OutputType::Hsl(None, Some(0.1), None, None),
1625+
OutputType::Hsl(0.0, 0.1, 0.0, 0.0),
16331626
),
16341627
(
16351628
"hwb(45deg 20% 30% / 0.4)",

0 commit comments

Comments
 (0)