{"record":{"id":"6fa82dc701f3def0","repo":"SixLabors/ImageSharp","slug":"hexadecimal-string-is-not-in-the-correct-format","errorCode":null,"errorMessage":"Hexadecimal string is not in the correct format.","messagePattern":"Hexadecimal string is not in the correct format\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Color/Color.cs","lineNumber":235,"sourceCode":"    /// <param name=\"hex\">\n    /// The hexadecimal representation of the combined color components.\n    /// </param>\n    /// <param name=\"format\">\n    /// The format of the hexadecimal string to parse, if applicable. Defaults to <see cref=\"ColorHexFormat.Rgba\"/>.\n    /// </param>\n    /// <returns>\n    /// The <see cref=\"Color\"/> equivalent of the hexadecimal input.\n    /// </returns>\n    /// <exception cref=\"ArgumentException\">\n    /// Thrown when the <paramref name=\"hex\"/> is not in the correct format.\n    /// </exception>\n    public static Color ParseHex(string hex, ColorHexFormat format = ColorHexFormat.Rgba)\n    {\n        Guard.NotNull(hex, nameof(hex));\n\n        if (!TryParseHex(hex, out Color color, format))\n        {\n            throw new ArgumentException(\"Hexadecimal string is not in the correct format.\", nameof(hex));\n        }\n\n        return color;\n    }\n\n    /// <summary>\n    /// Gets a <see cref=\"Color\"/> from the given hexadecimal string.\n    /// </summary>\n    /// <param name=\"hex\">\n    /// The hexadecimal representation of the combined color components.\n    /// </param>\n    /// <param name=\"result\">\n    /// When this method returns, contains the <see cref=\"Color\"/> equivalent of the hexadecimal input.\n    /// </param>\n    /// <param name=\"format\">\n    /// The format of the hexadecimal string to parse, if applicable. Defaults to <see cref=\"ColorHexFormat.Rgba\"/>.\n    /// </param>\n    /// <returns>","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Color/Color.cs#L217-L253","documentation":"Color.ParseHex validates the string via TryParseHex and throws ArgumentException naming the 'hex' parameter when the string is not a valid hexadecimal color (e.g. #RGB, #RGBA, #RRGGBB, #AARRGGBB per the requested ColorHexFormat). Werner palette generation also funnels through this method, so a bad entry in a palette fails with this message.","triggerScenarios":"Calling Color.ParseHex(\"GGHHII\") or a string with wrong length/wrong digits; parsing hex strings from user input, CSS-ish values with unsupported syntax (e.g. rgb() strings), or palette files containing malformed entries (caught by CreateWernerPalette).","commonSituations":"Importing color lists from external files with typos; passing named colors like 'red' to ParseHex instead of Color.Parse; extra whitespace, missing '#', or 3/6/8 digit length violations.","solutions":["Fix the hex string to a valid form: #RGB, #RGBA, #RRGGBB, or #AARRGGBB (format depends on the ColorHexFormat argument)","Pre-validate with Color.TryParseHex(hex, out Color c, format) before calling ParseHex","For non-hex inputs (named colors) use Color.Parse instead","Trim whitespace and ensure the '#' prefix where required"],"exampleFix":"// before\nvar color = Color.ParseHex(userInput); // e.g. \"#12345\" (5 digits)\n// after\nif (!Color.TryParseHex(userInput, out var color)) { /* handle invalid input */ }","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(hex) || !System.Text.RegularExpressions.Regex.IsMatch(hex.Trim(), \"^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$\"))\n    throw new ArgumentException($\"Invalid hex color: {hex}\");","typeGuard":null,"tryCatchPattern":"try { var c = Color.ParseHex(hex); }\ncatch (ArgumentException ex) { logger.LogError(ex, \"Bad hex color: {Hex}\", hex); }","preventionTips":["Always prefer TryParseHex over ParseHex for untrusted input","Validate palette/color files at load time, entry by entry","Remember ParseHex accepts only hex forms — use Color.Parse for named colors"],"tags":["color","hex-parsing","invalid-argument-format"],"backgroundTag":"invalid-argument-format","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}