Validate palette entries separately

Keep valid colors from a submitted palette while reporting each rejected entry by name.

Use Color::tryFrom() when one invalid value should not reject an entire submitted palette:

use PhpColor\Color\Color;

$input = [
    'brand' => '#7c3aed',
    'accent' => 'not-a-color',
    'surface' => 'oklch(0.96 0.01 260)',
];

$valid = $invalid = [];

foreach ($input as $name => $value) {
    $color = Color::tryFrom($value);

    if (null === $color) {
        $invalid[$name] = $value;
        continue;
    }

    $valid[$name] = $color->toCss();
}

assert(['accent' => 'not-a-color'] === $invalid);
assert('rgb(124 58 237)' === $valid['brand']);
use PhpColor\Color\Color; $input = [ 'brand' => '#7c3aed', 'accent' => 'not-a-color', 'surface' => 'oklch(0.96 0.01 260)', ]; $valid = $invalid = []; foreach ($input as $name => $value) { $color = Color::tryFrom($value); if (null === $color) { $invalid[$name] = $value; continue; } $valid[$name] = $color->toCss(); } assert(['accent' => 'not-a-color'] === $invalid); assert('rgb(124 58 237)' === $valid['brand']);

The original keys identify the failed fields. Use ColorPalette::parse() instead when the whole palette must be rejected after the first invalid entry.

Review the parsing boundary or Color.