python-colormath
python-colormath copied to clipboard
Converting from HSL to RGB or LAB gets odd results
If I try to convert from HSL I get some weird outputs:
For example:
from colormath.color_objects import LabColor, HSLColor, sRGBColor
color = HSLColor(89, 19, 90)
rgb = convert_color(color, sRGBColor)
lab = convert_color(color, LabColor)
print(color)
print(rgb)
print(lab)
Output looks like this:
HSLColor (hsl_h:89.0000 hsl_s:19.0000 hsl_l:90.0000)
sRGBColor (rgb_r:33.6333 rgb_g:-1601.0000 rgb_b:1781.0000)
LabColor (lab_l:18430.3065 lab_a:30243.3421 lab_b:-41190.8731)
As you can see this is obviously wrong, and when I use a tool such as this: http://colormine.org/color-converter I get results of:
# results are rounded
L*A*B* = (92.23, -3.27, 4.14)
RGB = (229, 234, 224)
After more playing around I've found I need to be dividing my HSL values, but I'm still getting weird results; when compare to colorsys conversion (where I get the correct result).
For example:
from colormath.color_conversions import convert_color
import colorsys
from colormath.color_objects import LabColor, HSLColor, sRGBColor
h = 89
s = 19
l = 90
color = HSLColor(h/360, s/100, l/100)
lab1 = convert_color(color, LabColor)
print(lab1)
r, g, b = colorsys.hls_to_rgb(h/360, l/100, s/100)
lab2 = convert_color(sRGBColor(r, g, b), LabColor)
print(r, g, b)
print(lab2)
Output is:
LabColor (lab_l:90.1623 lab_a:3.2667 lab_b:1.1810)
0.9006333333333334 0.919 0.881
LabColor (lab_l:92.2324 lab_a:-3.2794 lab_b:4.1440)
So when converting through colorsys.hls_to_rgb first then i get a correct output, but just using only convert_color I do not.
I've tried using through_rgb_type=sRGBColor but still does not give correct results.