Errors

The tc-lib-color exceptions: the two classes, the shared interface, every message with the call that raises it, and what clamps instead

Two exception classes, split by the origin of the fault.

ClassExtendsSignals
\Com\Tecnick\Color\Exception\Exceptioninput the library cannot parse
\Com\Tecnick\Color\UnknownComponentException\LogicExceptiona component name no model defines

Both implement \Com\Tecnick\Color\ExceptionInterface, so one catch covers them:

<?php

require_once __DIR__ . '/vendor/autoload.php';

$web = new \Com\Tecnick\Color\Web();

foreach (['#12', ['red' => 1.0, 'purple' => 0.5]] as $input) {
    try {
        $col = is_array($input)
            ? new \Com\Tecnick\Color\Model\Rgb($input)
            : $web->getColorObj($input);
        echo $col->getCssColor(), "\n";
    } catch (\Com\Tecnick\Color\ExceptionInterface $exc) {
        echo get_class($exc), ': ', $exc->getMessage(), "\n";
    }
}
Com\Tecnick\Color\Exception: unable to extract the color hash: #12
Com\Tecnick\Color\UnknownComponentException: unknown color components: purple

A colour string arriving from a stylesheet or a template can be wrong at runtime; a component name is written in the calling code, so a wrong one is a bug. The lenient accessors follow that split: tryGetColorObj() and getColorObject() catch Exception and let UnknownComponentException through.

$pdf = new \Com\Tecnick\Color\Pdf();

// the lenient accessors catch Com\Tecnick\Color\Exception
var_dump($pdf->tryGetColorObj('notacolor'));
var_dump($pdf->getColorObject('notacolor'));
echo '[', $pdf->getPdfFillColor('notacolor'), "]\n";
NULL
NULL
[]

getPdfColor() and its wrappers return an empty string for anything they cannot resolve, so one bad colour in a stylesheet leaves one element uncoloured rather than failing the document.

Every Message

CallClassMessage
getColorObj('#12')Exceptionunable to extract the color hash: #12
getColorObj('#GGGGGG')Exceptionunable to extract the color hash: #gggggg
getColorObj('notacolor')Exceptionunable to find the color hex for the name: notacolor
getColorObj('rgb(1,2)')Exceptioninvalid css color: rgb(1,2)
getColorObj('rgb(1, 2 3)')Exceptionmixed css color separators: rgb(1, 2 3)
getColorObj('lab(50)')Exceptioninvalid css color: lab(50)
getColorObj('["XYZ",1]')Exceptioninvalid javascript color: ["xyz",1]
getColorObj('oklch(0.7 0.1 200)')Exceptionunsupported color syntax: oklch(0.7 0.1 200)
getHexFromName('nosuch')Exceptionunable to find the color hex for the name: nosuch
getNameFromHex('#010203')Exceptionunable to find the color name for the hex code: #010203
extractHexCode('zz')Exceptionunable to extract the color hash: zz
getSpotColor('nope')Exceptionunable to find the spot color: nope
getPdfSpotResources() before getPdfSpotObjects()Exceptionunable to reference a spot color that has no PDF object, call getPdfSpotObjects() first: x
new Rgb(['purple' => 1.0])UnknownComponentExceptionunknown color components: purple
Model::create('XYZ', [])Exceptionunknown color model type: XYZ
ColorModelType::fromLoose('XYZ')Exceptionunknown color model type: XYZ

A hash is lowercased before it is reported, so #GGGGGG appears as #gggggg.

Accepted Without Raising

The inputs that are accepted and adjusted rather than rejected:

InputResult
a component outside its rangeclamped to the range
a hue outside [0..1)wrapped, not clamped
a non-numeric component valuethe component default
NANthe component default
INF, -INFthe ends of the range
a missing alpha1.0
transparent, color.transparent, ''null
hsl(210,50,40)the same as hsl(210,50%,40%)
an unparseable colour, through getPdfColor()an empty string
an unparseable colour, through getJsColorString()color.transparent
// out of range clamps, non-numeric falls back to the default
$rgb = new \Com\Tecnick\Color\Model\Rgb([
    'red' => 5.0,
    'green' => -1.0,
    'blue' => 'abc',
]);

echo $rgb->getCssColor(), "\n";
echo (new \Com\Tecnick\Color\Model\Hsl(['hue' => 1.25, 'saturation' => 1.0, 'lightness' => 0.5]))->getCssColor(), "\n";
rgb(255,0,0)
hsl(90,100%,50%)

Validate a colour string at the point it is accepted, with getColorObj() in a try. Notations has the rules a string has to satisfy.

See Also