refactor(driver): harden preset parsing and refresh tooling
This commit is contained in:
33
src/utils.py
33
src/utils.py
@@ -33,21 +33,26 @@ def convert_and_reorder_colors(colors, settings_or_color_order):
|
||||
|
||||
converted_colors = []
|
||||
for color in colors:
|
||||
# Convert "#RRGGBB" to (R, G, B)
|
||||
if isinstance(color, str) and color.startswith("#"):
|
||||
r = int(color[1:3], 16)
|
||||
g = int(color[3:5], 16)
|
||||
b = int(color[5:7], 16)
|
||||
rgb = (r, g, b)
|
||||
try:
|
||||
# Convert "#RRGGBB" to (R, G, B)
|
||||
if isinstance(color, str) and color.startswith("#") and len(color) == 7:
|
||||
r = int(color[1:3], 16)
|
||||
g = int(color[3:5], 16)
|
||||
b = int(color[5:7], 16)
|
||||
rgb = (r, g, b)
|
||||
elif isinstance(color, (list, tuple)) and len(color) == 3:
|
||||
# Already a tuple/list, just coerce and clamp.
|
||||
rgb = tuple(max(0, min(255, int(x))) for x in color)
|
||||
else:
|
||||
# Unknown format: ignore safely.
|
||||
continue
|
||||
|
||||
# Reorder based on device color order
|
||||
reordered = (rgb[channel_order[0]], rgb[channel_order[1]], rgb[channel_order[2]])
|
||||
converted_colors.append(reordered)
|
||||
elif isinstance(color, (list, tuple)) and len(color) == 3:
|
||||
# Already a tuple/list, just reorder (JSON may use string numbers)
|
||||
rgb = tuple(int(x) for x in color)
|
||||
reordered = (rgb[channel_order[0]], rgb[channel_order[1]], rgb[channel_order[2]])
|
||||
converted_colors.append(reordered)
|
||||
else:
|
||||
# Keep as-is if not recognized format
|
||||
converted_colors.append(color)
|
||||
except (TypeError, ValueError, IndexError):
|
||||
# Skip malformed color entries to avoid crashing pattern loops.
|
||||
continue
|
||||
if not converted_colors:
|
||||
converted_colors.append((255, 255, 255))
|
||||
return converted_colors
|
||||
|
||||
Reference in New Issue
Block a user