From 405a7b1c036fef5eac00cc042c4a93f46b7dcfea Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 4 Oct 2020 11:25:12 +0300 Subject: [PATCH] Update RGBToHSL.md --- snippets/RGBToHSL.md | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/snippets/RGBToHSL.md b/snippets/RGBToHSL.md index 220842284..1d7ddb2a1 100644 --- a/snippets/RGBToHSL.md +++ b/snippets/RGBToHSL.md @@ -10,18 +10,27 @@ Converts a RGB color tuple to HSL format. - The range of the resulting values is H: [0, 360], S: [0, 100], L: [0, 100]. ```js -const RGBToHSL = ((r, g, b) => { +const RGBToHSL = (r, g, b) => { r /= 255; g /= 255; b /= 255; const l = Math.max(r, g, b); const s = l - Math.min(r, g, b); - const h = - (s) ? (l === r) ? (g-b)/s : (l === g) ? 2 + (b-r)/s : 4 + (r-g)/s : 0; - return [ (60*h<0)?(60*h+360):(60*h) , 100*((s) ? (l<=0.5) ? s/((2*l-s)) : s/(2 - (2*l-s)) : 0), 100*(2*l-s)/2]; + const h = s + ? l === r + ? (g - b) / s + : l === g + ? 2 + (b - r) / s + : 4 + (r - g) / s + : 0; + return [ + 60 * h < 0 ? 60 * h + 360 : 60 * h, + 100 * (s ? (l <= 0.5 ? s / (2 * l - s) : s / (2 - (2 * l - s))) : 0), + (100 * (2 * l - s)) / 2, + ]; }; ``` ```js -RGBToHSL(45, 23, 11); // [21.17647058823529, 60.71428571428573, 10.980392156862745] +RGBToHSL(45, 23, 11); // [21.17647, 60.71428, 10.98039] ```