{"record":{"id":"99f03a0c43aad363","repo":"SixLabors/ImageSharp","slug":"component-index-must-be-between-0-and-this-componentcount-1","errorCode":null,"errorMessage":"Component index must be between 0 and {this.ComponentCount - 1} inclusive.","messagePattern":"Component index must be between 0 and (.+?) inclusive\\.","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/PixelFormats/PixelComponentInfo.cs","lineNumber":97,"sourceCode":"            }\n\n            sum += p;\n        }\n\n        return new PixelComponentInfo(count, bitsPerPixel - sum, precisionData1, precisionData2);\n    }\n\n    /// <summary>\n    /// Returns the precision of the component in bits at the given index.\n    /// </summary>\n    /// <param name=\"componentIndex\">The component index.</param>\n    /// <returns>The <see cref=\"int\"/>.</returns>\n    /// <exception cref=\"ArgumentOutOfRangeException\">The component index cannot exceed the component range.</exception>\n    public int GetComponentPrecision(int componentIndex)\n    {\n        if (componentIndex < 0 || componentIndex >= this.ComponentCount)\n        {\n            throw new ArgumentOutOfRangeException($\"Component index must be between 0 and {this.ComponentCount - 1} inclusive.\");\n        }\n\n        long selectedPrecisionData = componentIndex < 8 ? this.precisionData1 : this.precisionData2;\n        return (int)((selectedPrecisionData >> (8 * (componentIndex & 7))) & 0xFF);\n    }\n\n    /// <summary>\n    /// Returns the maximum precision in bits of all components.\n    /// </summary>\n    /// <returns>The <see cref=\"int\"/>.</returns>\n    public int GetMaximumComponentPrecision()\n    {\n        int maxPrecision = 0;\n        for (int i = 0; i < this.ComponentCount; i++)\n        {\n            int componentPrecision = this.GetComponentPrecision(i);\n            if (componentPrecision > maxPrecision)\n            {","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/PixelFormats/PixelComponentInfo.cs#L79-L115","documentation":"PixelComponentInfo.GetComponentPrecision retrieves the bit precision of a component by index; valid indices are 0 through ComponentCount-1. Any other index throws ArgumentOutOfRangeException because precision is packed for exactly the declared component count.","triggerScenarios":"Calling GetComponentPrecision with a negative index or an index >= the ComponentCount the info was created with, e.g. querying component 3 on RGB (3 components) or using a pixel type's component count different from the PixelComponentInfo's.","commonSituations":"Looping over a hardcoded component count instead of info.ComponentCount; reusing a PixelComponentInfo built for another pixel type; off-by-one loops using <=.","solutions":["Iterate with for (int i = 0; i < info.ComponentCount; i++)","Match the PixelComponentInfo instance to the actual pixel type you are querying","Guard the index before the call if it comes from external input","Catch ArgumentOutOfRangeException when handling arbitrary pixel formats dynamically"],"exampleFix":"// before\nfor (int i = 0; i <= 4; i++) info.GetComponentPrecision(i);\n// after\nfor (int i = 0; i < info.ComponentCount; i++) info.GetComponentPrecision(i);","handlingStrategy":"validation","validationCode":"if ((uint)componentIndex < (uint)info.ComponentCount) { int p = info.GetComponentPrecision(componentIndex); }","typeGuard":"static bool InRange(PixelComponentInfo info, int i) => (uint)i < (uint)info.ComponentCount;","tryCatchPattern":"try { int p = info.GetComponentPrecision(i); } catch (ArgumentOutOfRangeException) { p = 0; }","preventionTips":["Loop with i < info.ComponentCount, never hardcoded counts","Keep PixelComponentInfo instances paired with their pixel type","Validate external indices before querying"],"tags":["pixels","pixelformat","index-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}