Skip to content
Snippets Groups Projects
Verified Commit f1bbbd1a authored by dos's avatar dos
Browse files

sim: fakes: Implement RGB <-> HSV color conversions

parent b50fc6f2
No related branches found
No related tags found
1 merge request!600Simulator improvements
from _sim import _sim
from sys_colors import hsv_to_rgb
from math import tau
import pygame
......@@ -30,11 +32,7 @@ def set_all_rgb(r, g, b):
def set_hsv(ix, h, s, v):
color = pygame.Color(0)
h = int(h)
h = h % 360
color.hsva = (h, s * 100, v * 100, 1.0)
set_rgb(ix, color.r / 255, color.g / 255, color.b / 255)
set_rgb(ix, *hsv_to_rgb(h / 360 * tau, s, v))
def set_all_hsv(h, s, v):
......
from colorsys import hsv_to_rgb, rgb_to_hsv
from typing import Tuple
import pygame
from math import tau
def hsv_to_rgb(h: float, s: float, v: float) -> Tuple[float, float, float]:
color = pygame.Color(0)
color.hsva = ((h % tau) / tau * 360, s * 100, v * 100, 100)
return color.normalize()[:3]
def rgb_to_hsv(r: float, g: float, b: float) -> Tuple[float, float, float]:
r = min(1, max(0, r))
g = min(1, max(0, g))
b = min(1, max(0, b))
color = pygame.Color(r, g, b)
h, s, v, a = color.hsva
return (h / 360 * tau, s / 100, v / 100)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment