Skip to content

Commit

Permalink
fix: handle 0 and 360 hue equally with hsl fixes #85
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed May 12, 2020
1 parent 2bed452 commit 5b7a4c1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions __tests__/Ratio_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ testAll(
("hsl(210, 30%, 48%)", "#ffffff", 4.47),
("hsl(210, 30%, 68%)", "#ffffff", 2.31),
("hsl(0, 0%, 20%)", "hsl(0, 0%, 100%)", 12.63),
("hsl(0, 100%, 40%)", "#fff", 5.89),
("hsl(360, 100%, 40%)", "#fff", 5.89),
],
((fg, bg, expected)) =>
expect(Ratio.calculate(fg, bg)) |> toEqual(expected)
Expand Down
9 changes: 8 additions & 1 deletion src/HSL.re
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ let createRgbFromHsl = (h, s, l) => {
let p = 2.0 *. l -. q;
let rgb = hueToRgb(p, q);

[|rgb(tempR), rgb(hue), rgb(tempB)|];
let b =
switch (tempB) {
| x when x < 0. => 0.
| x => x
};

[|rgb(tempR), rgb(hue), rgb(b)|];
};

/*
Expand All @@ -35,6 +41,7 @@ let convert = hsl => {
hsl =>
switch (hsl) {
| [|_, 0.0, l|] => [|l, l, l|]
| [|h, s, l|] when h === 3.6 => createRgbFromHsl(0., s, l)
| [|h, s, l|] => createRgbFromHsl(h, s, l)
| _ => [||]
}
Expand Down

0 comments on commit 5b7a4c1

Please # to comment.