Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
6 / 7 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| DeviceCmyk | |
85.71% |
6 / 7 |
|
50.00% |
1 / 2 |
4.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toCss | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Css\Value; |
| 6 | |
| 7 | /** |
| 8 | * CSS Color 5 §6 — `device-cmyk(<c> <m> <y> <k> [/ <alpha>]? [, <rgb-fallback>]?)`. |
| 9 | * |
| 10 | * Direct CMYK colour specification, preserved verbatim so the |
| 11 | * PDF writer can emit it through PDF's native /DeviceCMYK |
| 12 | * colour space without round-tripping through sRGB. Components |
| 13 | * are stored as 0..1 floats (the spec accepts both 0..1 |
| 14 | * numbers and percentages — both forms are normalised here). |
| 15 | * |
| 16 | * color: device-cmyk(0 1 1 0); full red |
| 17 | * color: device-cmyk(20% 0% 100% 10%); olive-yellow |
| 18 | * color: device-cmyk(0 1 1 0 / 50%); half-transparent red |
| 19 | * color: device-cmyk(0 1 1 0, #ff0000); with sRGB fallback |
| 20 | * |
| 21 | * The optional sRGB fallback is what screen contexts use; the |
| 22 | * CMYK components are preserved for print PDF output where they |
| 23 | * become a /DeviceCMYK colorspace operator. |
| 24 | */ |
| 25 | final readonly class DeviceCmyk extends Value |
| 26 | { |
| 27 | public function __construct( |
| 28 | public float $c, |
| 29 | public float $m, |
| 30 | public float $y, |
| 31 | public float $k, |
| 32 | public float $alpha = 1.0, |
| 33 | public ?Color $fallback = null, |
| 34 | ) {} |
| 35 | |
| 36 | public function toCss(): string |
| 37 | { |
| 38 | $body = sprintf('%g %g %g %g', $this->c, $this->m, $this->y, $this->k); |
| 39 | if ($this->alpha < 1.0) { |
| 40 | $body .= ' / ' . sprintf('%g', $this->alpha); |
| 41 | } |
| 42 | if ($this->fallback !== null) { |
| 43 | return 'device-cmyk(' . $body . ', ' . $this->fallback->toCss() . ')'; |
| 44 | } |
| 45 | return 'device-cmyk(' . $body . ')'; |
| 46 | } |
| 47 | } |