This template computes the saturation value in [0..1] in the HSL color space (as defined in standard CSS3), from a color specified in the sRGB color space. The returned value is rounded to three decimals (which preserves the precision of RGB colors whose components are specified in the standard range [0..255]).

Syntax
{{RGBtoHSL.S|r|g|b}}
The range of the three parameters is [0..255] ; they can be any numeric expressions.
{{subst:RGBtoHSL.S|r|g|b|subst=subst:}}
To substitute the template invokation by the compute value when saving an article Wiki source.
Algorithm used
if r = g and g = b then
S = 0;
else if g >= r and g >= b then
if r > b
then if g + b > 255 then S = (g - b) / (510 - g - b); else S = (g - b) / (g + b);
else if g + r > 255 then S = (g - r) / (510 - g - r); else S = (g - r) / (g + r);
else if r >= b and r >= g then
if r > g
then if r + g > 255 then S = (r - g) / (510 - r - g); else S = (r - g) / (r + g);
else if r + b > 255 then S = (r - b) / (510 - r - b); else S = (r - b) / (r + b);
else
if g > r
then if b + r > 255 then S = (b - r) / (510 - b - r); else S = (b - r) / (b + r);
else if b + g > 255 then S = (b - g) / (510 - b - g); else S = (b - g) / (b + g);
return S round 3;
Examples
  • "{{RGBtoHSL.S|0|0|0}}" returns "0".
  • "{{RGBtoHSL.S|255|255|255}}" returns "0".
  • "{{RGBtoHSL.S|255|0|0}}" returns "1".
  • "{{RGBtoHSL.S|255|1|0}}" returns "1".
  • "{{RGBtoHSL.S|255|127.5|0}}" returns "1".
  • "{{RGBtoHSL.S|255|255|0}}" returns "1".
  • "{{RGBtoHSL.S|127.5|255|0}}" returns "1".
  • "{{RGBtoHSL.S|0|255|0}}" returns "1".
  • "{{RGBtoHSL.S|0|255|127.5}}" returns "1".
  • "{{RGBtoHSL.S|0|255|255}}" returns "1".
  • "{{RGBtoHSL.S|0|127.5|255}}" returns "1".
  • "{{RGBtoHSL.S|0|0|255}}" returns "1".
  • "{{RGBtoHSL.S|127.5|0|255}}" returns "1".
  • "{{RGBtoHSL.S|255|0|255}}" returns "1".
  • "{{RGBtoHSL.S|255|0|127.5}}" returns "1".
  • "{{RGBtoHSL.S|255|0|1}}" returns "1".
  • example for "sandybrown" = "#F4A460" = "rgb(244, 164, 96)" = "hsl(27.6, 0.871, 0.667)"
"{{RGBtoHSL.S|244|164|96}}" returns "0.871".
  • same example using subst
"{{subst:RGBtoHSL.S|244|164|96|subst=subst:}}" returns "0.871".


See also