Skip to content

Commit f962676

Browse files
committed
Clean up legacy parsing functions.
1 parent 56ec454 commit f962676

File tree

1 file changed

+75
-104
lines changed

1 file changed

+75
-104
lines changed

src/color.rs

Lines changed: 75 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,21 +1276,15 @@ where
12761276
fn parse_legacy_alpha<'i, 't, P>(
12771277
color_parser: &P,
12781278
arguments: &mut Parser<'i, 't>,
1279-
uses_commas: bool,
1280-
) -> Result<Option<f32>, ParseError<'i, P::Error>>
1279+
) -> Result<f32, ParseError<'i, P::Error>>
12811280
where
12821281
P: ColorParser<'i>,
12831282
{
12841283
Ok(if !arguments.is_exhausted() {
1285-
if uses_commas {
1286-
arguments.expect_comma()?;
1287-
Some(parse_alpha_component(color_parser, arguments)?)
1288-
} else {
1289-
arguments.expect_delim('/')?;
1290-
parse_none_or(arguments, |p| parse_alpha_component(color_parser, p))?
1291-
}
1284+
arguments.expect_comma()?;
1285+
parse_alpha_component(color_parser, arguments)?
12921286
} else {
1293-
Some(OPAQUE)
1287+
OPAQUE
12941288
})
12951289
}
12961290

@@ -1302,60 +1296,62 @@ fn parse_rgb<'i, 't, P>(
13021296
where
13031297
P: ColorParser<'i>,
13041298
{
1305-
// Either integers or percentages, but all the same type.
1306-
// https://drafts.csswg.org/css-color/#rgb-functions
1307-
13081299
let maybe_red = parse_none_or(arguments, |p| color_parser.parse_number_or_percentage(p))?;
13091300

1310-
let (red, is_number, is_legacy_syntax) = if let Some(red) = maybe_red {
1311-
let is_legacy_syntax = arguments.try_parse(|i| i.expect_comma()).is_ok();
1312-
match red {
1301+
// If the first component is not "none" and is followed by a comma, then we
1302+
// are parsing the legacy syntax.
1303+
let is_legacy_syntax = maybe_red.is_some() && arguments.try_parse(|p| p.expect_comma()).is_ok();
1304+
1305+
let red: u8;
1306+
let green: u8;
1307+
let blue: u8;
1308+
1309+
let alpha = if is_legacy_syntax {
1310+
match maybe_red.unwrap() {
13131311
NumberOrPercentage::Number { value } => {
1314-
(clamp_floor_256_f32(value), true, is_legacy_syntax)
1312+
red = clamp_floor_256_f32(value);
1313+
green = clamp_floor_256_f32(color_parser.parse_number(arguments)?);
1314+
arguments.expect_comma()?;
1315+
blue = clamp_floor_256_f32(color_parser.parse_number(arguments)?);
13151316
}
13161317
NumberOrPercentage::Percentage { unit_value } => {
1317-
(clamp_unit_f32(unit_value), false, is_legacy_syntax)
1318+
red = clamp_unit_f32(unit_value);
1319+
green = clamp_unit_f32(color_parser.parse_percentage(arguments)?);
1320+
arguments.expect_comma()?;
1321+
blue = clamp_unit_f32(color_parser.parse_percentage(arguments)?);
13181322
}
13191323
}
1320-
} else {
1321-
(0, true, false)
1322-
};
13231324

1324-
let green;
1325-
let blue;
1326-
if is_number {
1327-
// parse numbers
1328-
if is_legacy_syntax {
1329-
green = clamp_floor_256_f32(color_parser.parse_number(arguments)?);
1330-
arguments.expect_comma()?;
1331-
blue = clamp_floor_256_f32(color_parser.parse_number(arguments)?);
1332-
} else {
1333-
green = clamp_floor_256_f32(
1334-
parse_none_or(arguments, |p| color_parser.parse_number(p))?.unwrap_or(0.0),
1335-
);
1336-
blue = clamp_floor_256_f32(
1337-
parse_none_or(arguments, |p| color_parser.parse_number(p))?.unwrap_or(0.0),
1338-
);
1339-
}
1325+
parse_legacy_alpha(color_parser, arguments)?
13401326
} else {
1341-
// parse percentages
1342-
if is_legacy_syntax {
1343-
green = clamp_unit_f32(color_parser.parse_percentage(arguments)?);
1344-
arguments.expect_comma()?;
1345-
blue = clamp_unit_f32(color_parser.parse_percentage(arguments)?);
1346-
} else {
1347-
green = clamp_unit_f32(
1348-
parse_none_or(arguments, |p| color_parser.parse_percentage(p))?.unwrap_or(0.0),
1349-
);
1350-
blue = clamp_unit_f32(
1351-
parse_none_or(arguments, |p| color_parser.parse_percentage(p))?.unwrap_or(0.0),
1352-
);
1327+
#[inline]
1328+
fn get_component_value(c: &Option<NumberOrPercentage>) -> u8 {
1329+
match *c {
1330+
Some(NumberOrPercentage::Number { value }) => clamp_floor_256_f32(value),
1331+
Some(NumberOrPercentage::Percentage { unit_value }) => clamp_unit_f32(unit_value),
1332+
None => 0,
1333+
}
13531334
}
1354-
}
13551335

1356-
let alpha = parse_legacy_alpha(color_parser, arguments, is_legacy_syntax)?;
1336+
red = get_component_value(&maybe_red);
13571337

1358-
Ok(P::Output::from_rgba(red, green, blue, alpha.unwrap_or(0.0)))
1338+
green = get_component_value(&parse_none_or(arguments, |p| {
1339+
color_parser.parse_number_or_percentage(p)
1340+
})?);
1341+
1342+
blue = get_component_value(&parse_none_or(arguments, |p| {
1343+
color_parser.parse_number_or_percentage(p)
1344+
})?);
1345+
1346+
if !arguments.is_exhausted() {
1347+
arguments.expect_delim('/')?;
1348+
parse_none_or(arguments, |p| parse_alpha_component(color_parser, p))?.unwrap_or(0.0)
1349+
} else {
1350+
OPAQUE
1351+
}
1352+
};
1353+
1354+
Ok(P::Output::from_rgba(red, green, blue, alpha))
13591355
}
13601356

13611357
/// Parses hsl syntax.
@@ -1369,15 +1365,33 @@ fn parse_hsl<'i, 't, P>(
13691365
where
13701366
P: ColorParser<'i>,
13711367
{
1372-
let (hue, saturation, lightness, alpha) = parse_legacy_components(
1373-
color_parser,
1374-
arguments,
1375-
P::parse_angle_or_number,
1376-
P::parse_percentage,
1377-
P::parse_percentage,
1378-
)?;
1368+
let maybe_hue = parse_none_or(arguments, |p| color_parser.parse_angle_or_number(p))?;
13791369

1380-
let hue = hue.map(|h| normalize_hue(h.degrees()));
1370+
// If the hue is not "none" and is followed by a comma, then we are parsing
1371+
// the legacy syntax.
1372+
let is_legacy_syntax = maybe_hue.is_some() && arguments.try_parse(|p| p.expect_comma()).is_ok();
1373+
1374+
let saturation: Option<f32>;
1375+
let lightness: Option<f32>;
1376+
1377+
let alpha = if is_legacy_syntax {
1378+
saturation = Some(color_parser.parse_percentage(arguments)?);
1379+
arguments.expect_comma()?;
1380+
lightness = Some(color_parser.parse_percentage(arguments)?);
1381+
Some(parse_legacy_alpha(color_parser, arguments)?)
1382+
} 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))?;
1385+
1386+
if !arguments.is_exhausted() {
1387+
arguments.expect_delim('/')?;
1388+
parse_none_or(arguments, |p| parse_alpha_component(color_parser, p))?
1389+
} else {
1390+
Some(OPAQUE)
1391+
}
1392+
};
1393+
1394+
let hue = maybe_hue.map(|h| normalize_hue(h.degrees()));
13811395
let saturation = saturation.map(|s| s.clamp(0.0, 1.0));
13821396
let lightness = lightness.map(|s| s.clamp(0.0, 1.0));
13831397

@@ -1553,50 +1567,7 @@ where
15531567
))
15541568
}
15551569

