Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix most type hinting issues in library/lcd #622

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions library/lcd/color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Union, Tuple

from PIL import ImageColor

RGBColor = Tuple[int, int, int]

# Color can be an RGB tuple (RGBColor), or a string in any of these formats:
# - "r, g, b" (e.g. "255, 0, 0"), as is found in the themes' yaml settings
# - any of the formats supported by PIL: https://pillow.readthedocs.io/en/stable/reference/ImageColor.html
#
# For example, here are multiple ways to write the pure red color:
# - (255, 0, 0)
# - "255, 0, 0"
# - "#ff0000"
# - "red"
# - "hsl(0, 100%, 50%)"
Color = Union[str, RGBColor]

def parse_color(color: Color) -> RGBColor:
# even if undocumented, let's be nice and accept a list in lieu of a tuple
if isinstance(color, tuple) or isinstance(color, list):
if len(color) != 3:
raise ValueError("RGB color must have 3 values")
return (int(color[0]), int(color[1]), int(color[2]))

if not isinstance(color, str):
raise ValueError("Color must be either an RGB tuple or a string")

# Try to parse it as our custom "r, g, b" format
rgb = color.split(',')
if len(rgb) == 3:
r, g, b = rgb
try:
rgbcolor = (int(r.strip()), int(g.strip()), int(b.strip()))
except ValueError:
# at least one element can't be converted to int, we continue to
# try parsing as a PIL color
pass
else:
return rgbcolor

# fallback as a PIL color
rgbcolor = ImageColor.getrgb(color)
if len(rgbcolor) == 4:
return (rgbcolor[0], rgbcolor[1], rgbcolor[2])
return rgbcolor

Loading
Loading