Two exception classes, split by the origin of the fault.
| Class | Extends | Signals |
|---|---|---|
\Com\Tecnick\Color\Exception | \Exception | input the library cannot parse |
\Com\Tecnick\Color\UnknownComponentException | \LogicException | a 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
| Call | Class | Message |
|---|---|---|
getColorObj('#12') | Exception | unable to extract the color hash: #12 |
getColorObj('#GGGGGG') | Exception | unable to extract the color hash: #gggggg |
getColorObj('notacolor') | Exception | unable to find the color hex for the name: notacolor |
getColorObj('rgb(1,2)') | Exception | invalid css color: rgb(1,2) |
getColorObj('rgb(1, 2 3)') | Exception | mixed css color separators: rgb(1, 2 3) |
getColorObj('lab(50)') | Exception | invalid css color: lab(50) |
getColorObj('["XYZ",1]') | Exception | invalid javascript color: ["xyz",1] |
getColorObj('oklch(0.7 0.1 200)') | Exception | unsupported color syntax: oklch(0.7 0.1 200) |
getHexFromName('nosuch') | Exception | unable to find the color hex for the name: nosuch |
getNameFromHex('#010203') | Exception | unable to find the color name for the hex code: #010203 |
extractHexCode('zz') | Exception | unable to extract the color hash: zz |
getSpotColor('nope') | Exception | unable to find the spot color: nope |
getPdfSpotResources() before getPdfSpotObjects() | Exception | unable to reference a spot color that has no PDF object, call getPdfSpotObjects() first: x |
new Rgb(['purple' => 1.0]) | UnknownComponentException | unknown color components: purple |
Model::create('XYZ', []) | Exception | unknown color model type: XYZ |
ColorModelType::fromLoose('XYZ') | Exception | unknown 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:
| Input | Result |
|---|---|
| a component outside its range | clamped to the range |
a hue outside [0..1) | wrapped, not clamped |
| a non-numeric component value | the component default |
NAN | the component default |
INF, -INF | the ends of the range |
| a missing alpha | 1.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
- tc-lib-color : the library overview.
- Notations : what parses and what does not.
- Colour Models : the component names each model defines.
- PDF Output : the methods that return an empty string instead.
- API reference : the generated class documentation.