stride3d/stride · error · ArgumentOutOfRangeException

Indices for Color3 run from 0 to 2, inclusive.

Error message

Indices for Color3 run from 0 to 2, inclusive.

What it means

Color3's indexer getter accepts only indices 0-2, mapping to R, G, B. Since Color3 has no alpha component, any other index has nothing to return and ArgumentOutOfRangeException is thrown. Guard against treating a Color3 like a 4-channel Color.

Solutions

  1. Limit iteration to 0..2 for Color3.
  2. If you need 4 channels, use Color or Color4 instead of Color3.
  3. Read R/G/B fields directly instead of the indexer when the component is known.

Example fix

// before
for (int i = 0; i < 4; i++)
    sum += color3[i];
// after
for (int i = 0; i < 3; i++)
    sum += color3[i];
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

bool IsValidColor3Index(int i) => i is >= 0 and <= 2;

Try / catch

try { var c = color3[index]; }
catch (ArgumentOutOfRangeException) { /* use a 3-channel loop bound */ }

Prevention

When it happens

Trigger: Reading color3[index] with index < 0 or index > 2, typically a loop running to 4 because the code was written for Color/Color4.

Common situations: Shared per-pixel loops that use a hardcoded channel count of 4; generic component-copy utilities parameterized for 4 channels; porting code from Color to Color3 without updating bounds.

Related errors


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

Appendix: source

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

    }

    /// <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
            {
                0 => R,
                1 => G,
                2 => B,
                _ => throw new ArgumentOutOfRangeException(nameof(index), "Indices for Color3 run from 0 to 2, inclusive."),
            };
        }

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

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

View on GitHub (pinned to 96fad776d2)