| 1 |
<?php |
| 2 |
declare(strict_types = 1); |
| 3 |
|
| 4 |
namespace BaconQrCode\Renderer\RendererStyle; |
| 5 |
|
| 6 |
use BaconQrCode\Exception\RuntimeException; |
| 7 |
use BaconQrCode\Renderer\Color\ColorInterface; |
| 8 |
use BaconQrCode\Renderer\Color\Gray; |
| 9 |
|
| 10 |
final class Fill |
| 11 |
{ |
| 12 |
private static ?Fill $default = null; |
| 13 |
|
| 14 |
private function __construct( |
| 15 |
private readonly ColorInterface $backgroundColor, |
| 16 |
private readonly ?ColorInterface $foregroundColor, |
| 17 |
private readonly ?Gradient $foregroundGradient, |
| 18 |
private readonly EyeFill $topLeftEyeFill, |
| 19 |
private readonly EyeFill $topRightEyeFill, |
| 20 |
private readonly EyeFill $bottomLeftEyeFill |
| 21 |
) { |
| 22 |
} |
| 23 |
|
| 24 |
public static function default() : self |
| 25 |
{ |
| 26 |
return self::$default ?: self::$default = self::uniformColor(new Gray(100), new Gray(0)); |
| 27 |
} |
| 28 |
|
| 29 |
public static function withForegroundColor( |
| 30 |
ColorInterface $backgroundColor, |
| 31 |
ColorInterface $foregroundColor, |
| 32 |
EyeFill $topLeftEyeFill, |
| 33 |
EyeFill $topRightEyeFill, |
| 34 |
EyeFill $bottomLeftEyeFill |
| 35 |
) : self { |
| 36 |
return new self( |
| 37 |
$backgroundColor, |
| 38 |
$foregroundColor, |
| 39 |
null, |
| 40 |
$topLeftEyeFill, |
| 41 |
$topRightEyeFill, |
| 42 |
$bottomLeftEyeFill |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
public static function withForegroundGradient( |
| 47 |
ColorInterface $backgroundColor, |
| 48 |
Gradient $foregroundGradient, |
| 49 |
EyeFill $topLeftEyeFill, |
| 50 |
EyeFill $topRightEyeFill, |
| 51 |
EyeFill $bottomLeftEyeFill |
| 52 |
) : self { |
| 53 |
return new self( |
| 54 |
$backgroundColor, |
| 55 |
null, |
| 56 |
$foregroundGradient, |
| 57 |
$topLeftEyeFill, |
| 58 |
$topRightEyeFill, |
| 59 |
$bottomLeftEyeFill |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
public static function uniformColor(ColorInterface $backgroundColor, ColorInterface $foregroundColor) : self |
| 64 |
{ |
| 65 |
return new self( |
| 66 |
$backgroundColor, |
| 67 |
$foregroundColor, |
| 68 |
null, |
| 69 |
EyeFill::inherit(), |
| 70 |
EyeFill::inherit(), |
| 71 |
EyeFill::inherit() |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
public static function uniformGradient(ColorInterface $backgroundColor, Gradient $foregroundGradient) : self |
| 76 |
{ |
| 77 |
return new self( |
| 78 |
$backgroundColor, |
| 79 |
null, |
| 80 |
$foregroundGradient, |
| 81 |
EyeFill::inherit(), |
| 82 |
EyeFill::inherit(), |
| 83 |
EyeFill::inherit() |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
public function hasGradientFill() : bool |
| 88 |
{ |
| 89 |
return null !== $this->foregroundGradient; |
| 90 |
} |
| 91 |
|
| 92 |
public function getBackgroundColor() : ColorInterface |
| 93 |
{ |
| 94 |
return $this->backgroundColor; |
| 95 |
} |
| 96 |
|
| 97 |
public function getForegroundColor() : ColorInterface |
| 98 |
{ |
| 99 |
if (null === $this->foregroundColor) { |
| 100 |
throw new RuntimeException('Fill uses a gradient, thus no foreground color is available'); |
| 101 |
} |
| 102 |
|
| 103 |
return $this->foregroundColor; |
| 104 |
} |
| 105 |
|
| 106 |
public function getForegroundGradient() : Gradient |
| 107 |
{ |
| 108 |
if (null === $this->foregroundGradient) { |
| 109 |
throw new RuntimeException('Fill uses a single color, thus no foreground gradient is available'); |
| 110 |
} |
| 111 |
|
| 112 |
return $this->foregroundGradient; |
| 113 |
} |
| 114 |
|
| 115 |
public function getTopLeftEyeFill() : EyeFill |
| 116 |
{ |
| 117 |
return $this->topLeftEyeFill; |
| 118 |
} |
| 119 |
|
| 120 |
public function getTopRightEyeFill() : EyeFill |
| 121 |
{ |
| 122 |
return $this->topRightEyeFill; |
| 123 |
} |
| 124 |
|
| 125 |
public function getBottomLeftEyeFill() : EyeFill |
| 126 |
{ |
| 127 |
return $this->bottomLeftEyeFill; |
| 128 |
} |
| 129 |
} |