Three additive channels, each a fraction between 0 and 1. Every hex code and colour name parses into this model, and every conversion passes through it: toCmykArray() on an Hsl object converts to RGB first.
Model
| Item | Value |
|---|---|
| Type string | RGB |
| Enum case | ColorModelType::Rgb |
| Class | \Com\Tecnick\Color\Model\Rgb |
| PDF colour space | DeviceRGB |
| Notations | #RGB, #RGBA, #RRGGBB, #RRGGBBAA, colour names, rgb(), rgba(), ["RGB",r,g,b] |
Components
| Component | Key | Range | Out of range |
|---|---|---|---|
| Red | red | 0.0 to 1.0 | clamped |
| Green | green | 0.0 to 1.0 | clamped |
| Blue | blue | 0.0 to 1.0 | clamped |
| Alpha | alpha | 0.0 to 1.0, default 1.0 | clamped |
Construction
<?php
require_once __DIR__ . '/vendor/autoload.php';
$rgb = new \Com\Tecnick\Color\Model\Rgb([
'red' => 0.2,
'green' => 0.4,
'blue' => 0.6,
]);
echo $rgb->getCssColor(), "\n";
echo $rgb->getRgbHexColor(), "\n";
rgb(51,102,153)
#336699
Output
Every accessor, against #336699:
| Call | Result |
|---|---|
getCssColor() | rgb(51,102,153) |
getRgbHexColor() | #336699 |
getRgbaHexColor() | #336699ff |
getComponentsString() | 0.200000 0.400000 0.600000 |
getPdfColor() | 0.200000 0.400000 0.600000 rg |
getPdfColor(true) | 0.200000 0.400000 0.600000 RG |
getJsPdfColor() | ["RGB",0.200000,0.400000,0.600000] |
getArray() | ['R' => 0.2, 'G' => 0.4, 'B' => 0.6, 'A' => 1.0] |
getNormalizedArray(255) | ['R' => 51.0, 'G' => 102.0, 'B' => 153.0, 'A' => 1.0] |
getPDFacArray() | [0.2, 0.4, 0.6] |
The Two Hex Getters
getRgbHexColor() drops the alpha channel, getRgbaHexColor() appends it. Both are on every model, not just this one.
$web = new \Com\Tecnick\Color\Web();
$col = $web->getColorObj('rgba(51,102,153,0.8)');
echo $col->getRgbHexColor(), "\n";
echo $col->getRgbaHexColor(), "\n";
echo $col->getCssColor(), "\n";
#336699
#336699cc
rgba(51,102,153,0.8)
Web::WEBHEX stores the eight-digit form, so a lookup in that constant and a call to getRgbHexColor() return strings of different lengths for the same colour. Compare through extractHexCode() or through the components, not by string equality.
Conversions
RGB reaches the other four models directly, and they reach each other through it. Conversions has the matrix and the formulas.
See Also
- tc-lib-color : the library overview.
- Colour Models : the five side by side.
- Grayscale : one channel, weighted from these three.
- HSL : the same colours, addressed by hue.
- Web Colours : the 150 names and their RGB components.
- API reference : the generated class documentation.