{"record":{"id":"9ca5317f3881e632","repo":"SubtitleEdit/subtitleedit","slug":"hex-string-must-be-6-rrggbb-or-8-aarrggbb-char-9ca531","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":"validation","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/SkColorExtensions.cs","lineNumber":57,"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 SKColor(r, g, b, a);\n    }\n}","sourceCodeStart":39,"sourceCodeEnd":62,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/SkColorExtensions.cs#L39-L62","documentation":"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.","triggerScenarios":"'#FFF' (3-char shorthand), '#RRGGBBAA' (alpha last, but the parser expects AARRGGBB), odd-length typos, or ARGB-vs-RGBA ordering confusion.","commonSituations":"Pasting CSS hex into a setting that expects AARRGGBB, alpha-channel ordering mismatches, or loosely-validated user input.","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."],"exampleFix":"// before\nvar c = userInput.FromHex();\n\n// after\nstatic SKColor FromHexSafe(string hex)\n{\n    hex = (hex ?? \"\").Trim().TrimStart('#');\n    if (hex.Length == 3) hex = string.Concat(hex.Select(ch => new string(ch, 2)));\n    if (hex.Length == 6) hex = \"FF\" + hex; // opaque alpha first -> AARRGGBB\n    return hex.Length == 8 ? hex.FromHex() : SKColors.White;\n}","handlingStrategy":"validation","validationCode":"hex = (hex ?? \"\").Trim().TrimStart('#');\nif (hex.Length == 3) hex = string.Concat(hex.Select(c => new string(c, 2)));\nif (hex.Length == 6) hex = \"FF\" + hex;\nif (hex.Length != 8 || !hex.All(\"0123456789abcdefABCDEF\".Contains)) throw new FormatException(\"Bad hex\");","typeGuard":null,"tryCatchPattern":"SKColor color;\ntry { color = NormalizeHex(userInput).FromHex(); }\ncatch (FormatException) { color = SKColors.White; logger.LogWarning(\"Invalid hex '{Input}', using default\", userInput); }","preventionTips":["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."],"tags":["color","parsing","format","validation"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}