{"record":{"id":"bf2cca096f5302b0","repo":"SixLabors/ImageSharp","slug":"unsupported-color-hex-format","errorCode":null,"errorMessage":"Unsupported color hex format.","messagePattern":"Unsupported color hex format\\.","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Color/Color.cs","lineNumber":375,"sourceCode":"\n    /// <summary>\n    /// Gets the hexadecimal string representation of the color instance.\n    /// </summary>\n    /// <param name=\"format\">\n    /// The format of the hexadecimal string to return. Defaults to <see cref=\"ColorHexFormat.Rgba\"/>.\n    /// </param>\n    /// <returns>A hexadecimal string representation of the value.</returns>\n    /// <exception cref=\"ArgumentOutOfRangeException\">Thrown when the <paramref name=\"format\"/> is not supported.</exception>\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public string ToHex(ColorHexFormat format = ColorHexFormat.Rgba)\n    {\n        Rgba32 rgba = this.ToPixel<Rgba32>();\n\n        uint hexOrder = format switch\n        {\n            ColorHexFormat.Argb => (uint)((rgba.B << 0) | (rgba.G << 8) | (rgba.R << 16) | (rgba.A << 24)),\n            ColorHexFormat.Rgba => (uint)((rgba.A << 0) | (rgba.B << 8) | (rgba.G << 16) | (rgba.R << 24)),\n            _ => throw new ArgumentOutOfRangeException(nameof(format), format, \"Unsupported color hex format.\")\n        };\n\n        return hexOrder.ToString(\"X8\", CultureInfo.InvariantCulture);\n    }\n\n    /// <inheritdoc />\n    public override string ToString() => this.ToHex(ColorHexFormat.Rgba);\n\n    /// <summary>\n    /// Converts the color instance to a specified <typeparamref name=\"TPixel\"/> type.\n    /// </summary>\n    /// <typeparam name=\"TPixel\">The pixel type to convert to.</typeparam>\n    /// <returns>The <typeparamref name=\"TPixel\"/>.</returns>\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public TPixel ToPixel<TPixel>()\n        where TPixel : unmanaged, IPixel<TPixel>\n    {\n        if (this.boxedHighPrecisionPixel is TPixel pixel)","sourceCodeStart":357,"sourceCodeEnd":393,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Color/Color.cs#L357-L393","documentation":"Color.ToHex validates the requested ColorHexFormat enum value; only Argb and Rgba are supported, and any other value (invalid cast, uninitialized enum, out-of-range int) hits the switch's discard arm and throws ArgumentOutOfRangeException with 'Unsupported color hex format.'","triggerScenarios":"Calling color.ToHex(format) with a ColorHexFormat value outside the defined members — typically from an unchecked (ColorHexFormat)cast of an int, a deserialized enum value, or default(ColorHexFormat) if the enum's zero value is not a valid member.","commonSituations":"Enum values round-tripped through config/JSON/DB as ints; API version changes adding/removing members; callers computing format values arithmetically.","solutions":["Only pass ColorHexFormat.Rgba or ColorHexFormat.Argb","Validate untrusted enum values with Enum.IsDefined(typeof(ColorHexFormat), value) before calling ToHex","Fix deserialization that produced an undefined enum value"],"exampleFix":"// before\nvar hex = color.ToHex((ColorHexFormat)storedInt);\n// after\nvar format = Enum.IsDefined(typeof(ColorHexFormat), storedInt) ? (ColorHexFormat)storedInt : ColorHexFormat.Rgba;\nvar hex = color.ToHex(format);","handlingStrategy":"try-catch","validationCode":"if (format is not (ColorHexFormat.Rgba or ColorHexFormat.Argb)) format = ColorHexFormat.Rgba;","typeGuard":"bool IsKnownFormat(ColorHexFormat f) => Enum.IsDefined(f);","tryCatchPattern":"try { return color.ToHex(format); } catch (ArgumentOutOfRangeException) { return color.ToHex(ColorHexFormat.Rgba); }","preventionTips":["Validate enum inputs from external sources with Enum.IsDefined","Avoid int-to-enum casts when reading persisted values"],"tags":["color","enum","argument-out-of-range","hex"],"backgroundTag":"invalid-enum-value","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"}