Fill.php 4.0 KB

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