SubtitleEdit/subtitleedit · error · ArgumentException

Invalid hex string.

Error message

Invalid hex string.

What it means

ArgumentException from FromHex when the input is null, empty, or whitespace-only. The hex parser requires a non-empty string before it can strip '#' and parse channels.

Source

Thrown at src/ui/Logic/SkColorExtensions.cs:33

    {
        return includeAlpha
            ? $"#{color.Alpha:X2}{color.Red:X2}{color.Green:X2}{color.Blue:X2}" // ARGB
            : $"#{color.Red:X2}{color.Green:X2}{color.Blue:X2}";               // RGB
    }

    public static Color ToAvaloniaColor(this SKColor color)
    {
        return new Color(color.Alpha, color.Red, color.Green, color.Blue);
    }

    /// <summary>
    /// Converts a hex string (e.g., "#RRGGBB" or "#AARRGGBB") to an SKColor.
    /// </summary>
    public static SKColor FromHex(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. Seed the color setting with a sensible default hex at the configuration source.
  2. Guard callers with !string.IsNullOrWhiteSpace(hex) before calling FromHex.
  3. Treat a missing value as a known default (e.g. transparent or white) rather than propagating the exception.

Example fix

// before
var color = setting.FromHex();

// after
var color = string.IsNullOrWhiteSpace(setting) ? SKColors.White : setting.FromHex();
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(setting)) setting = "#FFFFFFFF";
var color = setting.FromHex();

Try / catch

SKColor color;
try { color = setting.FromHex(); }
catch (ArgumentException) { color = SKColors.White; logger.LogWarning("Empty color value, using default"); }

Prevention

When it happens

Trigger: A color setting was never assigned (null), an empty config value, or whitespace-only input passed to FromHex.

Common situations: Default config before the user picks a color, a config migration that dropped a value, or a theme with a missing color key.

Related errors


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