stride3d/stride · error · ArgumentOutOfRangeException

Indices for ColorBGRA run from 0 to 3, inclusive.

Error message

Indices for ColorBGRA run from 0 to 3, inclusive.

What it means

ColorBGRA's indexer getter only accepts indices 0-3, mapping to B, G, R, A channels in that order. The library throws ArgumentOutOfRangeException when any other index is passed. This guards against silently reading undefined channel data.

Solutions

  1. Fix the index value to be within 0-3 (remember: 0=B, 1=G, 2=R, 3=A).
  2. Check loop bounds: use 'for (int i = 0; i < 4; i++)' not '<= 4' or a wrong constant.
  3. Prefer named properties (B, G, R, A) over the indexer when the channel is known at compile time.

Example fix

// before
for (int i = 0; i <= 4; i++) Console.WriteLine(color[i]);
// after
for (int i = 0; i < 4; i++) Console.WriteLine(color[i]);
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0 || index > 3) throw new ArgumentOutOfRangeException(nameof(index));
var channel = color[index];

Type guard

bool IsValidColorIndex(int i) => i is >= 0 and <= 3;

Try / catch

try { var c = color[i]; }
catch (ArgumentOutOfRangeException ex) { /* fall back to named property or skip */ }

Prevention

When it happens

Trigger: Calling the ColorBGRA indexer getter with index < 0 or index > 3, e.g. color[4] or color[-1], typically from loops with wrong bounds or code assuming RGBA channel order.

Common situations: Looping over pixels/channels with an off-by-one bound; assuming 0=R from RGBA habits when ColorBGRA indexes 0=B; copying channel-extraction code written for a 4-component type with different index semantics.

Related errors


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

Appendix: source

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

    /// <summary>
    /// Gets or sets the component at the specified index.
    /// </summary>
    /// <value>The value of the alpha, red, green, or blue component, depending on the index.</value>
    /// <param name="index">The index of the component to access. Use 0 for the alpha component, 1 for the red component, 2 for the green component, and 3 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, 3].</exception>
    public byte this[int index]
    {
        readonly get
        {
            return index switch
            {
                0 => B,
                1 => G,
                2 => R,
                3 => A,
                _ => throw new ArgumentOutOfRangeException(nameof(index), "Indices for ColorBGRA run from 0 to 3, inclusive."),
            };
        }

        set
        {
            switch (index)
            {
                case 0: B = value; break;
                case 1: G = value; break;
                case 2: R = value; break;
                case 3: A = value; break;
                default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for ColorBGRA run from 0 to 3, inclusive.");
            }
        }
    }

    /// <summary>
    /// Converts the color into a packed integer.

View on GitHub (pinned to 96fad776d2)