tecnickcom / tc-lib-color
PHP library to manipulate various color representations
Fund package maintenance!
Requires
- php: ^8.2
- ext-pcre: *
Requires (Dev)
- carthage-software/mago: 1.47.6
- pdepend/pdepend: ^2.16
- phpunit/phpunit: ^11.5 || ^12.5 || ^13.3
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-main
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.13.5
- 2.13.4
- 2.13.3
- 2.13.2
- 2.13.1
- 2.13.0
- 2.12.5
- 2.12.4
- 2.12.3
- 2.12.2
- 2.12.1
- 2.12.0
- 2.11.0
- 2.10.0
- 2.9.0
- 2.8.0
- 2.7.0
- 2.6.0
- 2.5.3
- 2.5.2
- 2.5.0
- 2.4.1
- 2.3.15
- 2.3.14
- 2.3.13
- 2.3.12
- 2.3.11
- 2.3.10
- 2.3.9
- 2.3.8
- 2.3.7
- 2.3.6
- 2.3.5
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.2.19
- 2.2.18
- 2.2.17
- 2.2.16
- 2.2.15
- 2.2.13
- 2.2.12
- 2.2.11
- 2.2.10
- 2.2.9
- 2.2.8
- 2.2.7
- 2.2.6
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 1.14.39
- 1.14.38
- 1.14.37
- 1.14.35
- 1.14.34
- 1.14.33
- 1.14.32
- 1.14.31
- 1.14.29
- 1.14.28
- 1.14.27
- 1.14.26
- 1.14.25
- 1.14.24
- 1.14.23
- 1.14.22
- 1.14.21
- 1.14.20
- 1.14.19
- 1.14.18
- 1.14.17
- 1.14.16
- 1.14.15
- 1.14.14
- 1.14.13
- 1.14.10
- 1.14.9
- 1.14.8
- 1.14.7
- 1.14.6
- 1.14.1
- 1.14.0
- 1.12.15
- 1.12.14
- 1.12.13
- 1.12.12
- 1.12.11
- 1.12.10
- 1.12.9
- 1.12.8
- 1.12.7
- 1.12.6
- 1.12.5
- 1.12.4
- 1.12.3
- 1.12.2
- 1.12.1
- 1.12.0
- 1.11.2
- 1.11.0
- 1.10.3
- 1.10.2
- 1.10.0
- 1.9.0
- 1.8.0
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.18
- 1.6.17
- 1.6.16
- 1.6.15
- 1.6.14
- 1.6.13
- 1.6.12
- 1.6.11
- 1.6.10
- 1.6.9
- 1.6.8
- 1.6.7
- 1.6.6
- 1.6.5
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.5
- 1.4.4
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.10
- 1.0.9
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2026-09-05 07:30:38 UTC
README
PHP library to manipulate various color representations.
💖 Part of the tc-lib-pdf / TCPDF ecosystem (100M+ installs). Sponsor its maintenance →
Overview
tc-lib-color parses, converts and formats the color values used in web and PDF output.
| Namespace | \Com\Tecnick\Color |
| Author | Nicola Asuni info@tecnick.com |
| License | GNU LGPL v3 - see LICENSE |
| API docs | https://tcpdf.org/docs/srcdoc/tc-lib-color |
| Packagist | https://packagist.org/packages/tecnickcom/tc-lib-color |
Features
Color Models
- Grayscale (GRAY)
- RGB/RGBA, including hexadecimal notations (RGB)
- HSL/HSLA (HSL)
- CMYK/CMYKA (CMYK)
- CIE Lab (LAB)
- Spot colors (Separation), with DeviceCMYK and Lab alternate color spaces for PDF output
Integration Helpers
- CSS output that parses back to the same color
- PDF and Acrobat JavaScript color output
- Cross-model conversion on all color models
- Named web color lookup (CSS Color Module Level 4 names)
ColorModelTypeenum andModel::create()factory
Requirements
- PHP 8.2 or later
- Composer
Installation
composer require tecnickcom/tc-lib-color
Quick Start
<?php require_once __DIR__ . '/vendor/autoload.php'; $web = new \Com\Tecnick\Color\Web(); $rgb = $web->getRgbObjFromHex('#336699'); echo $rgb->getCssColor(); // rgb(51,102,153) echo $rgb->getRgbHexColor(); // #336699 echo $rgb->getPdfColor(); // "0.200000 0.400000 0.600000 rg\n"
Any supported notation can be parsed through the same entry point:
$web->getColorObj('rgb(51 102 153)'); $web->getColorObj('hsl(210,50%,40%)'); $web->getColorObj('cmyk(67%,33%,0%,40%)'); $web->getColorObj('lab(41% -2 -25)'); $web->getColorObj('steelblue');
getCssColor() emits a value that getColorObj() parses back to the same color.
Supported color notations
| Notation | Examples |
|---|---|
| Hexadecimal | #RGB, #RGBA, #RRGGBB, #RRGGBBAA |
| Name | steelblue, color.steelblue, transparent |
| Gray | g(128), g(50%) |
| RGB | rgb(51,102,153), rgb(20% 40% 60%), rgba(51,102,153,0.85), rgb(51 102 153 / 85%) |
| HSL | hsl(210,50%,40%), hsl(210deg 50% 40%), hsla(210,50%,40%,0.85) |
| CMYK | cmyk(67%,33%,0%,40%), cmyka(67,33,0,40,0.85) |
| CIE Lab | lab(41% -2 -25), lab(41 -2 -25 / 0.85) |
| Acrobat JavaScript | ["T"], ["G",0.5], ["RGB",0.2,0.4,0.6], ["CMYK",0.67,0.33,0,0.4] |
Components are separated either by commas or by spaces, not by a mixture of the
two, and the alpha channel by a comma in the first form or by a slash in the
second. A hue accepts the CSS angle units (deg, grad, rad, turn).
Out-of-range values are clamped and a hue wraps, as CSS Color Level 4 requires,
so rgb(-10,0,0) is black and hsl(-150,50%,50%) is the same as
hsl(210,50%,50%). Anything else is rejected with a
Com\Tecnick\Color\Exception.
g(), cmyk() and cmyka() are notations of this library, not CSS functions,
and round-trip through getCssColor() and getColorObj() like the others.
Models can also be built directly by type:
use Com\Tecnick\Color\ColorModelType; use Com\Tecnick\Color\Model; $cmyk = Model::create(ColorModelType::Cmyk, [ 'cyan' => 0.67, 'magenta' => 0.33, 'yellow' => 0.0, 'key' => 0.4, 'alpha' => 1.0, ]);
Nearest named color
$web = new \Com\Tecnick\Color\Web(); $web->getClosestWebColorFromString('#9577a6'); // 'lightslategray' $web->getClosestWebColorByDeltaEFromString('#9577a6'); // 'plum'
getClosestWebColor() measures the Euclidean distance in sRGB, which is not
perceptually uniform. getClosestWebColorByDeltaE() measures the CIE76
difference in CIE Lab, which tracks perceived difference.
PDF and spot colors
$pdf = new \Com\Tecnick\Color\Pdf(); $pdf->getPdfFillColor('steelblue'); // "0.274510 0.509804 0.705882 rg\n" $pdf->addSpotColorFromArray('My Spot', ['cyan' => 1]); $pon = 0; $objects = $pdf->getPdfSpotObjects($pon); // write these first $resources = $pdf->getPdfSpotResources(); // then reference them
Eight of the eleven default spot color names are also CSS color names (red,
green, blue, cyan, magenta, yellow, black, white) and resolve to
the spot color. They agree with their CSS namesake except green: the spot
Green is #00ff00 while CSS green is #008000. Pass $allowSpot = false to
getPdfColor(), getPdfFillColor(), getPdfStrokeColor(), getColorObject(),
getPdfRgbComponents() or getPdfCmykComponents() to force the device color.
The remaining three, key, all and none, are spot-only names. With
$allowSpot = false they do not resolve: getPdfColor() and the component
accessors return an empty string and getColorObject() returns null.
Every registered spot color must be emitted by getPdfSpotObjects() before
getPdfSpotResources() is called; otherwise the resource writers raise a
Com\Tecnick\Color\Exception.
Exceptions
Com\Tecnick\Color\Exception (a \Exception) signals an invalid input.
Com\Tecnick\Color\UnknownComponentException (a \LogicException) signals a
component name a model does not define, and is not swallowed by the lenient
accessors tryGetColorObj() and getColorObject(). Both implement
Com\Tecnick\Color\ExceptionInterface, so a single catch covers them.
See example/index.php for a web-color conversion table covering RGB, HSL, the
Acrobat JavaScript form and the nearest-color inversion.
Roles and extension points
Pdf extends Spot extends Web extends Css, so one object offers the
parser, the spot registry and the PDF writer. Each role also has its own
interface to type against: ColorParserInterface, SpotRegistryInterface and
PdfColorWriterInterface.
Four methods can be overridden: Web::getColorObj(), Spot::getSpotColor(),
Pdf::getPdfColor() and Pdf::getColorObject(), plus the protected parser
methods on Css and Spot::resolveSpotColorData(). Every other public method
is final. A subclass that overrides one of the four must mirror its signature
exactly.
Versioning
This project follows Semantic Versioning. A change that alters a public signature, the bytes a method emits, or whether an input parses, goes in a major release. See CHANGELOG.md.
Development
make deps
make help
make qa
make server
make server serves the example/ directory on http://localhost:8000. Use a custom port with make server PORT=8080.
Packaging
make rpm make deb
For system packages, bootstrap with:
require_once '/usr/share/php/Com/Tecnick/Color/autoload.php';
Contributing
Contributions are welcome. Please review CONTRIBUTING.md, CODE_OF_CONDUCT.md, and SECURITY.md.