Honor WebAPI button colors without background override

This commit is contained in:
Meik
2026-03-05 12:40:08 +01:00
parent 57094a3036
commit 3f39165b81

View File

@@ -234,6 +234,24 @@ namespace C4IT_CustomerPanel.libs
return ((0.2126 * color.R) + (0.7152 * color.G) + (0.0722 * color.B)) / 255.0;
}
private static bool TryGetConfiguredUiColor(Dictionary<string, string> uiColors, string key, out Color color)
{
color = Colors.Transparent;
if (uiColors == null || !uiColors.TryGetValue(key, out string rawColor) || string.IsNullOrWhiteSpace(rawColor))
return false;
try
{
color = ConverterHelper.ColorConvertFromString(rawColor);
return true;
}
catch
{
return false;
}
}
private static void ApplyBackgroundDerivedResources(Color baseColor)
{
bool isLightBackground = GetRelativeLuminance(baseColor) >= 0.55;
@@ -1000,25 +1018,42 @@ namespace C4IT_CustomerPanel.libs
{
try
{
Color backgroundColor = (Color)ConverterHelper.ColorConvertFromString(cpConfig._uiColors["backgroundColor"]);
Color headerColor = (Color)ConverterHelper.ColorConvertFromString(cpConfig._uiColors["headerColor"]);
var uiColors = cpConfig?._uiColors;
Color backgroundColor = TryGetConfiguredUiColor(uiColors, "backgroundColor", out Color configuredBackground)
? configuredBackground
: ConverterHelper.ColorConvertFromString(Properties.Resources.backgroundColor);
Color headerColor = TryGetConfiguredUiColor(uiColors, "headerColor", out Color configuredHeader)
? configuredHeader
: ConverterHelper.ColorConvertFromString(Properties.Resources.headerColor);
bool hasConfiguredActiveColor = TryGetConfiguredUiColor(uiColors, "activeButtonColor", out Color configuredActiveButtonColor);
bool hasConfiguredInactiveColor = TryGetConfiguredUiColor(uiColors, "inactiveButtonColor", out Color configuredInactiveButtonColor);
System.Windows.Application.Current.Resources["headerColor"] = new SolidColorBrush(headerColor);
ApplyBackgroundDerivedResources(backgroundColor);
// Active/Inactive button colors must come directly from config (WebAPI), not be overridden by derived background shades.
System.Windows.Application.Current.Resources["activeButtonColor"] = new SolidColorBrush(
hasConfiguredActiveColor ? configuredActiveButtonColor : ConverterHelper.ColorConvertFromString(Properties.Resources.activeButtonColor));
System.Windows.Application.Current.Resources["inactiveButtonColor"] = new SolidColorBrush(
hasConfiguredInactiveColor ? configuredInactiveButtonColor : ConverterHelper.ColorConvertFromString(Properties.Resources.inactiveButtonColor));
ApplyHeaderDerivedResources(headerColor);
LogEntry($"[Theme] activeButtonColor source={(hasConfiguredActiveColor ? "webapi" : "default")} value={((SolidColorBrush)System.Windows.Application.Current.Resources["activeButtonColor"]).Color}", LogLevels.Debug);
}
catch
{
cpConfig._uiColors["inactiveButtonColor"] = Properties.Resources.inactiveButtonColor;
cpConfig._uiColors["backgroundColor"] = Properties.Resources.backgroundColor;
cpConfig._uiColors["headerColor"] = Properties.Resources.headerColor;
cpConfig._uiColors["activeButtonColor"] = Properties.Resources.activeButtonColor;
Color fallbackBackground = (Color)ConverterHelper.ColorConvertFromString(cpConfig._uiColors["backgroundColor"]);
Color fallbackHeader = (Color)ConverterHelper.ColorConvertFromString(cpConfig._uiColors["headerColor"]);
Color fallbackBackground = ConverterHelper.ColorConvertFromString(Properties.Resources.backgroundColor);
Color fallbackHeader = ConverterHelper.ColorConvertFromString(Properties.Resources.headerColor);
Color fallbackActive = ConverterHelper.ColorConvertFromString(Properties.Resources.activeButtonColor);
Color fallbackInactive = ConverterHelper.ColorConvertFromString(Properties.Resources.inactiveButtonColor);
System.Windows.Application.Current.Resources["headerColor"] = new SolidColorBrush(fallbackHeader);
ApplyBackgroundDerivedResources(fallbackBackground);
System.Windows.Application.Current.Resources["activeButtonColor"] = new SolidColorBrush(fallbackActive);
System.Windows.Application.Current.Resources["inactiveButtonColor"] = new SolidColorBrush(fallbackInactive);
ApplyHeaderDerivedResources(fallbackHeader);
}
@@ -1349,6 +1384,11 @@ namespace C4IT_CustomerPanel.libs
cpConfig = JsonConvert.DeserializeObject<CustomerPanelConfig>(json);
cpConfig._encryptedApiToken = CustomerPanelSecurePassword.Instance.Decode(cpConfig._encryptedApiToken);
if (cpConfig?._uiColors != null && cpConfig._uiColors.TryGetValue("activeButtonColor", out string apiActiveColor) && !string.IsNullOrWhiteSpace(apiActiveColor))
LogEntry($"[Config API] activeButtonColor={apiActiveColor}", LogLevels.Debug);
else
LogEntry("[Config API] activeButtonColor missing or empty", LogLevels.Warning);
}
catch (Exception E)
{