Grayscale

The grayscale colour model in tc-lib-color: one component, the Rec. 709 luma weights, and the g() notation

The grayscale model holds one component, 0.0 for black to 1.0 for white. DeviceGray takes a single operand per colour operator, so a monochrome document written in it is smaller than the same document in DeviceRGB.

Model

ItemValue
Type stringGRAY
Enum caseColorModelType::Gray
Class\Com\Tecnick\Color\Model\Gray
PDF colour spaceDeviceGray
Notationsg(128), g(50%), ["G",0.5]

g() is a notation of this library, not a CSS function. getCssColor() emits it and getColorObj() reads it back. A browser does not parse it.

Components

ComponentKeyRangeOut of range
Graygray0.0 to 1.0clamped
Alphaalpha0.0 to 1.0, default 1.0clamped

Construction

<?php

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

$gray = new \Com\Tecnick\Color\Model\Gray(['gray' => 0.371920]);

echo $gray->getCssColor(), "\n";
echo $gray->getPdfColor();
echo $gray->getRgbHexColor(), "\n";
g(95)
0.371920 g
#5f5f5f

Output

Every accessor, against #336699:

CallResult
getCssColor()g(95)
getRgbHexColor()#5f5f5f
getRgbaHexColor()#5f5f5fff
getComponentsString()0.371920
getPdfColor()0.371920 g
getPdfColor(true)0.371920 G
getJsPdfColor()["G",0.371920]
getArray()['G' => 0.37192000000000003, 'A' => 1.0]
getNormalizedArray(255)['G' => 95.0, 'A' => 1.0]
getPDFacArray()[0.37192000000000003]

The Weights

The conversion from RGB uses the Rec. 709 luma coefficients: 0.2126 red, 0.7152 green, 0.0722 blue. Green carries most of the weight, so full green greys to 182 and full blue to 18.

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

foreach (['#ff0000', '#00ff00', '#0000ff', '#336699'] as $hex) {
    $gray = \Com\Tecnick\Color\Model::create('GRAY', $web->getColorObj($hex)->toGrayArray());
    printf("%-9s %s\n", $hex, $gray->getCssColor());
}
#ff0000   g(54)
#00ff00   g(182)
#0000ff   g(18)
#336699   g(95)

An unweighted mean of the three channels of #336699 is 102; the luma weights give 95.

toRgbArray() writes the grey value into all three channels.

Conversions

toHslArray() returns hue 0 and saturation 0, and a round trip through HSL returns the same grey. Keep the source colour where the hue is needed later.

See Also