RGB

The RGB colour model in tc-lib-color: components, hex forms, CSS and PDF output, and the alpha channel

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

ItemValue
Type stringRGB
Enum caseColorModelType::Rgb
Class\Com\Tecnick\Color\Model\Rgb
PDF colour spaceDeviceRGB
Notations#RGB, #RGBA, #RRGGBB, #RRGGBBAA, colour names, rgb(), rgba(), ["RGB",r,g,b]

Components

ComponentKeyRangeOut of range
Redred0.0 to 1.0clamped
Greengreen0.0 to 1.0clamped
Blueblue0.0 to 1.0clamped
Alphaalpha0.0 to 1.0, default 1.0clamped

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:

CallResult
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