Skip to content

Commit

Permalink
fix parsing lab & lch #2
Browse files Browse the repository at this point in the history
  • Loading branch information
mazznoer committed Jul 23, 2022
1 parent 6ed6d9e commit a213f2b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,10 @@ pub fn parse(s: &str) -> Result<Color, ParseColorError> {
Some((1.0, true))
};

if let (Some((l, _)), Some((a, a_fmt)), Some((b, b_fmt)), Some((alpha, _))) =
if let (Some((l, l_fmt)), Some((a, a_fmt)), Some((b, b_fmt)), Some((alpha, _))) =
(l, a, b, alpha)
{
let l = if l_fmt { l * 100.0 } else { l };
let a = if a_fmt {
remap(a, -1.0, 1.0, -125.0, 125.0)
} else {
Expand All @@ -225,7 +226,7 @@ pub fn parse(s: &str) -> Result<Color, ParseColorError> {
} else {
b
};
return Ok(Color::from_lab(l.max(0.0) * 100.0, a, b, alpha));
return Ok(Color::from_lab(l.max(0.0), a, b, alpha));
}

return Err(ParseColorError::InvalidLab);
Expand All @@ -246,12 +247,13 @@ pub fn parse(s: &str) -> Result<Color, ParseColorError> {
Some((1.0, true))
};

if let (Some((l, _)), Some((c, c_fmt)), Some(h), Some((alpha, _))) =
if let (Some((l, l_fmt)), Some((c, c_fmt)), Some(h), Some((alpha, _))) =
(l, c, h, alpha)
{
let l = if l_fmt { l * 100.0 } else { l };
let c = if c_fmt { c * 150.0 } else { c };
return Ok(Color::from_lch(
l * 100.0,
l.max(0.0),
c.max(0.0),
h.to_radians(),
alpha,
Expand Down
4 changes: 4 additions & 0 deletions tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ fn equal() {
("hwb(180 0% 60%)", "hwb(180,0%,60%)"),
("hwb(290 30% 0%)", "hwb(290 0.3 0)"),
("hsl(180,50%,27%)", "hsl(180,0.5,0.27)"),
("rgb(255, 165, 0)", "hsl(38.824 100% 50%)"),
("#7654CD", "rgb(46.27% 32.94% 80.39%)"),
//#[cfg(feature = "lab")]
//("#7654CD", "lab(44.36% 36.05 -58.99)"),
];

for (a, b) in test_data {
Expand Down

0 comments on commit a213f2b

Please sign in to comment.