feat(util): add HexToRgba parser for theme JSON

This commit is contained in:
2026-05-05 10:21:46 +02:00
parent 289fe2eb78
commit 0b13efd0b5
+16
View File
@@ -62,4 +62,20 @@ internal static class ColourUtil {
return ((uint) a << 24) | ((uint) nb << 16) | ((uint) ng << 8) | nr; return ((uint) a << 24) | ((uint) nb << 16) | ((uint) ng << 8) | nr;
} }
public static uint HexToRgba(string hex)
{
ArgumentNullException.ThrowIfNull(hex);
var s = hex.StartsWith('#') ? hex[1..] : hex;
if (s.Length != 6 && s.Length != 8)
throw new FormatException($"Hex colour must be 6 or 8 hex digits, got {s.Length}: '{hex}'");
if (!uint.TryParse(s, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out var value))
throw new FormatException($"Hex colour '{hex}' is not a valid hexadecimal value");
if (s.Length == 6)
value = (value << 8) | 0xFFu; // RRGGBB → RRGGBBFF
return value;
}
} }