{"record":{"id":"aeb33112d54b86d2","repo":"stride3d/stride","slug":"indices-for-colorbgra-run-from-0-to-3-inclusive","errorCode":null,"errorMessage":"Indices for ColorBGRA run from 0 to 3, inclusive.","messagePattern":"Indices for ColorBGRA run from 0 to 3, inclusive\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Mathematics/ColorBGRA.cs","lineNumber":194,"sourceCode":"\n    /// <summary>\n    /// Gets or sets the component at the specified index.\n    /// </summary>\n    /// <value>The value of the alpha, red, green, or blue component, depending on the index.</value>\n    /// <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>\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 => B,\n                1 => G,\n                2 => R,\n                3 => A,\n                _ => throw new ArgumentOutOfRangeException(nameof(index), \"Indices for ColorBGRA run from 0 to 3, inclusive.\"),\n            };\n        }\n\n        set\n        {\n            switch (index)\n            {\n                case 0: B = value; break;\n                case 1: G = value; break;\n                case 2: R = value; break;\n                case 3: A = value; break;\n                default: throw new ArgumentOutOfRangeException(nameof(index), \"Indices for ColorBGRA run from 0 to 3, inclusive.\");\n            }\n        }\n    }\n\n    /// <summary>\n    /// Converts the color into a packed integer.","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Mathematics/ColorBGRA.cs#L176-L212","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Fix the index value to be within 0-3 (remember: 0=B, 1=G, 2=R, 3=A).","Check loop bounds: use 'for (int i = 0; i < 4; i++)' not '<= 4' or a wrong constant.","Prefer named properties (B, G, R, A) over the indexer when the channel is known at compile time."],"exampleFix":"// before\nfor (int i = 0; i <= 4; i++) Console.WriteLine(color[i]);\n// after\nfor (int i = 0; i < 4; i++) Console.WriteLine(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[i]; }\ncatch (ArgumentOutOfRangeException ex) { /* fall back to named property or skip */ }","preventionTips":["Prefer named B/G/R/A properties over the indexer.","Remember ColorBGRA index order is B,G,R,A — not RGBA.","Use i < 4 (not <= 4) in channel loops."],"tags":["argument-out-of-range","indexer","csharp","mathematics"],"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"}