SubtitleEdit/subtitleedit · error · FormatException

Hex string must be 6 (RRGGBB) or 8 (AARRGGBB) characters lon

Error message

Hex string must be 6 (RRGGBB) or 8 (AARRGGBB) characters long.

What it means

Thrown by AvaloniaColorExtensions.FromHexToColor after stripping the leading '#': the remaining string is neither 6 (RRGGBB) nor 8 (AARRGGBB) hex characters. This FormatException is the length guard; a correct length with non-hex characters would instead fail inside byte.Parse as a separate FormatException.

Source

Thrown at src/ui/Logic/AvaloniaColorExtensions.cs:51

        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);
            g = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);
            b = byte.Parse(hex.Substring(6, 2), NumberStyles.HexNumber);
        }
        else
        {
            throw new FormatException("Hex string must be 6 (RRGGBB) or 8 (AARRGGBB) characters long.");
        }

        return new Avalonia.Media.Color(a, r, g, b);
    }

    /// <summary>
    /// Converts an Avalonia Color to an SKColor.
    /// </summary>
    public static SKColor ToSkColor(this Avalonia.Media.Color color)
    {
        return new SKColor(color.R, color.G, color.B, color.A);
    }
}

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Normalize the input first: trim, strip '#' and '0x', expand 3-digit shorthand, and reorder alpha-last to alpha-first if needed.
  2. Validate length is 6 or 8 before calling FromHexToColor.
  3. Correct the source config/theme value to #RRGGBB or #AARRGGBB.

Example fix

// before
var color = userHex.FromHexToColor();

// after - normalize common variants first
var hex = userHex.Trim().TrimStart('#').Replace("0x", "", StringComparison.OrdinalIgnoreCase);
if (hex.Length == 3) hex = string.Concat(hex.Select(c => new string(c, 2)));          // #fff -> #ffffff
if (hex.Length == 8 && !IsArgbFirst(hex)) hex = hex.Substring(6, 2) + hex.Substring(0, 6); // RRGGBBAA -> AARRGGBB
var color = hex.FromHexToColor();
Defensive patterns

Strategy: validation

Validate before calling

var h = userHex.Trim().TrimStart('#').Replace("0x", "", StringComparison.OrdinalIgnoreCase);
if (h.Length == 3) h = string.Concat(h.Select(c => new string(c, 2)));
if (h.Length != 6 && h.Length != 8) throw new FormatException("Hex must be 6 or 8 digits after normalization.");

Type guard

static bool IsValidHexColor(this string? s)
{
    if (string.IsNullOrWhiteSpace(s)) return false;
    var h = s.Trim().TrimStart('#');
    return (h.Length == 6 || h.Length == 8) && h.All(IsHexDigit);
    static bool IsHexDigit(char c) => (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}

Prevention

When it happens

Trigger: A hex value with the wrong number of digits: '#RGB' (3), '#RRGGBBAA' (wrong byte order/length), a value still containing '0x', a truncated value like '#FF', or stray whitespace inside the string.

Common situations: Hand-edited theme/config with a typo; a CSS-style 3-digit shorthand (#fff) which this parser does not support; an alpha-last value from another tool (#RRGGBBAA) fed into an alpha-first parser; copy-paste including quotes.

Related errors


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