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 textSignatures
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 colorWhen 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 Method | Parameters | Color Space |
|---|---|---|
Color::rgb(r, g, b) | 0--255 per channel | DeviceRGB |
Color::cmyk(c, m, y, k) | 0--100 per channel | DeviceCMYK |
Color::gray(value) | 0--255 | DeviceGray |
Color::spot(name, c, m, y, k) | Name + CMYK fallback | Separation |
Color Spaces
| Color Space | Usage |
|---|---|
| DeviceRGB | Screen-oriented output (web, presentations) |
| DeviceCMYK | Print-oriented output (commercial printing) |
| DeviceGray | Grayscale documents |
| Separation | Spot/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'): staticphp
$pdf->setAlpha(0.5, 'Multiply') // 50% opacity, Multiply blend
->rect(10, 10, 50, 30, 'F')
->setAlpha(1.0); // Reset to fully opaqueAlways reset opacity after transparent sections to prevent unintended effects on subsequent content.
Blend Modes
All 16 PDF blend modes are supported:
| Mode | Effect |
|---|---|
Normal | Source over destination (default) |
Multiply | Darkens — multiplies channel values |
Screen | Lightens — inverse multiply |
Overlay | Combines Multiply and Screen |
Darken / Lighten | Keeps darker / lighter value per channel |
ColorDodge / ColorBurn | Brightens / darkens destination |
HardLight / SoftLight | Strong / subtle light effect |
Difference / Exclusion | Channel difference (high / low contrast) |
Hue / Saturation | Source hue or saturation, destination luminosity |
Color / Luminosity | Source 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 opacityTips
- 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.0after transparent sections. - Gradient coordinates use the current user unit system (default: mm).