{"record":{"id":"a29f1526f311619d","repo":"Intervention/image","slug":"unable-to-import-color-colorclass-to-class","errorCode":null,"errorMessage":"Unable to import color {colorClass} to {class}","messagePattern":"Unable to import color (.+?) to (.+?)","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\ColorException","httpStatus":null,"severity":"error","filePath":"src/Colors/Cmyk/Colorspace.php","lineNumber":85,"sourceCode":"\n    /**\n     * {@inheritdoc}\n     *\n     * @see ColorspaceInterface::importColor()\n     *\n     * @throws ColorException\n     */\n    public function importColor(ColorInterface $color): CmykColor\n    {\n        return match ($color::class) {\n            OklchColor::class,\n            OklabColor::class,\n            HsvColor::class,\n            NamedColor::class,\n            HslColor::class => $this->importViaRgbColor($color),\n            RgbColor::class => $this->importRgbColor($color),\n            CmykColor::class => $color,\n            default => throw new ColorException(\n                'Unable to import color ' . $color::class . ' to ' . $this::class,\n            ),\n        };\n    }\n\n    /**\n     * Import given RGB color to CMYK colorspace.\n     *\n     * @throws ColorException\n     */\n    private function importRgbColor(RgbColor $color): CmykColor\n    {\n        $c = (255 - $color->red()->value()) / 255.0 * 100;\n        $m = (255 - $color->green()->value()) / 255.0 * 100;\n        $y = (255 - $color->blue()->value()) / 255.0 * 100;\n        $k = intval(round(min([$c, $m, $y])));\n\n        $c = intval(round($c - $k));","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Cmyk/Colorspace.php#L67-L103","documentation":"When RemoveAnimationModifier's $position is a string, normalizePosition() first casts numeric strings to int, then tries the strict pattern ^[0-9]{1,3}%$ for percentages. Any other string — negative percents, 4+ digit percents, spaces, decimals, unit suffixes — throws InvalidArgumentException. The regex is deliberately tight: 1-3 digits followed immediately by '%'.","triggerScenarios":"->removeAnimation('50 %') (space), ->removeAnimation('-10%'), ->removeAnimation('1000%'), ->removeAnimation('50percent'), ->removeAnimation('first').","commonSituations":"User input trimmed insufficiently ('50 ' or '50 %'); percentage values from spreadsheets formatted like '50.0%'; locale-specific formatting; attempts to use frame labels or relative keywords.","solutions":["Normalize input before calling: strip spaces, cast integer-like values to int, format percents as 'NN%' with 1-3 digits.","Validate with the same pattern: preg_match('/^[0-9]{1,3}%$/', $value) === 1 before passing a string.","Pass an int when you know the frame index; strings are only needed for percentage selection.","For values above 999% (nonsensical) clamp or reject outright at your input layer."],"exampleFix":"// before\n$image->removeAnimation($request->input('position')); // '50 %' throws\n\n// after\n$position = preg_replace('/\\s+/', '', $request->input('position'));\nif (is_numeric($position)) {\n    $position = (int) $position;\n}\n$image->removeAnimation($position);","handlingStrategy":"validation","validationCode":"$position = preg_replace('/\\s+/', '', (string) $position);\nif (is_numeric($position)) {\n    $position = max(0, (int) $position);\n} elseif (preg_match('/^[0-9]{1,3}%$/', $position) !== 1) {\n    throw new InvalidArgumentException('Bad position: ' . $position);\n}\n$image->removeAnimation($position);","typeGuard":"function isRemovableAnimationPosition(int|string $position): bool\n{\n    return is_int($position)\n        ? $position >= 0\n        : (is_numeric($position) || preg_match('/^[0-9]{1,3}%$/', $position) === 1);\n}","tryCatchPattern":"try {\n    $image->removeAnimation($position);\n} catch (Intervention\\Image\\Exceptions\\InvalidArgumentException $e) {\n    // fall back to frame 0\n    $image->removeAnimation(0);\n}","preventionTips":["Strip whitespace from user input before passing position strings.","Only 1-3 digits followed by '%' are accepted — no minus sign, no decimals.","Cast integer-like strings to int before calling."],"tags":["animation","percent-value","string-validation","php"],"backgroundTag":"malformed-percent-string","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}