Skip to content

Colors

TCPDF-Next provides full color management through the HasColors trait, the Graphics\Color value object, and the Graphics\ColorSpace abstraction. Colors apply independently to strokes, fills, and text.

Setting Colors

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setDrawColor(255, 0, 0)      // Red stroke
    ->setFillColor(0, 0, 255)      // Blue fill
    ->setTextColor(51, 51, 51);    // Dark gray text

Signatures

php
setDrawColor(int $r, int $g = -1, int $b = -1): static   // Stroke color
setFillColor(int $r, int $g = -1, int $b = -1): static   // Fill color
setTextColor(int $r, int $g = -1, int $b = -1): static   // Text color

When only $r is provided ($g and $b default to -1), the value is treated as grayscale (0 = black, 255 = white).

Color Value Object

php
use Yeeefang\TcpdfNext\Graphics\Color;

$red  = Color::rgb(255, 0, 0);
$cyan = Color::cmyk(100, 0, 0, 0);
$gray = Color::gray(128);
$spot = Color::spot('Pantone 151 C', 0, 60, 100, 0);
Factory MethodParametersColor Space
Color::rgb(r, g, b)0--255 per channelDeviceRGB
Color::cmyk(c, m, y, k)0--100 per channelDeviceCMYK
Color::gray(value)0--255DeviceGray
Color::spot(name, c, m, y, k)Name + CMYK fallbackSeparation

Color Spaces

Color SpaceUsage
DeviceRGBScreen-oriented output (web, presentations)
DeviceCMYKPrint-oriented output (commercial printing)
DeviceGrayGrayscale documents
SeparationSpot/Pantone colors for brand-accurate printing

CMYK values are written directly into the PDF stream. For color-managed workflows, embed an ICC profile via setColorProfile().

Transparency

php
setAlpha(float $opacity = 1.0, string $blendMode = 'Normal'): static
php
$pdf->setAlpha(0.5, 'Multiply')    // 50% opacity, Multiply blend
    ->rect(10, 10, 50, 30, 'F')
    ->setAlpha(1.0);                // Reset to fully opaque

Always reset opacity after transparent sections to prevent unintended effects on subsequent content.

Blend Modes

All 16 PDF blend modes are supported:

ModeEffect
NormalSource over destination (default)
MultiplyDarkens — multiplies channel values
ScreenLightens — inverse multiply
OverlayCombines Multiply and Screen
Darken / LightenKeeps darker / lighter value per channel
ColorDodge / ColorBurnBrightens / darkens destination
HardLight / SoftLightStrong / subtle light effect
Difference / ExclusionChannel difference (high / low contrast)
Hue / SaturationSource hue or saturation, destination luminosity
Color / LuminositySource color or luminosity

Gradients

Four gradient types are rendered as native PDF shading objects:

php
$pdf->linearGradient(10, 10, 100, 50, '#ff0000', '#0000ff', [0, 0, 1, 1]);   // Linear
$pdf->radialGradient(60, 60, 80, 80, '#ffffff', '#000000');                    // Radial
$pdf->coonsPatchMesh(10, 10, 100, 100, $colors, $coords);       // Coons patch mesh
$pdf->tensorProductPatchMesh(10, 10, 100, 100, $colors, $coords); // Tensor-product
  • Linear — two-color gradient along a direction vector.
  • Radial — two-color gradient expanding from a center point.
  • Coons patch mesh — complex multi-color fills defined by Bezier control points.
  • Tensor-product patch mesh — Coons extension with additional interior control points.

Complete Example

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setDrawColor(255, 0, 0)       // Red stroke
    ->setFillColor(0, 0, 255)       // Blue fill
    ->rect(10, 10, 50, 30, 'DF')    // Draw + Fill
    ->setAlpha(0.5, 'Multiply')     // 50% opacity with Multiply blend
    ->setFillColor(255, 255, 0)     // Yellow
    ->rect(30, 20, 50, 30, 'F')     // Overlapping semi-transparent rect
    ->setAlpha(1.0);                // Reset opacity

Tips

  • Use DeviceRGB for on-screen PDFs and DeviceCMYK for commercial print.
  • Spot colors require SpotColorManager — it is lazily initialized on first use.
  • Always reset alpha to 1.0 after transparent sections.
  • Gradient coordinates use the current user unit system (default: mm).

Released under the LGPL-3.0-or-later License.