Created
August 26, 2018 01:57
-
-
Save MatthaeusHarris/ba522d46c5be608b4856c0e4a9606a4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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