SixLabors/ImageSharp · error · ArgumentException

Hexadecimal string is not in the correct format.

Error message

Hexadecimal string is not in the correct format.

What it means

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.

Solutions

  1. Fix the hex string to a valid form: #RGB, #RGBA, #RRGGBB, or #AARRGGBB (format depends on the ColorHexFormat argument)
  2. Pre-validate with Color.TryParseHex(hex, out Color c, format) before calling ParseHex
  3. For non-hex inputs (named colors) use Color.Parse instead
  4. Trim whitespace and ensure the '#' prefix where required

Example fix

// before
var color = Color.ParseHex(userInput); // e.g. "#12345" (5 digits)
// after
if (!Color.TryParseHex(userInput, out var color)) { /* handle invalid input */ }
Defensive patterns

Strategy: validation

Validate before calling

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})$"))
    throw new ArgumentException($"Invalid hex color: {hex}");

Try / catch

try { var c = Color.ParseHex(hex); }
catch (ArgumentException ex) { logger.LogError(ex, "Bad hex color: {Hex}", hex); }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13). Data as JSON: /api/errors/6fa82dc701f3def0. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/Color/Color.cs:235

    /// <param name="hex">
    /// The hexadecimal representation of the combined color components.
    /// </param>
    /// <param name="format">
    /// The format of the hexadecimal string to parse, if applicable. Defaults to <see cref="ColorHexFormat.Rgba"/>.
    /// </param>
    /// <returns>
    /// The <see cref="Color"/> equivalent of the hexadecimal input.
    /// </returns>
    /// <exception cref="ArgumentException">
    /// Thrown when the <paramref name="hex"/> is not in the correct format.
    /// </exception>
    public static Color ParseHex(string hex, ColorHexFormat format = ColorHexFormat.Rgba)
    {
        Guard.NotNull(hex, nameof(hex));

        if (!TryParseHex(hex, out Color color, format))
        {
            throw new ArgumentException("Hexadecimal string is not in the correct format.", nameof(hex));
        }

        return color;
    }

    /// <summary>
    /// Gets a <see cref="Color"/> from the given hexadecimal string.
    /// </summary>
    /// <param name="hex">
    /// The hexadecimal representation of the combined color components.
    /// </param>
    /// <param name="result">
    /// When this method returns, contains the <see cref="Color"/> equivalent of the hexadecimal input.
    /// </param>
    /// <param name="format">
    /// The format of the hexadecimal string to parse, if applicable. Defaults to <see cref="ColorHexFormat.Rgba"/>.
    /// </param>
    /// <returns>

View on GitHub (pinned to 59ce6af6fc)