stride3d/stride · error · ArgumentOutOfRangeException

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

Error message

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

What it means

The Color(float[]) constructor converts an RGBA float array into byte color channels and requires exactly four elements. A null array yields ArgumentNullException; any other length yields this ArgumentOutOfRangeException.

Solutions

  1. Ensure the array has exactly 4 elements (R, G, B, A) before constructing.
  2. For RGB-only data, append an alpha of 1f: new[]{ r, g, b, 1f }.
  3. Use the Color(byte r, byte g, byte b, byte a) or Color(float r, float g, float b, float a) constructor instead of an array.
  4. Validate array length and throw a clearer domain error at the parsing boundary.

Example fix

// before
var color = new Color(rgbFloats); // rgbFloats.Length == 3 -> throws
// after
var rgba = rgbFloats.Length == 3 ? new[] { rgbFloats[0], rgbFloats[1], rgbFloats[2], 1f } : rgbFloats;
var color = new Color(rgba);
Defensive patterns

Strategy: validation

Validate before calling

if (values is not { Length: 4 })
    throw new ArgumentException("Color requires exactly 4 RGBA float components.", nameof(values));

Type guard

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

Try / catch

try { var c = new Color(values); }
catch (ArgumentOutOfRangeException) { var c = Color.White; /* or log & rethrow with context */ }

Prevention

When it happens

Trigger: Calling new Color(floatArray) where floatArray.Length is 3 (RGB-only), 0, or more than 4 — e.g. passing parsed color data without an alpha channel.

Common situations: Importing colors from formats that omit alpha (hex #RRGGBB expanded to 3 floats); JSON/CSV parsed arrays with variable length; hand-written arrays missing the alpha component.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Color.cs:189

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

    /// <summary>
    /// Initializes a new instance of the <see cref="Color"/> 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 Color(float[] values)
    {
        ArgumentNullException.ThrowIfNull(values);
        if (values.Length != 4)
            throw new ArgumentOutOfRangeException(nameof(values), "There must be four and only four input values for Color.");

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

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

View on GitHub (pinned to 96fad776d2)