stride3d/stride · error · ArgumentOutOfRangeException

There must be three and only three input values for Color3.

Error message

There must be three and only three input values for Color3.

What it means

The Color3(float[]) constructor requires the array to contain exactly three values (R, G, B). Passing null throws ArgumentNullException; passing an array of any other length throws ArgumentOutOfRangeException with this message. Note the XML doc incorrectly says 'four elements' — the code enforces three.

Solutions

  1. Pass an array of exactly 3 floats, or slice: new Color3(values[..3]).
  2. If the source is RGBA and alpha should be dropped, build Color3(values[0], values[1], values[2]).
  3. Validate values.Length == 3 before constructing.

Example fix

// before
var color = new Color3(rgbaArray); // rgbaArray.Length == 4
// after
var color = new Color3(rgbaArray[0], rgbaArray[1], rgbaArray[2]);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try { var c = new Color3(values); }
catch (ArgumentOutOfRangeException) { /* handle wrong-size input, e.g. slice or default */ }

Prevention

When it happens

Trigger: new Color3(new float[]{0.1f, 0.2f}) or new Color3(rgbaFloatArrayOf4) — any float[] whose Length != 3.

Common situations: Reusing an RGBA buffer (4 floats) from Color4/Color code paths; passing an array built for another math type (Vector4, Color4); parsing config where an extra alpha value was included.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Color3.cs:131

    /// The alpha component is ignored.</param>
    public Color3(uint rgb)
    {
        B = ((rgb >> 16) & 255) / 255.0f;
        G = ((rgb >> 8) & 255) / 255.0f;
        R = (rgb & 255) / 255.0f;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Color3"/> struct.
    /// </summary>
    /// <param name="values">The values to assign to the red, green, and blue components of the color. This must be an array with three 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 Color3(float[] values)
    {
        ArgumentNullException.ThrowIfNull(values);
        if (values.Length != 3)
            throw new ArgumentOutOfRangeException(nameof(values), "There must be three and only three input values for Color3.");

        R = values[0];
        G = values[1];
        B = values[2];
    }

    /// <summary>
    /// Gets or sets the component at the specified index.
    /// </summary>
    /// <value>The value of the red, green, or blue component, depending on the index.</value>
    /// <param name="index">The index of the component to access. Use 0 for the red component, 1 for the green component, and 2 for the blue component.</param>
    /// <returns>The value of the component at the specified index.</returns>
    /// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 2].</exception>
    public float this[int index]
    {
        readonly get
        {
            return index switch

View on GitHub (pinned to 96fad776d2)