Add color conversion utilities and message format example
- Add convert_and_reorder_colors() for hex to RGB conversion - Add msg.json example with new list-based select format - Support color order reordering based on device settings
This commit is contained in:
40
msg.json
Normal file
40
msg.json
Normal file
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"presets": {
|
||||
"test": {
|
||||
"pattern": "on",
|
||||
"colors": ["#FF0000", "#00FF00", "#0000FF"],
|
||||
"delay": 100
|
||||
},
|
||||
"test2": {
|
||||
"pattern": "rainbow",
|
||||
"colors": ["#FF0000", "#00FF00", "#0000FF"],
|
||||
"delay": 100
|
||||
},
|
||||
"test3": {
|
||||
"pattern": "pulse",
|
||||
"colors": ["#FF0000", "#00FF00", "#0000FF"],
|
||||
"delay": 100
|
||||
},
|
||||
"test4": {
|
||||
"pattern": "transition",
|
||||
"colors": ["#FF0000", "#00FF00", "#0000FF"],
|
||||
"delay": 100
|
||||
},
|
||||
"test5": {
|
||||
"pattern": "chase",
|
||||
"colors": ["#FF0000", "#00FF00", "#0000FF"],
|
||||
"delay": 100
|
||||
},
|
||||
"test6": {
|
||||
"pattern": "circle",
|
||||
"colors": ["#FF0000", "#00FF00", "#0000FF"],
|
||||
"delay": 100
|
||||
}
|
||||
},
|
||||
|
||||
"select": {
|
||||
"name1": "test",
|
||||
"name2": "test2",
|
||||
"name3": "test3"
|
||||
}
|
||||
}
|
||||
53
src/utils.py
Normal file
53
src/utils.py
Normal file
@@ -0,0 +1,53 @@
|
||||
def convert_and_reorder_colors(colors, settings_or_color_order):
|
||||
"""Convert hex color strings to RGB tuples and reorder based on device color order.
|
||||
|
||||
Args:
|
||||
colors: List of colors, either hex strings like "#FF0000" or RGB tuples like (255, 0, 0)
|
||||
settings_or_color_order: Either a Settings object or a color_order string (e.g., "rgb", "grb")
|
||||
|
||||
Returns:
|
||||
List of RGB tuples reordered according to device color order
|
||||
"""
|
||||
# Get channel order from settings or color_order string
|
||||
if hasattr(settings_or_color_order, 'get_rgb_channel_order'):
|
||||
# It's a Settings object
|
||||
channel_order = settings_or_color_order.get_rgb_channel_order()
|
||||
elif isinstance(settings_or_color_order, str):
|
||||
# It's a color_order string, convert to channel order
|
||||
color_order = settings_or_color_order.lower()
|
||||
color_orders = {
|
||||
"rgb": (1, 3, 5),
|
||||
"rbg": (1, 5, 3),
|
||||
"grb": (3, 1, 5),
|
||||
"gbr": (3, 5, 1),
|
||||
"brg": (5, 1, 3),
|
||||
"bgr": (5, 3, 1)
|
||||
}
|
||||
hex_indices = color_orders.get(color_order, (1, 3, 5))
|
||||
# Map hex string positions to RGB channel indices
|
||||
hex_to_channel = {1: 0, 3: 1, 5: 2}
|
||||
channel_order = tuple(hex_to_channel[pos] for pos in hex_indices)
|
||||
else:
|
||||
# Assume it's already a channel order tuple
|
||||
channel_order = 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)
|
||||
# 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
|
||||
rgb = tuple(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)
|
||||
return converted_colors
|
||||
Reference in New Issue
Block a user