stride3d/stride · error · ArgumentOutOfRangeException

There must be 3 or 4 float[] values for Color4.

Error message

There must be 3 or 4 float[] values for Color4.

What it means

The Color4(float[]) constructor accepts arrays of exactly 3 or 4 floats: RGB (alpha defaults to 1f) or RGBA. Any other length throws ArgumentOutOfRangeException. The guard keeps ambiguous or truncated input from silently producing a wrong color.

Solutions

  1. Ensure the array has exactly 3 or 4 elements before constructing.
  2. If the source array is larger, slice the first 3-4 elements: new Color4(values[..4]).
  3. If it is smaller, build via the scalar constructor new Color4(r, g, b) or new Color4(r, g, b, a).

Example fix

// before
var color = new Color4(shortArray); // shortArray.Length == 2
// after
var color = new Color4(shortArray[0], shortArray[1], 0f);
Defensive patterns

Strategy: validation

Validate before calling

if (values is { Length: 3 } or { Length: 4 })
    var c = new Color4(values);
else
    throw new ArgumentException("Color4 requires 3 or 4 floats", nameof(values));

Type guard

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

Try / catch

try { var c = new Color4(values); }
catch (ArgumentOutOfRangeException) { /* construct from scalars or pad array */ }

Prevention

When it happens

Trigger: new Color4(floatArray) where floatArray.Length is 0, 1, 2, 5 or more; passing an empty array or a padded buffer.

Common situations: Passing a whole vertex buffer slice with stride padding; parsing hex/config values into wrong-sized arrays; reusing buffers sized for Vector2/Vector3 code paths (2 elements).

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Color4.cs:169

    public Color4(int rgba)
    {
        A = ((rgba >> 24) & 255) / 255.0f;
        B = ((rgba >> 16) & 255) / 255.0f;
        G = ((rgba >> 8) & 255) / 255.0f;
        R = (rgba & 255) / 255.0f;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Color4"/> struct.
    /// </summary>
    /// <param name="values">The values to assign to the red, green, blue, and 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 Color4(float[] values)
    {
        ArgumentNullException.ThrowIfNull(values);
        if (values.Length is not 3 and not 4)
            throw new ArgumentOutOfRangeException(nameof(values), "There must be 3 or 4 float[] values for Color4.");

        R = values[0];
        G = values[1];
        B = values[2];
        A = values.Length >= 4 ? values[3] : 1f;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Color4"/> struct.
    /// </summary>
    /// <param name="color"><see cref="Color3"/> used to initialize the color.</param>
    public Color4(Color3 color)
    {
        R = color.R;
        G = color.G;
        B = color.B;
        A = 1.0f;
    }

View on GitHub (pinned to 96fad776d2)