From d513f951dd6e9d3517e144da9151d9db0e410105 Mon Sep 17 00:00:00 2001 From: RNL Date: Tue, 24 Feb 2026 21:33:43 +1100 Subject: [PATCH] added AMS hex color trimming --- bambu_run/management/commands/bambu_collector.py | 3 +-- bambu_run/utils.py | 8 ++++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/bambu_run/management/commands/bambu_collector.py b/bambu_run/management/commands/bambu_collector.py index 4b18dc0..a94742e 100644 --- a/bambu_run/management/commands/bambu_collector.py +++ b/bambu_run/management/commands/bambu_collector.py @@ -332,7 +332,6 @@ class Command(BaseCommand): color_code = strip_color_padding(mqtt_color) color_hex = f"#{color_code}" if color_code else None - color_name = mqtt_color filament_color = match_filament_color( filament_type=type_val, filament_sub_type=sub_type, @@ -345,7 +344,7 @@ class Command(BaseCommand): if self.verbose: logger.info(f"Matched color from database: {color_name} (#{color_code})") else: - color_name = mqtt_color + color_name = color_hex or mqtt_color if self.verbose: logger.warning( f"No color match in database for {type_val} {sub_type} #{color_code}. " diff --git a/bambu_run/utils.py b/bambu_run/utils.py index 1dbf8cc..c28e786 100644 --- a/bambu_run/utils.py +++ b/bambu_run/utils.py @@ -2,6 +2,10 @@ Utility functions for filament color matching """ +# BambuLab AMS reports colors as 8-char hex with an alpha channel suffix (e.g. '489FDFFF'). +# The last two chars are always 'FF' (fully opaque). Only the first 6 chars are the RGB value. +MQTT_COLOR_HEX_LENGTH = 6 + def strip_color_padding(mqtt_color): """ @@ -12,8 +16,8 @@ def strip_color_padding(mqtt_color): if not mqtt_color: return None if len(mqtt_color) == 8: - return mqtt_color[:6].upper() - return mqtt_color[:6].upper() if len(mqtt_color) >= 6 else mqtt_color.upper() + return mqtt_color[:MQTT_COLOR_HEX_LENGTH].upper() + return mqtt_color[:MQTT_COLOR_HEX_LENGTH].upper() if len(mqtt_color) >= MQTT_COLOR_HEX_LENGTH else mqtt_color.upper() def match_filament_color(filament_type, filament_sub_type, color_code, brand='Bambu Lab'):