forked from andreeuuetoa/litsimaja
Add TKinter Visualizer Emulator(Slow but works)
This commit is contained in:
52
pyleds/lib/Color.py
Normal file
52
pyleds/lib/Color.py
Normal file
@@ -0,0 +1,52 @@
|
||||
Color = lambda r, g, b: (r << 16) | (g << 8) | b
|
||||
|
||||
|
||||
def Color_to_list(color):
|
||||
rgb = [(color >> 16) & 255, (color >> 8) & 255, color & 255]
|
||||
w = (color >> 24) & 255
|
||||
if w:
|
||||
rgb.append(w)
|
||||
return rgb
|
||||
|
||||
|
||||
def Color_to_hex(color):
|
||||
s = [(color >> 16) & 255, (color >> 8) & 255, color & 255]
|
||||
return "#"+''.join(map(lambda y: ("0" + hex(int(y))[2:])[-2:], s))
|
||||
|
||||
|
||||
def Color_to_rgb(color):
|
||||
return (color >> 16) & 255, (color >> 8) & 255, color & 255
|
||||
|
||||
|
||||
def hex_to_rgb(value):
|
||||
value = value.lstrip('#') if value[0] == "#" else value
|
||||
lv = len(value)
|
||||
return [int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3)]
|
||||
|
||||
|
||||
def to_Color(value):
|
||||
if type(value) == str:
|
||||
r, g, b = hex_to_rgb(value)
|
||||
return Color(r, g, b)
|
||||
if type(value) == int:
|
||||
if 0 <= value <= 16777215:
|
||||
return value
|
||||
else:
|
||||
raise ValueError("RGB Int exceeded the lower and upper limits")
|
||||
if type(value) == list or type(value) == tuple:
|
||||
out = []
|
||||
for n, code in enumerate(value):
|
||||
if not str(code).isnumeric():
|
||||
raise ValueError("RGB color must be numeric")
|
||||
if n > 4:
|
||||
raise ValueError("RGB contains more than 4 values")
|
||||
_code = int(code)
|
||||
if _code < 0:
|
||||
print(f"RGB value under 0")
|
||||
out.append(0)
|
||||
elif _code > 255:
|
||||
print(f"RGB value over 255")
|
||||
out.append(255)
|
||||
else:
|
||||
out.append(_code)
|
||||
return Color(out[0], out[1], out[2])
|
||||
Reference in New Issue
Block a user