Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
17.50% |
7 / 40 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ColorMix | |
17.50% |
7 / 40 |
|
25.00% |
1 / 4 |
375.95 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toCss | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
420 | |||
| evaluate | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| trim | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Css\Value; |
| 6 | |
| 7 | /** |
| 8 | * CSS Color 5 §3 — `color-mix(in <space>, c1 [p1%], c2 [p2%])`. |
| 9 | * |
| 10 | * Stored as the parsed declaration; the actual mixing math runs |
| 11 | * when the 4E color engine ships. Percentages are normalised at |
| 12 | * parse time per §3.1: |
| 13 | * |
| 14 | * - If neither percentage is supplied, both default to 50%. |
| 15 | * - If one is supplied, the other is 100% − that value. |
| 16 | * - If both sum to > 0% but ≠ 100%, multiply by 100/(p1+p2) to |
| 17 | * normalise and remember the original sum as `alphaMultiplier` |
| 18 | * so the resulting alpha can be scaled. |
| 19 | * - If both sum to 0%, the result is invalid; the parser |
| 20 | * returns null in that case. |
| 21 | * |
| 22 | * `hueInterpolation` is non-null only when `space` is a polar |
| 23 | * space (HSL, HWB, LCH, OKLCH); for other spaces the field is |
| 24 | * meaningless and stays null. |
| 25 | */ |
| 26 | final readonly class ColorMix extends Value |
| 27 | { |
| 28 | public function __construct( |
| 29 | public ColorSpace $space, |
| 30 | public Color $color1, |
| 31 | public float $percentage1, |
| 32 | public Color $color2, |
| 33 | public float $percentage2, |
| 34 | public float $alphaMultiplier = 1.0, |
| 35 | public ?HueInterpolation $hueInterpolation = null, |
| 36 | ) {} |
| 37 | |
| 38 | public function toCss(): string |
| 39 | { |
| 40 | $hueSuffix = $this->hueInterpolation !== null |
| 41 | ? ' ' . $this->hueInterpolation->value . ' hue' |
| 42 | : ''; |
| 43 | $space = match ($this->space) { |
| 44 | ColorSpace::sRGB => 'srgb', |
| 45 | ColorSpace::sRGBLinear => 'srgb-linear', |
| 46 | ColorSpace::DisplayP3 => 'display-p3', |
| 47 | ColorSpace::DisplayP3Linear => 'display-p3-linear', |
| 48 | ColorSpace::A98RGB => 'a98-rgb', |
| 49 | ColorSpace::A98RGBLinear => 'a98-rgb-linear', |
| 50 | ColorSpace::ProPhotoRGB => 'prophoto-rgb', |
| 51 | ColorSpace::ProPhotoRGBLinear => 'prophoto-rgb-linear', |
| 52 | ColorSpace::Rec2020 => 'rec2020', |
| 53 | ColorSpace::Rec2020Linear => 'rec2020-linear', |
| 54 | ColorSpace::Lab => 'lab', |
| 55 | ColorSpace::Lch => 'lch', |
| 56 | ColorSpace::OKLab => 'oklab', |
| 57 | ColorSpace::OKLCH => 'oklch', |
| 58 | ColorSpace::XYZ, ColorSpace::XYZD65 => 'xyz-d65', |
| 59 | ColorSpace::XYZD50 => 'xyz-d50', |
| 60 | ColorSpace::HWB => 'hwb', |
| 61 | ColorSpace::HSL => 'hsl', |
| 62 | }; |
| 63 | return sprintf( |
| 64 | 'color-mix(in %s%s, %s %s%%, %s %s%%)', |
| 65 | $space, |
| 66 | $hueSuffix, |
| 67 | $this->color1->toCss(), |
| 68 | self::trim($this->percentage1), |
| 69 | $this->color2->toCss(), |
| 70 | self::trim($this->percentage2), |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * CSS Color 5 §3 — resolve the mix to a concrete sRGB {@see Color} by |
| 76 | * interpolating the two colours in `$space` (with the hue direction for |
| 77 | * polar spaces) at the fraction implied by the normalised percentages, |
| 78 | * then scaling the result alpha by `$alphaMultiplier` (non-100 %-sum |
| 79 | * mixes make the result partly transparent). Both colours are always |
| 80 | * concrete — the parser rejects unresolvable arguments to a plain |
| 81 | * {@see \Phpdftk\Css\Value\CssFunction} — so this never fails. |
| 82 | */ |
| 83 | public function evaluate(): Color |
| 84 | { |
| 85 | $total = $this->percentage1 + $this->percentage2; |
| 86 | if ($total <= 0.0) { |
| 87 | // Degenerate (both 0 %); the parser normally rejects this, but |
| 88 | // fall back to transparent rather than dividing by zero. |
| 89 | return new Color(0.0, 0.0, 0.0, 0.0); |
| 90 | } |
| 91 | $t = $this->percentage2 / $total; |
| 92 | $mixed = ColorConverter::interpolate($this->color1, $this->color2, $t, $this->space, $this->hueInterpolation); |
| 93 | $alpha = max(0.0, min(1.0, $mixed->a * $this->alphaMultiplier)); |
| 94 | return new Color($mixed->r, $mixed->g, $mixed->b, $alpha); |
| 95 | } |
| 96 | |
| 97 | private static function trim(float $v): string |
| 98 | { |
| 99 | return fmod($v, 1.0) === 0.0 ? (string) (int) $v : (string) $v; |
| 100 | } |
| 101 | } |