Skip to content

Instantly share code, notes, and snippets.

@tekk
Created December 28, 2023 16:22
Show Gist options
  • Save tekk/9c61d0ed0a4a2f0203f8015e87ad5708 to your computer and use it in GitHub Desktop.
Save tekk/9c61d0ed0a4a2f0203f8015e87ad5708 to your computer and use it in GitHub Desktop.
def hsv_to_rgb(h, s, v):
if s == 0.0:
r, g, b = v, v, v
else:
i = int(h * 6.0)
f = (h * 6.0) - i
p = v * (1.0 - s)
q = v * (1.0 - s * f)
t = v * (1.0 - s * (1.0 - f))
i = i % 6
if i == 0:
r, g, b = v, t, p
elif i == 1:
r, g, b = q, v, p
elif i == 2:
r, g, b = p, v, t
elif i == 3:
r, g, b = p, q, v
elif i == 4:
r, g, b = t, p, v
elif i == 5:
r, g, b = v, p, q
return round(r * 255), round(g * 255), round(b * 255)
# Example usage
h, s, v = 0.5, 0.5, 0.5 # Example HSV values (h, s, and v should be between 0.0 and 1.0)
r, g, b = hsv_to_rgb(h, s, v)
print(r, g, b) # Outputs the corresponding RGB values in the range 0 to 255
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment