stride3d/stride · error · ArgumentOutOfRangeException
Indices for Color4 run from 0 to 3, inclusive.
Error message
Indices for Color4 run from 0 to 3, inclusive.
What it means
The Color4 indexer performs a bounds check on the index parameter and throws ArgumentOutOfRangeException whenever the index is outside [0, 3]. It is a generic guard on component access: any subscript other than 0 (red), 1 (green), 2 (blue) or 3 (alpha) fires it — Color4 has exactly four components, so negative or >= 4 indices are invalid.
Solutions
- Clamp or check the index to 0..3 before access.
- Iterate with an explicit bound of 4 for Color4.
- Access R/G/B/A fields directly for known components.
Example fix
// before
for (int i = 0; i <= 4; i++)
sum += color4[i];
// after
for (int i = 0; i < 4; i++)
sum += color4[i]; Defensive patterns
Strategy: validation
Validate before calling
if (index < 0 || index > 3) throw new ArgumentOutOfRangeException(nameof(index)); var channel = color4[index];
Type guard
bool IsValidColor4Index(int i) => i is >= 0 and <= 3;
Try / catch
try { var c = color4[index]; }
catch (ArgumentOutOfRangeException) { /* clamp index and retry or default */ } Prevention
- Use < 4 (not <= 4) as loop bound for Color4 channels.
- Define channel-count constants alongside each color type.
- Prefer named field access to avoid index math entirely.
When it happens
Trigger: Reading color4[index] with index < 0 or index > 3, e.g. a loop bound copied from a larger vector type or a computed index going negative.
Common situations: Generic per-channel loops with wrong upper bounds; math utilities shared with Vector4-like types where a fifth slot was expected; off-by-one in alpha handling.
Related errors
- Indices for Color run from 0 to 3, inclusive.
- Indices for Color3 run from 0 to 2, inclusive.
- There must be three and only three input values for Color3.
- There must be 3 or 4 float[] values for Color4.
- There must be four and only four input values for ColorBGRA.
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/1c425893ee5a1db8.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core.Mathematics/Color4.cs:243
/// <summary>
/// Gets or sets the component at the specified index.
/// </summary>
/// <value>The value of the red, green, blue, and alpha components, 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 float this[int index]
{
readonly get
{
return index switch
{
0 => R,
1 => G,
2 => B,
3 => A,
_ => throw new ArgumentOutOfRangeException(nameof(index), "Indices for Color4 run from 0 to 3, inclusive."),
};
}
set
{
switch (index)
{
case 0: R = value; break;
case 1: G = value; break;
case 2: B = value; break;
case 3: A = value; break;
default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for Color4 run from 0 to 3, inclusive.");
}
}
}
/// <summary>
/// Converts the color into a packed integer.View on GitHub (pinned to 96fad776d2)