Add color conversion methods with defaults and refactor ColorPicker parsing for web plugins

This commit is contained in:
Stone_Red
2026-06-27 19:08:36 +02:00
parent 4999ae6e88
commit d43f1ed949
2 changed files with 26 additions and 31 deletions
@@ -89,11 +89,35 @@ internal static partial class MultiColorConverter
return System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B);
}
public static System.Windows.Media.Color ConvertToMediaColor(string hex, System.Windows.Media.Color defaultColor)
{
if (TryConvertToMediaColor(hex, out System.Windows.Media.Color color))
{
return color;
}
else
{
return defaultColor;
}
}
public static System.Drawing.Color ConvertToSystemColor(System.Windows.Media.Color color)
{
return System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);
}
public static System.Drawing.Color ConvertToSystemColor(string hex, System.Drawing.Color defaultColor)
{
if (TryConvertToSystemColor(hex, out System.Drawing.Color color))
{
return color;
}
else
{
return defaultColor;
}
}
[GeneratedRegex("(?:[0-9a-fA-F]{8})")]
private static partial Regex Hex8();
@@ -554,7 +554,7 @@ public partial class WebPluginWindow : Window, IPluginWindow
Slider sl => sl.Value,
IntegerUpDown iud => iud.Value,
ComboBox cb => cb.Value,
ColorPicker cp => $"#{cp.Value.A:X2}{cp.Value.R:X2}{cp.Value.G:X2}{cp.Value.B:X2}",
ColorPicker cp => MultiColorConverter.ConvertToHexRgba(cp.Value),
Button btn => btn.Value,
Label lbl => lbl.Value,
FileSelector fs => fs.Value,
@@ -584,7 +584,7 @@ public partial class WebPluginWindow : Window, IPluginWindow
"colorpicker" => new ColorPicker(
element.TryGetProperty("default", out JsonElement cpDefault)
? ParseHexColor(cpDefault.GetString() ?? "#FFFFFFFF")
? MultiColorConverter.ConvertToSystemColor(cpDefault.GetString() ?? "#FFFFFFFF", System.Drawing.Color.White)
: System.Drawing.Color.White),
"button" => new Button(element.TryGetProperty("default", out JsonElement btnDefault) ? btnDefault.GetString() ?? "" : ""),
@@ -627,33 +627,4 @@ public partial class WebPluginWindow : Window, IPluginWindow
return comboBox;
}
private static System.Drawing.Color ParseHexColor(string hex)
{
if (string.IsNullOrEmpty(hex))
{
return System.Drawing.Color.White;
}
if (hex.StartsWith('#'))
{
hex = hex[1..];
}
if (hex.Length == 6)
{
hex = "FF" + hex;
}
if (hex.Length == 8 &&
byte.TryParse(hex[..2], System.Globalization.NumberStyles.HexNumber, null, out byte a) &&
byte.TryParse(hex[2..4], System.Globalization.NumberStyles.HexNumber, null, out byte r) &&
byte.TryParse(hex[4..6], System.Globalization.NumberStyles.HexNumber, null, out byte g) &&
byte.TryParse(hex[6..8], System.Globalization.NumberStyles.HexNumber, null, out byte b))
{
return System.Drawing.Color.FromArgb(a, r, g, b);
}
return System.Drawing.Color.White;
}
}