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
FormatException from FromHex: after stripping '#', the string length is neither 6 (RRGGBB) nor 8 (AARRGGBB). CSS-style 3-char shorthand (#FFF) and 4-char forms are rejected, and the 8-char form requires alpha FIRST (AARRGGBB), not last.
Source
Thrown at src/ui/Logic/SkColorExtensions.cs:57
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 SKColor(r, g, b, a);
}
}View on GitHub (pinned to 17a9f07487)
Solutions
- Expand 3-char shorthand to 6 and 4-char to 8, reordering alpha to the front if needed.
- Validate the length is 6 or 8 (after '#') before parsing.
- Document the expected AARRGGBB ordering at the UI/config boundary.
Example fix
// before
var c = userInput.FromHex();
// after
static SKColor FromHexSafe(string hex)
{
hex = (hex ?? "").Trim().TrimStart('#');
if (hex.Length == 3) hex = string.Concat(hex.Select(ch => new string(ch, 2)));
if (hex.Length == 6) hex = "FF" + hex; // opaque alpha first -> AARRGGBB
return hex.Length == 8 ? hex.FromHex() : SKColors.White;
} Defensive patterns
Strategy: validation
Validate before calling
hex = (hex ?? "").Trim().TrimStart('#');
if (hex.Length == 3) hex = string.Concat(hex.Select(c => new string(c, 2)));
if (hex.Length == 6) hex = "FF" + hex;
if (hex.Length != 8 || !hex.All("0123456789abcdefABCDEF".Contains)) throw new FormatException("Bad hex"); Try / catch
SKColor color;
try { color = NormalizeHex(userInput).FromHex(); }
catch (FormatException) { color = SKColors.White; logger.LogWarning("Invalid hex '{Input}', using default", userInput); } Prevention
- Document the expected AARRGGBB ordering at the UI boundary.
- Expand 3/4-char shorthand before parsing.
- Validate length (6 or 8 after '#') before calling FromHex.
- Normalize alpha ordering for RGBA-style inputs.
When it happens
Trigger: '#FFF' (3-char shorthand), '#RRGGBBAA' (alpha last, but the parser expects AARRGGBB), odd-length typos, or ARGB-vs-RGBA ordering confusion.
Common situations: Pasting CSS hex into a setting that expects AARRGGBB, alpha-channel ordering mismatches, or loosely-validated user input.
Related errors
- Hex string must be 6 (RRGGBB) or 8 (AARRGGBB) characters lon
- Invalid hex string.
- Color '{colorName}' not found in SKColors.
- Invalid hex string.
- Invalid version format
AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13).
Data as JSON: /api/errors/9ca5317f3881e632.
Report an issue: GitHub.