Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RgbaPixel
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
9
 ofClamped
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 transparent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Raster;
6
7/**
8 * One pixel of an RGBA raster surface. Components are 8-bit integers
9 * in `[0, 255]`. Alpha follows the WHATWG canvas convention:
10 * `0` = fully transparent, `255` = fully opaque.
11 *
12 * The constructor validates each component is in range. Out-of-range
13 * values are clamped via {@see RgbaPixel::ofClamped} (preferred for
14 * arithmetic results that may overshoot) or rejected via the regular
15 * constructor (preferred for explicit caller-supplied values).
16 */
17final readonly class RgbaPixel
18{
19    public function __construct(
20        public int $r,
21        public int $g,
22        public int $b,
23        public int $a = 255,
24    ) {
25        if ($r < 0 || $r > 255 || $g < 0 || $g > 255 || $b < 0 || $b > 255 || $a < 0 || $a > 255) {
26            throw new \InvalidArgumentException(sprintf(
27                'RgbaPixel components must be in [0, 255]; got (%d, %d, %d, %d)',
28                $r,
29                $g,
30                $b,
31                $a,
32            ));
33        }
34    }
35
36    /**
37     * Clamp each component to `[0, 255]`. Useful when constructing
38     * from arithmetic results (filter outputs, blend computations)
39     * that may temporarily overshoot.
40     */
41    public static function ofClamped(int $r, int $g, int $b, int $a = 255): self
42    {
43        return new self(
44            r: max(0, min(255, $r)),
45            g: max(0, min(255, $g)),
46            b: max(0, min(255, $b)),
47            a: max(0, min(255, $a)),
48        );
49    }
50
51    /**
52     * Fully-transparent black — the default for unset pixels in a
53     * fresh {@see RasterSurface}.
54     */
55    public static function transparent(): self
56    {
57        return new self(0, 0, 0, 0);
58    }
59}