{"record":{"id":"20c68d7223076f17","repo":"Intervention/image","slug":"unable-to-set-color-via-array-access-use-addcolor","errorCode":null,"errorMessage":"Unable to set color via array access, use addColor() instead","messagePattern":"Unable to set color via array access, use addColor\\(\\) instead","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Colors/Palette.php","lineNumber":70,"sourceCode":"     * {@inheritdoc}\n     *\n     * @see ArrayAccess::offsetGet()\n     */\n    public function offsetGet(mixed $offset): ColorInterface\n    {\n        return $this->toArray()[$offset];\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see ArrayAccess::offsetSet()\n     *\n     * @throws NotSupportedException\n     */\n    public function offsetSet(mixed $offset, mixed $value): void\n    {\n        throw new NotSupportedException('Unable to set color via array access, use addColor() instead');\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see ArrayAccess::offsetUnset()\n     *\n     * @throws NotSupportedException\n     */\n    public function offsetUnset(mixed $offset): void\n    {\n        throw new NotSupportedException('Unable to remove color from palette via array access');\n    }\n\n    /**\n     * Implementation of IteratorAggregate.\n     *\n     * @return Traversable<ColorInterface>","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Palette.php#L52-L88","documentation":"Palette implements ArrayAccess for reading only; offsetSet() unconditionally throws NotSupportedException to direct writes to the accepted API addColor() (src/Colors/Palette.php:68-71). offsetUnset() is disabled for the same reason, since bins are keyed by color hashes and hold occurrence counts that offsetSet could not maintain.","triggerScenarios":"Executing $palette[0] = $color, $palette[] = $color, or any ArrayAccess assignment on a Palette, e.g. one returned by ColorExtractor or built from an image.","commonSituations":"Treating Palette like a plain array during refactors (foreach + assignment, array_map-style rebuilds); generic collection code that writes via offsetSet on anything ArrayAccess.","solutions":["Use $palette->addColor($color, $count) for every insert","Build a new palette from scratch: new Palette([$colorA, $colorB]) which routes through addColor()","Use $palette->filter()/map() to derive modified palettes instead of in-place writes","To remove entries, create a filtered palette rather than unset($palette[$i]) (also disabled)"],"exampleFix":"// before\n$palette = $image->extractColorPalette();\n$palette[0] = $myColor; // NotSupportedException\n\n// after\n$palette = $image->extractColorPalette();\n$palette->addColor($myColor); // optionally with count: addColor($myColor, 3)","handlingStrategy":"type-guard","validationCode":"// Detect palette-like collections and route writes to the supported API\nif ($palette instanceof Intervention\\Image\\Interfaces\\PaletteInterface) {\n    $palette->addColor($color, $count);\n} else {\n    $palette[] = $color; // plain array path\n}","typeGuard":"function addColorToPalette(PaletteInterface $palette, ColorInterface $color, int $count = 1): PaletteInterface\n{\n    return $palette->addColor($color, $count);\n}","tryCatchPattern":null,"preventionTips":["Treat Palette as read-only over ArrayAccess: iterate and filter()/map() instead of writing","Use addColor($color, $count) for inserts and new Palette([...]) for bulk construction","Remember offsetUnset() is disabled too, so removals must go through filter()"],"tags":["color","palette","arrayaccess","immutable-collection","php"],"backgroundTag":"immutable-collection-write","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}