diff --git a/docs/helpful-guides/advance-usage.md b/docs/helpful-guides/advance-usage.md index b0dd225..4bc00c5 100644 --- a/docs/helpful-guides/advance-usage.md +++ b/docs/helpful-guides/advance-usage.md @@ -46,5 +46,60 @@ $qrCode = (new QrCode('https://2am.tech')) ->setScaleLogoHeight(false); ``` +### Gradient Foreground + +You can easily change from uniform to gradient color by setting +the range limit for you gradient for using the method `setForegroundEndColor`. +When you do this, the color you've set for the foreground using `setForegroundColor` +function will be taken as the start point for the gradient color. + +```PHP +$qrCode = (new QrCode('https://2am.tech')) + ->setForegroundColor(0, 255, 0, 70) + ->setForegroundEndColor(0, 0, 255, 50); +``` + +By default, it will render the gradient color using a vertical orientation. +You can change it by the `setGradientType` function. It takes one parameter +to determine witch gradient type must be set. +The available types are: + +* \Da\QrCode\Contracts\ColorsInterface::GRADIENT_VERTICAL +* \Da\QrCode\Contracts\ColorsInterface::GRADIENT_HORIZONTAL +* \Da\QrCode\Contracts\ColorsInterface::GRADIENT_RADIAL +* \Da\QrCode\Contracts\ColorsInterface::GRADIENT_DIAGONAL +* \Da\QrCode\Contracts\ColorsInterface::GRADIENT_INVERSE_DIAGONAL + +```PHP +$qrCode = (new QrCode('https://2am.tech')) + ->setForegroundColor(0, 255, 0, 70) + ->setForegroundEndColor(0, 0, 255, 50) + ->setGradientType(\Da\QrCode\Contracts\ColorsInterface::GRADIENT_DIAGONAL); +``` + +### Foreground Path Style +It is possible to change the foreground path style by using the `setPathStyle` +function. The default style will be the square pattern, and the available +styles are the following: + +* \Da\QrCode\Contracts\PathStyleInterface::SQUARE; +* \Da\QrCode\Contracts\PathStyleInterface::DOTS; +* \Da\QrCode\Contracts\PathStyleInterface::ROUNDED; + +```PHP +$qrCode = (new QrCode('https://2am.tech')) + ->setPathStyle(\Da\QrCode\Contracts\PathStyleInterface::ROUNDED); +``` + +You can also set the intensity for the pattern appliance: + +```PHP +$qrCode = (new QrCode('https://2am.tech')) + ->setPathStyle(\Da\QrCode\Contracts\PathStyleInterface::ROUNDED) + ->setPathIntensity(0.7); +``` + +The default value for the intensity is 1. It must be a number between 0 and 1, +otherwise an exception will be thrown. © [2amigos](https://2am.tech/) 2013-2023 diff --git a/src/Contracts/ColorsInterface.php b/src/Contracts/ColorsInterface.php new file mode 100644 index 0000000..9a44979 --- /dev/null +++ b/src/Contracts/ColorsInterface.php @@ -0,0 +1,78 @@ + 0, - 'g' => 0, - 'b' => 0, - 'a' => 100, - ]; - /** - * @var array - */ - protected $backgroundColor = [ - 'r' => 255, - 'g' => 255, - 'b' => 255, - 'a' => 100, - ]; /** * @var string */ @@ -79,6 +63,11 @@ class QrCode implements QrCodeInterface */ protected $writer; + /** + * @var StyleManager + */ + protected $styleManager; + /** * QrCode constructor. * @@ -91,6 +80,13 @@ public function __construct(string $text, string $errorCorrectionLevel = null, W $this->text = $text; $this->errorCorrectionLevel = $errorCorrectionLevel ?: ErrorCorrectionLevelInterface::LOW; $this->writer = $writer ?: new PngWriter(); + + $canApplyAlpha = ! $this->writer instanceof EpsWriter; + + $this->styleManager = new StyleManager( + StyleManager::buildColor(0, 0, 0, 100, $canApplyAlpha), + StyleManager::buildColor(255, 255, 255, 100, $canApplyAlpha) + ); } /** @@ -102,12 +98,47 @@ public function __construct(string $text, string $errorCorrectionLevel = null, W */ public function setForegroundColor(int $red, int $green, int $blue, int $alpha = 100): self { - $this->foregroundColor = [ - 'r' => $red, - 'g' => $green, - 'b' => $blue, - 'a' => $alpha, - ]; + $color = StyleManager::buildColor( + $red, + $green, + $blue, + $alpha, + ! $this->getWriter() instanceof EpsWriter + ); + + $this->styleManager->setForegroundColor($color); + + return $this; + } + + /** + * @param int $red + * @param int $green + * @param int $blue + * + * @return $this + */ + public function setForegroundEndColor(int $red, int $green, int $blue, int $alpha = 100): self + { + $color = StyleManager::buildColor( + $red, + $green, + $blue, + $alpha, + !$this->getWriter() instanceof EpsWriter + ); + + $this->styleManager->setForegroundEndColor($color); + + return $this; + } + + /** + * @return $this + */ + public function unsetForegroundEndColor(): self + { + $this->styleManager->unsetForegroundEndColor(); return $this; } @@ -121,16 +152,67 @@ public function setForegroundColor(int $red, int $green, int $blue, int $alpha = */ public function setBackgroundColor(int $red, int $green, int $blue): self { - $this->backgroundColor = [ - 'r' => $red, - 'g' => $green, - 'b' => $blue, - 'a' => 100, - ]; + $color = StyleManager::buildColor( + $red, + $green, + $blue, + 100, + ! $this->getWriter() instanceof EpsWriter + ); + + $this->styleManager->setBackgroundColor($color); + + return $this; + } + + /** + * @param string $style + * @return $this + */ + public function setPathStyle(string $style): self + { + $this->styleManager->setPathStyle($style); + + return $this; + } + + /** + * @param float $intensity + * @return $this + */ + public function setPathIntensity(float $intensity): self + { + $this->styleManager->setIntensity($intensity); return $this; } + /** + * @return StyleManager + */ + public function getStyleManager() + { + return $this->styleManager; + } + + /** + * @return float + */ + public function getPathIntensity(): float + { + return $this->styleManager->getIntensity(); + } + + /** + * @return string + */ + public function getGradientType(): string + { + return $this + ->styleManager + ->getGradientTye(); + } + /** * @param string $path * @@ -169,6 +251,21 @@ public function setWriter(WriterInterface $writer): self { $this->writer = $writer; + if ($writer instanceof EpsWriter) { + $this->styleManager->forceUniformRgbColors(); + } + + return $this; + } + + /** + * @param string $type + * @return $this + */ + public function setGradientType(string $type): self + { + $this->styleManager->setGradientType($type); + return $this; } @@ -292,7 +389,40 @@ public function getMargin(): int */ public function getForegroundColor(): array { - return $this->foregroundColor; + $color = $this->styleManager->getForegroundColor(); + $rgb = $color->toRgb(); + + return [ + 'r' => $rgb->getRed(), + 'g' => $rgb->getGreen(), + 'b' => $rgb->getBlue(), + 'a' => $color instanceof Alpha + ? $color->getAlpha() + : 100 + ]; + } + + /** + * @inheritdoc + */ + public function getForegroundEndColor() + { + $color = $this->styleManager->getForegroundEndColor(); + + if (is_null($color)) { + return null; + } + + $rgb = $color->toRgb(); + + return [ + 'r' => $rgb->getRed(), + 'g' => $rgb->getGreen(), + 'b' => $rgb->getBlue(), + 'a' => $color instanceof Alpha + ? $color->getAlpha() + : 100 + ]; } /** @@ -300,7 +430,14 @@ public function getForegroundColor(): array */ public function getBackgroundColor(): array { - return $this->backgroundColor; + $color = $this->styleManager->getBackgroundColor(); + $rgb = $color->toRgb(); + + return [ + 'r' => $rgb->getRed(), + 'g' => $rgb->getGreen(), + 'b' => $rgb->getBlue() + ]; } /** @@ -382,7 +519,7 @@ public function writeFile(string $path) /** * @return bool */ - public function getScaleLogoHeight(): bool + public function isScaleLogoHeight(): bool { return $this->scaleLogoHeight; } diff --git a/src/StyleManager.php b/src/StyleManager.php new file mode 100644 index 0000000..68b1515 --- /dev/null +++ b/src/StyleManager.php @@ -0,0 +1,274 @@ +setForegroundColor($foregroundColor); + $this->setBackgroundColor($backgroundColor); + + $this->pathStyle = $pathStyle ?: PathStyleInterface::SQUARE; + $this->styleIntensity = $styleIntensity ?: 1; + $this->gradientType = $gradientType ?: ColorsInterface::GRADIENT_VERTICAL; + } + + /** + * @param $color + * @return void + * @throws Exception + */ + public function setForegroundColor($color): void + { + if (! $color instanceof ColorInterface) { + throw new Exception('Invalid type. Variable `color` should be instance of ' . ColorInterface::class); + } + + $this->foregroundColor = $color; + } + + public function getForegroundColor() + { + return $this->foregroundColor; + } + + public function getForegroundEndColor() + { + return $this->foregroundEndColor; + } + + /** + * @param Alpha|Rgb $color + * @return void + * @throws \Exception + */ + public function setForegroundEndColor($color): void + { + if (! $color instanceof ColorInterface) { + throw new Exception('Invalid type. Variable `color` should be instance of ' . ColorInterface::class); + } + + $this->foregroundEndColor = $color; + } + + /** + * @return void + */ + public function unsetForegroundEndColor(): void + { + $this->foregroundEndColor = null; + } + + public function setBackgroundColor($color): void + { + if (! $color instanceof ColorInterface) { + throw new Exception('Invalid type. Variable `color` should be instance of ' . ColorInterface::class); + } + + $this->backgroundColor = $color; + } + + /** + * @return Alpha|Rgb|Gradient + */ + public function getBackgroundColor() + { + return $this->backgroundColor; + } + + /** + * @param string $pathStyle + * @return void + */ + public function setPathStyle(string $pathStyle): void + { + $this->pathStyle = $pathStyle; + } + + /** + * @return string + */ + public function getPathStyle(): string + { + return $this->pathStyle; + } + + /** + * @param float $intensity + * @return void + */ + public function setIntensity(float $intensity): void + { + $this->styleIntensity = $intensity; + } + + /** + * @return float + */ + public function getIntensity(): float + { + return $this->styleIntensity; + } + + /** + * @param string $type + * @return void + */ + public function setGradientType(string $type): void + { + $this->gradientType = $type; + } + + /** + * @return GradientType + */ + public function getGradientTye() + { + switch ($this->gradientType) { + case ColorsInterface::GRADIENT_VERTICAL: { + return GradientType::VERTICAL(); + } + case ColorsInterface::GRADIENT_DIAGONAL: { + return GradientType::DIAGONAL(); + } + case ColorsInterface::GRADIENT_INVERSE_DIAGONAL: { + return GradientType::INVERSE_DIAGONAL(); + } + case ColorsInterface::GRADIENT_HORIZONTAL: { + return GradientType::HORIZONTAL(); + } + case ColorsInterface::GRADIENT_RADIAL: { + return GradientType::RADIAL(); + } + default: return GradientType::VERTICAL(); + } + } + + /** + * @return void + */ + public function forceUniformRgbColors(): void + { + $this->unsetForegroundEndColor(); + + $this->foregroundColor = $this->foregroundColor instanceof Alpha + ? $this->foregroundColor->getBaseColor() + : $this->foregroundColor; + + $this->backgroundColor = $this->backgroundColor instanceof Alpha + ? $this->backgroundColor->getBaseColor() + : $this->backgroundColor; + } + + /** + * @param $red + * @param $green + * @param $blue + * @param $alpha + * @return Alpha|Rgb + */ + public static function buildColor($red, $green, $blue, $alpha = 100, $applyAlpha = true) + { + if ($applyAlpha) { + return new Alpha($alpha, new Rgb( + $red, + $green, + $blue, + )); + } + + return new Rgb($red, $green, $blue); + } + + /** + * @return Fill + */ + public function buildFillColor() + { + if (! is_null($this->foregroundEndColor)) { + return Fill::uniformGradient( + $this->getBackgroundColor(), + new Gradient( + $this->getForegroundColor(), + $this->getForegroundEndColor(), + $this->getGradientTye(), + ) + ); + } + else { + return Fill::uniformColor( + $this->getBackgroundColor(), + $this->getForegroundColor() + ); + } + } + + /** + * @return ModuleInterface + */ + public function buildModule() + { + switch ($this->getPathStyle()) { + case PathStyleInterface::DOTS: return new DotsModule($this->getIntensity()); + case PathStyleInterface::ROUNDED: return new RoundnessModule($this->getIntensity()); + default: return SquareModule::instance(); + } + } + + /** + * @return ModuleEye + */ + public function buildEye() + { + return new ModuleEye($this->buildModule()); + } +} \ No newline at end of file diff --git a/src/Traits/ImageTrait.php b/src/Traits/ImageTrait.php index 95512e4..ec7a145 100644 --- a/src/Traits/ImageTrait.php +++ b/src/Traits/ImageTrait.php @@ -11,8 +11,6 @@ namespace Da\QrCode\Traits; -use BaconQrCode\Renderer\RendererStyle\RendererStyle; -use BaconQrCode\Renderer\ImageRenderer; use BaconQrCode\Writer; use Da\QrCode\Contracts\LabelInterface; use Da\QrCode\Contracts\QrCodeInterface; @@ -45,14 +43,7 @@ public function validateResult(bool $validate): self */ public function writeString(QrCodeInterface $qrCode): string { - $fill = $this->buildQrCodeFillColor($qrCode); - - $rendererStyle = new RendererStyle($qrCode->getSize(), 0, null, null, $fill); - - $renderer = new ImageRenderer( - $rendererStyle, - $this->renderBackEnd - ); + $renderer = $this->buildRenderer($qrCode); $writer = new Writer($renderer); $string = $writer->writeString( @@ -75,7 +66,7 @@ public function writeString(QrCodeInterface $qrCode): string $image, $qrCode->getLogoPath(), $qrCode->getLogoWidth(), - $qrCode->getScaleLogoHeight() + $qrCode->isScaleLogoHeight() ); } diff --git a/src/Writer/AbstractWriter.php b/src/Writer/AbstractWriter.php index c4df8bd..8542d3a 100644 --- a/src/Writer/AbstractWriter.php +++ b/src/Writer/AbstractWriter.php @@ -12,10 +12,9 @@ namespace Da\QrCode\Writer; use BaconQrCode\Common\ErrorCorrectionLevel; -use BaconQrCode\Renderer\Color\Alpha; -use BaconQrCode\Renderer\Color\Rgb; use BaconQrCode\Renderer\Image\ImageBackEndInterface; -use BaconQrCode\Renderer\RendererStyle\Fill; +use BaconQrCode\Renderer\ImageRenderer; +use BaconQrCode\Renderer\RendererStyle\RendererStyle; use Da\QrCode\Contracts\QrCodeInterface; use Da\QrCode\Contracts\WriterInterface; use Da\QrCode\Dto\LogoDto; @@ -39,22 +38,6 @@ protected function __construct(ImageBackEndInterface $renderBackEnd) $this->renderBackEnd = $renderBackEnd; } - /** - * @param QrCodeInterface $qrCode - * @return Fill - */ - protected function buildQrCodeFillColor(QrCodeInterface $qrCode): Fill - { - $background = $qrCode->getBackgroundColor(); - $foreground = $qrCode->getForegroundColor(); - $isAlphaColor = ! $qrCode->getWriter() instanceof EpsWriter; - - return Fill::uniformColor( - $this->convertColor($background, $isAlphaColor), - $this->convertColor($foreground, $isAlphaColor), - ); - } - /** * @inheritdoc */ @@ -80,22 +63,6 @@ public function getName(): string return strtolower(str_replace('Writer', '', (new ReflectionClass($this))->getShortName())); } - /** - * @param array $color - * - * @return Alpha|Rgb - */ - protected function convertColor(array $color, bool $isAlphaColor = true) - { - $baseColor = new Rgb($color['r'], $color['g'], $color['b']); - - if ($isAlphaColor) { - return new Alpha($color['a'], $baseColor); - } - - return $baseColor; - } - /** * @param string $errorCorrectionLevel * @@ -131,4 +98,35 @@ protected function transformLogo($logoPath, $logoWidth = null, $scale = false) return LogoDto::create($logoImage, $logoSourceWidth, $logoSourceHeight, $logoTargetWidth, $logoTargetHeight); } + + /** + * @param QrCodeInterface $qrCode + * @return RendererStyle + */ + protected function getRendererStyle(QrCodeInterface $qrCode) + { + $margin = $qrCode->getWriter() instanceof EpsWriter + ? $qrCode->getMargin() + : 0; + + return new RendererStyle( + $qrCode->getSize(), + $margin, + $qrCode->getStyleManager()->buildModule(), + $qrCode->getStyleManager()->buildEye(), + $qrCode->getStyleManager()->buildFillColor() + ); + } + + /** + * @param QrCodeInterface $qrCode + * @return ImageRenderer + */ + protected function buildRenderer(QrCodeInterface $qrCode) + { + return new ImageRenderer( + $this->getRendererStyle($qrCode), + $this->renderBackEnd + ); + } } diff --git a/src/Writer/EpsWriter.php b/src/Writer/EpsWriter.php index f4c9aa8..89d1308 100644 --- a/src/Writer/EpsWriter.php +++ b/src/Writer/EpsWriter.php @@ -32,13 +32,7 @@ public function __construct() */ public function writeString(QrCodeInterface $qrCode): string { - $fill = $this->buildQrCodeFillColor($qrCode); - $rendererStyle = new RendererStyle($qrCode->getSize(), $qrCode->getMargin(), null, null, $fill); - - $renderer = new ImageRenderer( - $rendererStyle, - $this->renderBackEnd - ); + $renderer = $this->buildRenderer($qrCode);; $writer = new Writer($renderer); diff --git a/src/Writer/SvgWriter.php b/src/Writer/SvgWriter.php index 28a4f8d..d192cb8 100644 --- a/src/Writer/SvgWriter.php +++ b/src/Writer/SvgWriter.php @@ -14,7 +14,6 @@ use BaconQrCode\Encoder\Encoder; use BaconQrCode\Renderer\Image\SvgImageBackEnd; use BaconQrCode\Renderer\ImageRenderer; -use BaconQrCode\Renderer\RendererStyle\RendererStyle; use BaconQrCode\Writer; use Da\QrCode\Contracts\LabelInterface; use Da\QrCode\Contracts\QrCodeInterface; @@ -37,14 +36,7 @@ public function __construct() */ public function writeString(QrCodeInterface $qrCode): string { - $fill = $this->buildQrCodeFillColor($qrCode); - - $rendererStyle = new RendererStyle($qrCode->getSize(), 0, null, null, $fill); - - $renderer = new ImageRenderer( - $rendererStyle, - $this->renderBackEnd - ); + $renderer = $this->buildRenderer($qrCode); $writer = new Writer($renderer); @@ -64,7 +56,7 @@ public function writeString(QrCodeInterface $qrCode): string $qrCode, $qrCode->getLogoPath(), $qrCode->getLogoWidth(), - $qrCode->getScaleLogoHeight() + $qrCode->isScaleLogoHeight() ); } diff --git a/tests/_data/colors/gradient.jpg b/tests/_data/colors/gradient.jpg new file mode 100644 index 0000000..46f92cd Binary files /dev/null and b/tests/_data/colors/gradient.jpg differ diff --git a/tests/_data/colors/gradient.png b/tests/_data/colors/gradient.png new file mode 100644 index 0000000..c539ca2 Binary files /dev/null and b/tests/_data/colors/gradient.png differ diff --git a/tests/_data/colors/gradient.svg b/tests/_data/colors/gradient.svg new file mode 100644 index 0000000..b8de3bc --- /dev/null +++ b/tests/_data/colors/gradient.svg @@ -0,0 +1,2 @@ + + diff --git a/tests/_data/colors/gradient2.png b/tests/_data/colors/gradient2.png new file mode 100644 index 0000000..32920c9 Binary files /dev/null and b/tests/_data/colors/gradient2.png differ diff --git a/tests/_data/colors/gradient3.png b/tests/_data/colors/gradient3.png new file mode 100644 index 0000000..2d81a2d Binary files /dev/null and b/tests/_data/colors/gradient3.png differ diff --git a/tests/_data/colors/gradient4.png b/tests/_data/colors/gradient4.png new file mode 100644 index 0000000..625a09e Binary files /dev/null and b/tests/_data/colors/gradient4.png differ diff --git a/tests/_data/colors/uniform.eps b/tests/_data/colors/uniform.eps new file mode 100644 index 0000000..2e6b683 --- /dev/null +++ b/tests/_data/colors/uniform.eps @@ -0,0 +1 @@ +data:image/eps;base64,JSFQUy1BZG9iZS0zLjAgRVBTRi0zLjAKJSVDcmVhdG9yOiBCYWNvblFyQ29kZQolJUJvdW5kaW5nQm94OiAwIDAgMzAwIDMwMCAKJSVCZWdpblByb2xvZwpzYXZlCjUwIGRpY3QgYmVnaW4KL3EgeyBnc2F2ZSB9IGJpbmQgZGVmCi9RIHsgZ3Jlc3RvcmUgfSBiaW5kIGRlZgovcyB7IHNjYWxlIH0gYmluZCBkZWYKL3QgeyB0cmFuc2xhdGUgfSBiaW5kIGRlZgovciB7IHJvdGF0ZSB9IGJpbmQgZGVmCi9uIHsgbmV3cGF0aCB9IGJpbmQgZGVmCi9tIHsgbW92ZXRvIH0gYmluZCBkZWYKL2wgeyBsaW5ldG8gfSBiaW5kIGRlZgovYyB7IGN1cnZldG8gfSBiaW5kIGRlZgoveiB7IGNsb3NlcGF0aCB9IGJpbmQgZGVmCi9mIHsgZW9maWxsIH0gYmluZCBkZWYKL3JnYiB7IHNldHJnYmNvbG9yIH0gYmluZCBkZWYKL2NteWsgeyBzZXRjbXlrY29sb3IgfSBiaW5kIGRlZgovZ3JheSB7IHNldGdyYXkgfSBiaW5kIGRlZgolJUVuZFByb2xvZwoxIC0xIHMKMCAtMzAwIHQKMCAwIG0gMzAwIDAgbCAzMDAgMzAwIGwgMCAzMDA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BsIDcgMTMgbCB6IDE3IDE0IG0gMTcgMTUgbCAxOSAxNSBsIDE5IDE0IGwgeiAxNyAxNyBtIDE3IDIwCiBsIDIwIDIwIGwgMjAgMTcgbCB6IDE4IDE4IG0gMTggMTkgbCAxOSAxOSBsIDE5IDE4IGwgeiAxMSAyMSBtIDExIDI0IGwgMTIgMjQKIGwgMTIgMjMgbCAxMyAyMyBsIDEzIDIyIGwgMTIgMjIgbCAxMiAyMSBsIHogMTggMjEgbSAxOCAyMyBsIDE5IDIzIGwgMTkgMjEgbAogeiAyMCAyMiBtIDIwIDIzIGwgMjIgMjMgbCAyMiAyNCBsIDIzIDI0IGwgMjMgMjMgbCAyNCAyMyBsIDI0IDIyIGwgeiAwIDAgbSAwCiA3IGwgNyA3IGwgNyAwIGwgeiAxIDEgbSAxIDYgbCA2IDYgbCA2IDEgbCB6IDIgMiBtIDIgNSBsIDUgNSBsIDUgMiBsIHogMTggMAogbSAxOCA3IGwgMjUgNyBsIDI1IDAgbCB6IDE5IDEgbSAxOSA2IGwgMjQgNiBsIDI0IDEgbCB6IDIwIDIgbSAyMCA1IGwgMjMgNSBsCiAyMyAyIGwgeiAwIDE4IG0gMCAyNSBsIDcgMjUgbCA3IDE4IGwgeiAxIDE5IG0gMSAyNCBsIDYgMjQgbCA2IDE5IGwgeiAyIDIwIG0KIDIgMjMgbCA1IDIzIGwgNSAyMCBsIHogMCAwIDAgcmdiIGYKJSVUUkFJTEVSCmVuZCByZXN0b3JlCiUlRU9G \ No newline at end of file diff --git a/tests/_data/colors/uniform2.eps b/tests/_data/colors/uniform2.eps new file mode 100644 index 0000000..2e6b683 --- /dev/null +++ b/tests/_data/colors/uniform2.eps @@ -0,0 +1 @@ +data:image/eps;base64,JSFQUy1BZG9iZS0zLjAgRVBTRi0zLjAKJSVDcmVhdG9yOiBCYWNvblFyQ29kZQolJUJvdW5kaW5nQm94OiAwIDAgMzAwIDMwMCAKJSVCZWdpblByb2xvZwpzYXZlCjUwIGRpY3QgYmV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IGwgMjUgNyBsIDI1IDAgbCB6IDE5IDEgbSAxOSA2IGwgMjQgNiBsIDI0IDEgbCB6IDIwIDIgbSAyMCA1IGwgMjMgNSBsCiAyMyAyIGwgeiAwIDE4IG0gMCAyNSBsIDcgMjUgbCA3IDE4IGwgeiAxIDE5IG0gMSAyNCBsIDYgMjQgbCA2IDE5IGwgeiAyIDIwIG0KIDIgMjMgbCA1IDIzIGwgNSAyMCBsIHogMCAwIDAgcmdiIGYKJSVUUkFJTEVSCmVuZCByZXN0b3JlCiUlRU9G \ No newline at end of file diff --git a/tests/_data/path/dots.png b/tests/_data/path/dots.png new file mode 100644 index 0000000..f3ada15 Binary files /dev/null and b/tests/_data/path/dots.png differ diff --git a/tests/_data/path/dots2.png b/tests/_data/path/dots2.png new file mode 100644 index 0000000..7c820a1 Binary files /dev/null and b/tests/_data/path/dots2.png differ diff --git a/tests/_data/path/rounded.png b/tests/_data/path/rounded.png new file mode 100644 index 0000000..91fa1b4 Binary files /dev/null and b/tests/_data/path/rounded.png differ diff --git a/tests/_data/path/rounded2.png b/tests/_data/path/rounded2.png new file mode 100644 index 0000000..402e215 Binary files /dev/null and b/tests/_data/path/rounded2.png differ diff --git a/tests/unit/ColorsTest.php b/tests/unit/ColorsTest.php new file mode 100644 index 0000000..0465ba7 --- /dev/null +++ b/tests/unit/ColorsTest.php @@ -0,0 +1,159 @@ +writeDataUri(); + + $eps2 = (new QrCode('2am Technologies')) + ->setWriter(new EpsWriter()) + ->writeDataUri(); + file_put_contents(codecept_data_dir('colors/uniform.eps'), $eps); + file_put_contents(codecept_data_dir('colors/uniform2.eps'), $eps2); + $this->assertEquals( + $this->normalizeString(file_get_contents(codecept_data_dir('colors/uniform.eps'))), + $this->normalizeString($eps) + ); + + $this->assertEquals( + $this->normalizeString(file_get_contents(codecept_data_dir('colors/uniform2.eps'))), + $this->normalizeString($eps2) + ); + } + + public function testGradientColors() + { + $png = (new QrCode('2am Technologies')) + ->setWriter(new PngWriter()) + ->setForegroundColor(0, 255, 0,75) + ->setForegroundEndColor(0, 0, 255,50) + ->setBackgroundColor(200, 200, 200) + ->setGradientType('x') + ->writeString(); + + $jpg = (new QrCode('2am Technologies')) + ->setWriter(new JpgWriter()) + ->setForegroundColor(0, 255, 0,25) + ->setForegroundEndColor(0, 0, 255,75) + ->setBackgroundColor(200, 200, 200) + ->setGradientType(ColorsInterface::GRADIENT_DIAGONAL) + ->writeString(); + + $svg = (new QrCode('2am Technologies')) + ->setWriter(new SvgWriter()) + ->setForegroundColor(0, 255, 0,25) + ->setForegroundEndColor(0, 0, 255,95) + ->setBackgroundColor(200, 200, 200) + ->setGradientType(ColorsInterface::GRADIENT_RADIAL) + ->writeString(); + + $png2 = (new QrCode('2am Technologies')) + ->setWriter(new PngWriter()) + ->setForegroundColor(0, 255, 0,80) + ->setForegroundEndColor(0, 0, 255,50) + ->setBackgroundColor(200, 200, 200) + ->setGradientType(ColorsInterface::GRADIENT_INVERSE_DIAGONAL) + ->writeString(); + + $png3 = (new QrCode('2am Technologies')) + ->setWriter(new PngWriter()) + ->setForegroundColor(0, 255, 0,75) + ->setForegroundEndColor(0, 0, 255,100) + ->setBackgroundColor(200, 200, 200) + ->setGradientType(ColorsInterface::GRADIENT_HORIZONTAL) + ->writeString(); + + $png4 = (new QrCode('2am Technologies')) + ->setWriter(new PngWriter()) + ->setForegroundColor(0, 255, 0,75) + ->setForegroundEndColor(0, 0, 255,100) + ->setBackgroundColor(200, 200, 200) + ->setGradientType(ColorsInterface::GRADIENT_VERTICAL) + ->writeString(); + + $this->assertEquals( + $this->normalizeString(file_get_contents(codecept_data_dir('colors/gradient.png'))), + $this->normalizeString($png) + ); + + $this->assertEquals( + $this->normalizeString(file_get_contents(codecept_data_dir('colors/gradient.jpg'))), + $this->normalizeString($jpg) + ); + + $this->assertEquals( + $this->normalizeString(file_get_contents(codecept_data_dir('colors/gradient.svg'))), + $this->normalizeString($svg) + ); + + $this->assertEquals( + $this->normalizeString(file_get_contents(codecept_data_dir('colors/gradient2.png'))), + $this->normalizeString($png2) + ); + + $this->assertEquals( + $this->normalizeString(file_get_contents(codecept_data_dir('colors/gradient3.png'))), + $this->normalizeString($png3) + ); + + $this->assertEquals( + $this->normalizeString(file_get_contents(codecept_data_dir('colors/gradient4.png'))), + $this->normalizeString($png4) + ); + } + + public function testInvalidForegroundColorShouldThrowException() + { + $this->expectException(\Exception::class); + + new StyleManager('x', 'y'); + } + + public function testInvalidForegroundEndColorShouldThrowException() + { + $this->expectException(\Exception::class); + + (new StyleManager( + new Rgb(0,0,0), new Rgb(255,255,255) + ))->setForegroundEndColor('x'); + } + + public function testInvalidBackgroundColorShouldThrowException() + { + $this->expectException(\Exception::class); + + (new StyleManager( + new Rgb(0,0,0), new Rgb(255,255,255) + ))->setBackgroundColor('x'); + } + + public function testForceRgb() + { + $this->expectNotToPerformAssertions(); + + (new StyleManager(new Rgb(0,0,0), new Rgb(255,255,255))) + ->forceUniformRgbColors(); + } + + protected function normalizeString($string) + { + return str_replace( + "\r\n", "\n", str_replace( + " ", "", $string + ) + ); + } +} \ No newline at end of file diff --git a/tests/unit/PathTest.php b/tests/unit/PathTest.php new file mode 100644 index 0000000..223e352 --- /dev/null +++ b/tests/unit/PathTest.php @@ -0,0 +1,60 @@ +setPathStyle(\Da\QrCode\Contracts\PathStyleInterface::ROUNDED) + ->writeString(); + file_put_contents(codecept_data_dir('path/rounded.png'), $rounded); + $rounded2 = (new \Da\QrCode\QrCode('2am Technologies')) + ->setPathStyle(\Da\QrCode\Contracts\PathStyleInterface::ROUNDED) + ->setPathIntensity(0.7) + ->writeString(); + file_put_contents(codecept_data_dir('path/rounded2.png'), $rounded2); + $this->assertEquals( + $this->normalizeString(file_get_contents(codecept_data_dir('path/rounded.png'))), + $this->normalizeString($rounded) + ); + + $this->assertEquals( + $this->normalizeString(file_get_contents(codecept_data_dir('path/rounded2.png'))), + $this->normalizeString($rounded2) + ); + } + + public function testDotsPath() + { + $dots = (new \Da\QrCode\QrCode('2am Technologies')) + ->setPathStyle(\Da\QrCode\Contracts\PathStyleInterface::DOTS) + ->writeString(); + file_put_contents(codecept_data_dir('path/dots.png'), $dots); + + $dots2 = (new \Da\QrCode\QrCode('2am Technologies')) + ->setPathStyle(\Da\QrCode\Contracts\PathStyleInterface::DOTS) + ->setPathIntensity(0.7) + ->writeString(); + file_put_contents(codecept_data_dir('path/dots2.png'), $dots2); + + $this->assertEquals( + $this->normalizeString(file_get_contents(codecept_data_dir('path/dots.png'))), + $this->normalizeString($dots) + ); + + $this->assertEquals( + $this->normalizeString(file_get_contents(codecept_data_dir('path/dots2.png'))), + $this->normalizeString($dots2) + ); + } + + protected function normalizeString($string) + { + return str_replace( + "\r\n", "\n", str_replace( + " ", "", $string + ) + ); + } +} \ No newline at end of file diff --git a/tests/unit/QrCodeTest.php b/tests/unit/QrCodeTest.php index c1bf603..bcdadfb 100644 --- a/tests/unit/QrCodeTest.php +++ b/tests/unit/QrCodeTest.php @@ -1,5 +1,6 @@ tester->assertEquals( $this->normalizeString(file_get_contents(codecept_data_dir('data.eps'))), - $out + $this->normalizeString($out) ); } @@ -103,12 +104,12 @@ public function testSetOutputFormat() $eps = (new QrCode('https://2am.tech')) ->setWriter(new \Da\QrCode\Writer\EpsWriter()) ->writeString(); - + file_put_contents(codecept_data_dir('writers/qrcode.eps'), $this->normalizeString($eps)); $this->tester->assertEquals(file_get_contents(codecept_data_dir('writers/qrcode.png')), $png); $this->tester->assertEquals(file_get_contents(codecept_data_dir('writers/qrcode.jpg')), $jpeg); $this->tester->assertEquals( $this->normalizeString(file_get_contents(codecept_data_dir('writers/qrcode.eps'))), - $eps + $this->normalizeString($eps) ); $this->tester->assertEquals( $this->normalizeString(file_get_contents(codecept_data_dir('writers/qrcode.svg'))), @@ -272,6 +273,34 @@ public function testScaleLogoSvg() ); } + public function testUnsetForegroundEndColor() + { + $qrCode = (new QrCode('2am Technologies')) + ->setForegroundEndColor(255, 255, 255); + + $color = $qrCode->getForegroundEndColor(); + + $this->assertIsArray($color); + $this->assertEquals($color['r'], 255); + $this->assertEquals($color['g'], 255); + $this->assertEquals($color['b'], 255); + + $qrCode->unsetForegroundEndColor(); + $this->assertNull($qrCode->getForegroundEndColor()); + } + + public function testGetPathIntensity() + { + $qrCode = (new QrCode('2am Technologies')); + $this->assertEquals($qrCode->getPathIntensity(), 1); + } + + public function testGetGradientType() + { + $qrCode = (new QrCode('2am Technologies')); + $this->assertEquals($qrCode->getGradientType(), GradientType::VERTICAL()); + } + protected function normalizeString($string) { return str_replace(