SubtitleEdit/subtitleedit · error · ArgumentException
Invalid hex string.
Error message
Invalid hex string.
What it means
Thrown by AvaloniaColorExtensions.FromHexToColor when the input string is null, empty, or whitespace. This is the first guard before TrimStart('#') and length parsing, so a missing/blank color value aborts with an ArgumentException rather than producing a misleading parse error downstream.
Source
Thrown at src/ui/Logic/AvaloniaColorExtensions.cs:27
/// <summary>
/// Converts an Color to a hex string. Default is ARGB (#AARRGGBB).
/// Set includeAlpha to false for RGB (#RRGGBB).
/// </summary>
public static string FromColorToHex(this Avalonia.Media.Color color, bool includeAlpha = true)
{
return includeAlpha
? $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}" // ARGB
: $"#{color.R:X2}{color.G:X2}{color.B:X2}"; // RGB
}
/// <summary>
/// Converts a hex string (e.g., "#RRGGBB" or "#AARRGGBB") to a Color.
/// </summary>
public static Avalonia.Media.Color FromHexToColor(this string hex)
{
if (string.IsNullOrWhiteSpace(hex))
{
throw new ArgumentException("Invalid hex string.");
}
hex = hex.TrimStart('#');
byte a = 255, r, g, b;
if (hex.Length == 6)
{
// Format: RRGGBB
r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);
g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);
b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);
}
else if (hex.Length == 8)
{
// Format: AARRGGBB
a = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);
r = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);View on GitHub (pinned to 17a9f07487)
Solutions
- Coalesce null/blank to a default color before calling FromHexToColor.
- Ensure settings/theme files always serialize a hex value for color keys.
- Validate user-entered hex input is non-empty before parsing.
Example fix
// before var color = userHex.FromHexToColor(); // after - default to a safe color when blank var color = (string.IsNullOrWhiteSpace(userHex) ? "#FFFFFFFF" : userHex).FromHexToColor();
Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(hex)) hex = "#FFFFFFFF"; // or throw with context
// or guard at the call site:
if (string.IsNullOrWhiteSpace(userHex)) { /* use default color */ return defaultColor; } Type guard
static bool IsHexColor(this string? s) => !string.IsNullOrWhiteSpace(s);
Prevention
- Always serialize a default hex value for color settings.
- Coalesce null/blank to a default before parsing.
- Validate user input at the UI layer, not in the converter.
When it happens
Trigger: Passing null/""/" " to FromHexToColor: an unset color setting serialized as null, a missing theme/style attribute, or a defaulted config value that was never populated.
Common situations: A color settings field defaulted to null after a settings reset; deserializing an old config missing a newly-added color key; UI binding a null color value into a hex converter.
Related errors
- Hex string must be 6 (RRGGBB) or 8 (AARRGGBB) characters lon
- Color '{colorName}' not found in SKColors.
- Invalid hex string.
- Hex string must be 6 (RRGGBB) or 8 (AARRGGBB) characters lon
- Too many bytes for CCData!
AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13).
Data as JSON: /api/errors/36d98792458ff326.
Report an issue: GitHub.