{"record":{"id":"0901fbae34b2f477","repo":"SubtitleEdit/subtitleedit","slug":"hex-string-must-be-6-rrggbb-or-8-aarrggbb-char","errorCode":null,"errorMessage":"Hex string must be 6 (RRGGBB) or 8 (AARRGGBB) characters long.","messagePattern":"Hex string must be 6 \\(RRGGBB\\) or 8 \\(AARRGGBB\\) characters long\\.","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/AvaloniaColorExtensions.cs","lineNumber":51,"sourceCode":"\n        if (hex.Length == 6)\n        {\n            // Format: RRGGBB\n            r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);\n            g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);\n            b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);\n        }\n        else if (hex.Length == 8)\n        {\n            // Format: AARRGGBB\n            a = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);\n            r = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);\n            g = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);\n            b = byte.Parse(hex.Substring(6, 2), NumberStyles.HexNumber);\n        }\n        else\n        {\n            throw new FormatException(\"Hex string must be 6 (RRGGBB) or 8 (AARRGGBB) characters long.\");\n        }\n\n        return new Avalonia.Media.Color(a, r, g, b);\n    }\n\n    /// <summary>\n    /// Converts an Avalonia Color to an SKColor.\n    /// </summary>\n    public static SKColor ToSkColor(this Avalonia.Media.Color color)\n    {\n        return new SKColor(color.R, color.G, color.B, color.A);\n    }\n}","sourceCodeStart":33,"sourceCodeEnd":64,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/AvaloniaColorExtensions.cs#L33-L64","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Normalize the input first: trim, strip '#' and '0x', expand 3-digit shorthand, and reorder alpha-last to alpha-first if needed.","Validate length is 6 or 8 before calling FromHexToColor.","Correct the source config/theme value to #RRGGBB or #AARRGGBB."],"exampleFix":"// before\nvar color = userHex.FromHexToColor();\n\n// after - normalize common variants first\nvar hex = userHex.Trim().TrimStart('#').Replace(\"0x\", \"\", StringComparison.OrdinalIgnoreCase);\nif (hex.Length == 3) hex = string.Concat(hex.Select(c => new string(c, 2)));          // #fff -> #ffffff\nif (hex.Length == 8 && !IsArgbFirst(hex)) hex = hex.Substring(6, 2) + hex.Substring(0, 6); // RRGGBBAA -> AARRGGBB\nvar color = hex.FromHexToColor();","handlingStrategy":"validation","validationCode":"var h = userHex.Trim().TrimStart('#').Replace(\"0x\", \"\", StringComparison.OrdinalIgnoreCase);\nif (h.Length == 3) h = string.Concat(h.Select(c => new string(c, 2)));\nif (h.Length != 6 && h.Length != 8) throw new FormatException(\"Hex must be 6 or 8 digits after normalization.\");","typeGuard":"static bool IsValidHexColor(this string? s)\n{\n    if (string.IsNullOrWhiteSpace(s)) return false;\n    var h = s.Trim().TrimStart('#');\n    return (h.Length == 6 || h.Length == 8) && h.All(IsHexDigit);\n    static bool IsHexDigit(char c) => (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');\n}","tryCatchPattern":null,"preventionTips":["Normalize shorthand (#fff) and alpha-last (#RRGGBBAA) before parsing.","Validate length and hex digits upstream.","Standardize theme/config colors on #RRGGBB or #AARRGGBB."],"tags":["color","hex","validation","format"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}