Skip to content

Instantly share code, notes, and snippets.

@MatthaeusHarris
Created August 12, 2018 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthaeusHarris/25c1a0b534c10b059820071c25e2f9a1 to your computer and use it in GitHub Desktop.
Save MatthaeusHarris/25c1a0b534c10b059820071c25e2f9a1 to your computer and use it in GitHub Desktop.
def hsv2rgb(h,s,v):
"""
0 <= h < 360 # Hue
0 <= s <= 255 # Saturation
0 <= v <= 255 # Value
Hue: position on the color wheel (0 is red)
Saturation: How much of the color is present
Value: How bright the resultant color is
So bright red would be (0, 255, 255) and white would be (*, 0, 255). Black is (*, *, 0).
"""
s = s / 255.0
v = v / 255.0
c = v * s
x = c * (1 - math.fabs((h / 60) % 2 - 1))
m = v - c
if h < 60:
rgb = (c, x, 0)
elif h < 120:
rgb = (x, c, 0)
elif h < 180:
rgb = (0, c, x)
elif h < 240:
rgb = (0, x, c)
elif h < 300:
rgb = (x, 0, c)
else:
rgb = (c, 0, x)
return (int((rgb[0]+m) * 255), int((rgb[1]+m)*255), int((rgb[2]+m)*255))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment