Skip to content Skip to sidebar Skip to footer

Convert Hsl To Hsv Color Codes And Vice Versa In Python

I'm currently trying to convert hsl color codes to hsv and vice versa in Python, but can't find an easy way to do this. I tried converting the codes indirectly by converting hsl to

Solution 1:

You need to make sure to normalize the values according to the matching ranges. According to your values (and the range hints in this website), it seems you're using values of H in the range [0, 360] ; S and V in the range [0, 100]. On the other hand, the built-in library colorsys uses all [0, 1] ranges. So a bit of normalization will give you the required results:

import colorsys

defhsv_to_hsl(hsv):
    h, s, v = hsv
    rgb = colorsys.hsv_to_rgb(h/360, s/100, v/100)
    r, g, b = rgb
    h, l, s = colorsys.rgb_to_hls(r, g, b)
    return h*360, s*100, l*100

hsv = (300, 65, 40)

print(hsv)
print(hsv_to_hsl(hsv))

Now gives the expected:

(300, 65, 40)
(300.0, 48.148148148148145, 27.0)

If you are looking for a manual implementation, you can use the algorithm described here (now the values are: H in range [0, 360] and S,V and L are in [0, 1]):

defhsv_to_hsl(hsv):
    h, s, v = hsv
    l = v * (1 - s/2)
    s = 0if l in (0, 1) else (v - l)/min(l, 1-l)
    return h, s, l

defhsl_to_hsv(hsl):
    h, s, l = hsl
    v = l + s * min(l, 1-l)
    s = 0if v == 0else2*(1 - l/v)
    return h, s, v

To compare the result with colorsys:

h, s, l = (300, 48.148, 27)

custom = hsl_to_hsv((h, s/100, l/100))
print("Custom function: ", custom[0], custom[1]*100, custom[2]*100)
expected = colorsys.rgb_to_hsv(*colorsys.hls_to_rgb(h/360, l/100, s/100))
print("Expected result: ", expected[0]*360, expected[1]*100, expected[2]*100)

Indeed gives:

Custom function:  30064.9998649998649839.99996
Expected result:  300.064.99986499986539.99996

Solution 2:

Not a native Python coder so this is pretty simplistic, but is based on the formula on Wikipedia and returns accurate values according to colorizer.org:

defhsl_to_hsv(hsl):
    hue,sat,lum = hsl
    if hue < 0or hue > 360or sat < 0or sat > 1or lum < 0or lum > 1:
        exit

    h = hue
    v = lum + sat * min(lum, 1 - lum)
    if v == 0:
        s = 0else:
        s = 2 * (1 - lum / v)

    return h,s,v

defhsv_to_hsl(hsv):
    hue,sat,val = hsv
    if hue < 0or hue > 360or sat < 0or sat > 1or val < 0or val > 1:
        exit

    h = hue
    l = val * (1 - sat / 2)
    if l == 0or l == 1:
        s = 0else:
        s = (val - l) / min(l, 1 - l)

    return h,s,l

print (hsl_to_hsv((36.9,0.5118,0.751)))
print (hsv_to_hsl((36.9,0.2902,0.8784)))

Output:

(36.9, 0.29014721809684496, 0.8784382)
(36.9, 0.5117560784762163, 0.75094416)

Note that hue is a float with values 0 to 360, the other values are floats with values from 0 to 1, often expressed as percentages. Your problem using the colorsys library may have been that you were passing larger values like 29 instead of 0.29.

Solution 3:

You can check out the colorsys module:

https://docs.python.org/3.7/library/colorsys.html

Examples from the site:

colorsys.rgb_to_yiq(r, g, b)
#Convert the colorfrom RGB coordinates to YIQ coordinates.

colorsys.yiq_to_rgb(y, i, q)
#Convert the colorfrom YIQ coordinates to RGB coordinates.

colorsys.rgb_to_hls(r, g, b)
#Convert the colorfrom RGB coordinates to HLS coordinates.

colorsys.hls_to_rgb(h, l, s)
#Convert the colorfrom HLS coordinates to RGB coordinates.

colorsys.rgb_to_hsv(r, g, b)
#Convert the colorfrom RGB coordinates to HSV coordinates.

colorsys.hsv_to_rgb(h, s, v)
#Convert the colorfrom HSV coordinates to RGB coordinates.

From this, you can convert hls -> rgb -> hsv

Post a Comment for "Convert Hsl To Hsv Color Codes And Vice Versa In Python"