stride3d/stride · error · ArgumentOutOfRangeException

There must be four and only four input values for ColorBGRA.

Error message

There must be four and only four input values for ColorBGRA.

What it means

The ColorBGRA(float[]) constructor requires exactly four floats in BGRA order (each converted from float to byte). Arrays of any other length throw ArgumentOutOfRangeException, since a BGRA color is defined by exactly four channels.

Solutions

  1. Supply exactly 4 floats in B, G, R, A order.
  2. If you only have RGB, append alpha: new ColorBGRA(r, g, b, 1f) style or new[] {b, g, r, 1f}.
  3. Check array length == 4 before constructing.

Example fix

// before
var c = new ColorBGRA(rgbFloats); // Length == 3
// after
var c = new ColorBGRA(new[] { rgbFloats[0], rgbFloats[1], rgbFloats[2], 1f });
Defensive patterns

Strategy: validation

Validate before calling

if (values is { Length: 4 })
    var c = new ColorBGRA(values);
else
    throw new ArgumentException("ColorBGRA requires exactly 4 floats (B,G,R,A)", nameof(values));

Type guard

bool IsBgraQuad(float[] v) => v is { Length: 4 };

Try / catch

try { var c = new ColorBGRA(values); }
catch (ArgumentOutOfRangeException) { /* pad RGB to BGRA or reject input */ }

Prevention

When it happens

Trigger: new ColorBGRA(floatArray) where floatArray.Length != 4 — e.g. 3-element RGB float arrays or longer buffers.

Common situations: Passing RGB (no alpha) float data from shaders or image decoders; reusing buffers from Color3 code; channel-order confusion (RGBA vs BGRA) that also changes expected array size in adjacent code.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/a07ff8153aa1c32b. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/ColorBGRA.cs:151

    public ColorBGRA(int bgra)
    {
        A = (byte)((bgra >> 24) & 255);
        R = (byte)((bgra >> 16) & 255);
        G = (byte)((bgra >> 8) & 255);
        B = (byte)(bgra & 255);
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="ColorBGRA"/> struct.
    /// </summary>
    /// <param name="values">The values to assign to the red, green, and blue, alpha components of the color. This must be an array with four elements.</param>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
    /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than four elements.</exception>
    public ColorBGRA(float[] values)
    {
        ArgumentNullException.ThrowIfNull(values);
        if (values.Length != 4)
            throw new ArgumentOutOfRangeException(nameof(values), "There must be four and only four input values for ColorBGRA.");

        B = ToByte(values[0]);
        G = ToByte(values[1]);
        R = ToByte(values[2]);
        A = ToByte(values[3]);
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="ColorBGRA"/> struct.
    /// </summary>
    /// <param name="values">The values to assign to the red, green, and blue, alpha components of the color. This must be an array with four elements.</param>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
    /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than four elements.</exception>
    public ColorBGRA(byte[] values)
    {
        ArgumentNullException.ThrowIfNull(values);
        if (values.Length != 4)
            throw new ArgumentOutOfRangeException(nameof(values), "There must be four and only four input values for ColorBGRA.");

View on GitHub (pinned to 96fad776d2)