in chartify/_core/colour.py [0:0]
def hsl2rgb(hsl):
"""Convert HSL representation towards RGB
:param h: Hue, position around the chromatic circle (h=1 equiv h=0)
:param s: Saturation, color saturation (0=full gray, 1=full color)
:param l: Ligthness, Overhaul lightness (0=full black, 1=full white)
:rtype: 3-uple for RGB values in float between 0 and 1
Hue, Saturation, Range from Lightness is a float between 0 and 1
Note that Hue can be set to any value but as it is a rotation
around the chromatic circle, any value above 1 or below 0 can
be expressed by a value between 0 and 1 (Note that h=0 is equiv
to h=1).
This algorithm came from:
http://www.easyrgb.com/index.php?X=MATH&H=19#text19
Here are some quick notion of HSL to RGB conversion:
>>> from colour import hsl2rgb
With a lightness put at 0, RGB is always rgbblack
>>> hsl2rgb((0.0, 0.0, 0.0))
(0.0, 0.0, 0.0)
>>> hsl2rgb((0.5, 0.0, 0.0))
(0.0, 0.0, 0.0)
>>> hsl2rgb((0.5, 0.5, 0.0))
(0.0, 0.0, 0.0)
Same for lightness put at 1, RGB is always rgbwhite
>>> hsl2rgb((0.0, 0.0, 1.0))
(1.0, 1.0, 1.0)
>>> hsl2rgb((0.5, 0.0, 1.0))
(1.0, 1.0, 1.0)
>>> hsl2rgb((0.5, 0.5, 1.0))
(1.0, 1.0, 1.0)
With saturation put at 0, the RGB should be equal to Lightness:
>>> hsl2rgb((0.0, 0.0, 0.25))
(0.25, 0.25, 0.25)
>>> hsl2rgb((0.5, 0.0, 0.5))
(0.5, 0.5, 0.5)
>>> hsl2rgb((0.5, 0.0, 0.75))
(0.75, 0.75, 0.75)
With saturation put at 1, and lightness put to 0.5, we can find
normal full red, green, blue colors:
>>> hsl2rgb((0 , 1.0, 0.5))
(1.0, 0.0, 0.0)
>>> hsl2rgb((1 , 1.0, 0.5))
(1.0, 0.0, 0.0)
>>> hsl2rgb((1.0/3 , 1.0, 0.5))
(0.0, 1.0, 0.0)
>>> hsl2rgb((2.0/3 , 1.0, 0.5))
(0.0, 0.0, 1.0)
Of course:
>>> hsl2rgb((0.0, 2.0, 0.5)) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: Saturation must be between 0 and 1.
And:
>>> hsl2rgb((0.0, 0.0, 1.5)) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: Lightness must be between 0 and 1.
"""
hue, saturation, lightness = [float(v) for v in hsl]
if not (0.0 - FLOAT_ERROR <= saturation <= 1.0 + FLOAT_ERROR):
raise ValueError("Saturation must be between 0 and 1.")
if not (0.0 - FLOAT_ERROR <= lightness <= 1.0 + FLOAT_ERROR):
raise ValueError("Lightness must be between 0 and 1.")
if saturation == 0:
return lightness, lightness, lightness
if lightness < 0.5:
v2 = lightness * (1.0 + saturation)
else:
v2 = (lightness + saturation) - (saturation * lightness)
v1 = 2.0 * lightness - v2
r = _hue2rgb(v1, v2, hue + (1.0 / 3))
g = _hue2rgb(v1, v2, hue)
b = _hue2rgb(v1, v2, hue - (1.0 / 3))
return r, g, b