{"record":{"id":"76843ec779368e48","repo":"stride3d/stride","slug":"indices-for-color-run-from-0-to-3-inclusive","errorCode":null,"errorMessage":"Indices for Color run from 0 to 3, inclusive.","messagePattern":"Indices for Color run from 0 to 3, inclusive\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Mathematics/Color.cs","lineNumber":232,"sourceCode":"\n    /// <summary>\n    /// Gets or sets the component at the specified index.\n    /// </summary>\n    /// <value>The value of the red, green, blue, or alpha component, depending on the index.</value>\n    /// <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>\n    /// <returns>The value of the component at the specified index.</returns>\n    /// <exception cref=\"System.ArgumentOutOfRangeException\">Thrown when the <paramref name=\"index\"/> is out of the range [0, 3].</exception>\n    public byte this[int index]\n    {\n        readonly get\n        {\n            return index switch\n            {\n                0 => R,\n                1 => G,\n                2 => B,\n                3 => A,\n                _ => throw new ArgumentOutOfRangeException(nameof(index), \"Indices for Color run from 0 to 3, inclusive.\"),\n            };\n        }\n\n        set\n        {\n            switch (index)\n            {\n                case 0: R = value; break;\n                case 1: G = value; break;\n                case 2: B = value; break;\n                case 3: A = value; break;\n                default: throw new ArgumentOutOfRangeException(nameof(index), \"Indices for Color run from 0 to 3, inclusive.\");\n            }\n        }\n    }\n\n    /// <summary>\n    /// Converts the color into a packed integer.","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Mathematics/Color.cs#L214-L250","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nfor (int i = 0; i < 5; i++)\n    sum += color[i];\n// after\nfor (int i = 0; i < 4; i++)\n    sum += color[i];","handlingStrategy":"validation","validationCode":"if (index < 0 || index > 3) throw new ArgumentOutOfRangeException(nameof(index));\nvar channel = color[index];","typeGuard":"bool IsValidColorIndex(int i) => i is >= 0 and <= 3;","tryCatchPattern":"try { var c = color[index]; }\ncatch (ArgumentOutOfRangeException) { /* fall back to default channel or log */ }","preventionTips":["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."],"tags":["argument-out-of-range","indexing","csharp"],"backgroundTag":"index-out-of-range","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}