Serialize CSS gradients

Keep gradient geometry and color stops in one object, then emit the complete CSS function.

Three gradient types leave as complete CSS

The emitted strings contain the geometry, stop positions, and concrete color syntax represented by each PHPColor gradient object.

Build the artifact before serializing it

The builder owns direction and stops. toCss() emits the same object used for interpolation and sampling.

<?php

use PhpColor\Color\Gradient\GradientBuilder;

$gradient = GradientBuilder::linear(90)
    ->from('#806eae')
    ->via('#d97706')
    ->to('#f8fafc')
    ->build();

echo $gradient->toCss();

// linear-gradient(90deg in oklab, rgb(128 110 174) 0%,
//   rgb(217 119 6) 50%, rgb(248 250 252) 100%)
use PhpColor\Color\Gradient\GradientBuilder; $gradient = GradientBuilder::linear(90) ->from('#806eae') ->via('#d97706') ->to('#f8fafc') ->build(); echo $gradient->toCss(); // linear-gradient(90deg in oklab, rgb(128 110 174) 0%, // rgb(217 119 6) 50%, rgb(248 250 252) 100%)

Preserve interpolation through serialization

PHPColor samples the gradient in its selected interpolation space and includes that space in the serialized CSS function.

LinearAngle and ordered stops.
RadialShape, size, position, and stops.
ConicStarting angle, position, and stops.
CSS resultOne complete gradient function.

Choose the gradient geometry

Build, sample, and serialize one artifact