1556-
/// Try to parse the components and alpha with the legacy syntax, but also allow
1557-
/// the [color-4] syntax if that fails.
1558-
/// https://drafts.csswg.org/css-color-4/#color-syntax-legacy
1559-
pub fn parse_legacy_components<'i, 't, P, F1, F2, F3, R1, R2, R3>(
1560-
color_parser: &P,
1561-
input: &mut Parser<'i, 't>,
1562-
f1: F1,
1563-
f2: F2,
1564-
f3: F3,
1565-
) -> Result<(Option<R1>, Option<R2>, Option<R3>, Option<f32>), ParseError<'i, P::Error>>
1566-
where
1567-
P: ColorParser<'i>,
1568-
F1: FnOnce(&P, &mut Parser<'i, 't>) -> Result<R1, ParseError<'i, P::Error>>,
1569-
F2: FnOnce(&P, &mut Parser<'i, 't>) -> Result<R2, ParseError<'i, P::Error>>,
1570-
F3: FnOnce(&P, &mut Parser<'i, 't>) -> Result<R3, ParseError<'i, P::Error>>,
1571-
{
1572-
let r1 = parse_none_or(input, |p| f1(color_parser, p))?;
1573-
// If the first argument is not none and we are separating with commas,
1574-
// only then are we parsing legacy syntax.
1575-
let is_legacy_syntax = r1.is_some() && input.try_parse(|i| i.expect_comma()).is_ok();
1576-
1577-
let r2;
1578-
let r3;
1579-
let alpha;
1580-
if is_legacy_syntax {
1581-
r2 = Some(f2(color_parser, input)?);
1582-
input.expect_comma()?;
1583-
r3 = Some(f3(color_parser, input)?);
1584-
alpha = parse_legacy_alpha(color_parser, input, is_legacy_syntax)?;
1585-
} else {
1586-
r2 = parse_none_or(input, |p| f2(color_parser, p))?;
1587-
r3 = parse_none_or(input, |p| f3(color_parser, p))?;
1588-
if !input.is_exhausted() {
1589-
input.expect_delim('/')?;
1590-
alpha = parse_none_or(input, |p| parse_alpha_component(color_parser, p))?;
1591-
} else {
1592-
alpha = Some(OPAQUE);
1593-
}
1594-
};
1595-
1596-
Ok((r1, r2, r3, alpha))
1597-
}
1598-
1599-
/// Parse the color components and alpha with the [color-4] syntax.
1570+
/// Parse the color components and alpha with the modern [color-4] syntax.
16001571
pub fn parse_components<'i, 't, P, F1, F2, F3, R1, R2, R3>(
16011572
color_parser: &P,
16021573
input: &mut Parser<'i, 't>,

0 commit comments

Comments
 (0)