stride3d/stride · error · ArgumentOutOfRangeException
Indices for Color run from 0 to 3, inclusive.
Error message
Indices for Color run from 0 to 3, inclusive.
What it means
Color implements an indexer mapping index 0-3 to R, G, B, A respectively. The library throws ArgumentOutOfRangeException from the getter when the supplied index is outside 0-3 because there is no fourth or later component to return. It is a defensive guard against bad indexing logic in caller code.
Solutions
- Clamp or bounds-check the index to 0..3 before accessing color[index].
- If you need only RGB, iterate 0..2 and access R/G/B fields directly instead of the indexer.
- If you intended a Color3, index a Color3 (0..2) rather than a Color.
Example fix
// before
for (int i = 0; i < 5; i++)
sum += color[i];
// after
for (int i = 0; i < 4; i++)
sum += 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[index]; }
catch (ArgumentOutOfRangeException) { /* fall back to default channel or log */ } Prevention
- Iterate with the constant 4 for Color, 3 for Color3, and never a hardcoded loop count shared across types.
- Prefer direct R/G/B/A field access over the indexer when the channel is known.
- Extract the component count from the type (or a constant) rather than duplicating magic numbers.
When it happens
Trigger: Reading color[index] with index < 0 or index > 3, e.g. iterating a 5-element loop or using a byte component count from another type (Color3 has only 3 components) to index a Color.
Common situations: Looping over '4 bytes per pixel' but starting at 1 instead of 0; copying generic component-access code written for Vector4 (which shares 0-3) from code that also handles Color3; off-by-one when reading the alpha channel.
Related errors
- Indices for Color3 run from 0 to 2, inclusive.
- Indices for Color4 run from 0 to 3, 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/76843ec779368e48.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core.Mathematics/Color.cs:232
/// <summary>
/// Gets or sets the component at the specified index.
/// </summary>
/// <value>The value of the red, green, blue, or alpha component, depending on the index.</value>
/// <param name="index">The index of the component to access. Use 0 for the red(R) component, 1 for the green(G) component, 2 for the blue(B) component, and 3 for the alpha(A) 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 => R,
1 => G,
2 => B,
3 => A,
_ => throw new ArgumentOutOfRangeException(nameof(index), "Indices for Color 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 Color run from 0 to 3, inclusive.");
}
}
}
/// <summary>
/// Converts the color into a packed integer.View on GitHub (pinned to 96fad776d2)