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

  1. Coalesce null/blank to a default color before calling FromHexToColor.
  2. Ensure settings/theme files always serialize a hex value for color keys.
  3. 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

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


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/36d98792458ff326. Report an issue: GitHub.