kiryha
December 15, 2020, 5:37pm
1
Hello developers,
I have an HSV value (according to docs) which is [8.67, 0.98, 0.63] and I cant convert it to RGB which should be [208, 89, 24]
import colorsys
value_hsv = [8.67, 0.98, 0.63]
value_rgb = colorsys.hsv_to_rgb(value_hsv[0], value_hsv[1], value_hsv[2])
print value_rgb[0]*255, value_rgb[1]*255, value_rgb[2]*255
Any ideas?
clesage
December 16, 2020, 5:28pm
2
I don’t know, but the examples in the docs suggest that the HSV values should be in the range of 0 to 1. You have H = 8.67.
Edit this SO comment would agree, 0-1 ranges: python - RGB to HSV wrong in colorsys? - Stack Overflow
bob.w
December 16, 2020, 5:35pm
3
Yeah colorsys
wants everything in 0-1 range. We wrote a bunch of wrapper functions to deal with different systems preferring different ranges to get stuff in and out properly.
Was a bit of a pain.
kiryha
December 16, 2020, 8:03pm
4
value_hsv = [8.67/360, 0.98, 0.63] gives [160, 25, 3] which is also far from RGB!
chalk
December 17, 2020, 7:39am
5
This looks to be the correct code for the conversion - im trying to understand what YIQ space is seeing as some of the values can be negative…
Based on this site:
Tested against this site:
import math
def hsv2rgb(h, s, v):
"""HSV to RGB
:param float h: 0.0 - 360.0
:param float s: 0.0 - 1.0
:param float v: 0.0 - 1.0
:return: rgb
:rtype: list
"""
c = v * s
x = c * (1 - abs(((h/60.0) % 2) - 1))
m = v - c
if 0.0 <= h < 60:
rgb = (c, x, 0)
elif 0.0 <= h < 120:
rgb = (x, c, 0)
elif 0.0 <= h < 180:
rgb = (0, c, x)
elif 0.0 <= h < 240:
rgb = (0, x, c)
elif 0.0 <= h < 300:
rgb = (x, 0, c)
elif 0.0 <= h < 360:
rgb = (c, 0, x)
return list(map(lambda n: (n + m) * 255, rgb))
# Example
print(hsv2rgb(57, 1, .5))
Running this from RGB to HSL in the colorsys module:
import colorsys
print(colorsys.rgb_to_hsv(208/255., 89/255., 24/255.))
gives us: (0.0588768115942029, 0.8846153846153846, 0.8156862745098039)
1 Like
mudoglu
December 17, 2020, 9:33am
6