From 5b7a4c18df9220543ffdb6552d7766c439f3a305 Mon Sep 17 00:00:00 2001 From: Rickard Laurin Date: Tue, 12 May 2020 12:55:52 +0200 Subject: [PATCH] fix: handle 0 and 360 hue equally with hsl fixes #85 --- __tests__/Ratio_test.re | 2 ++ src/HSL.re | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/__tests__/Ratio_test.re b/__tests__/Ratio_test.re index 939c491..1530b74 100644 --- a/__tests__/Ratio_test.re +++ b/__tests__/Ratio_test.re @@ -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) diff --git a/src/HSL.re b/src/HSL.re index 6f2b15d..a3ef886 100644 --- a/src/HSL.re +++ b/src/HSL.re @@ -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)|]; }; /* @@ -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) | _ => [||] }