Skip to content

Commit

Permalink
Set correct fill opacity on SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Jan 21, 2018
1 parent 11de346 commit 1555853
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Writer/SvgWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function writeString(QrCodeInterface $qrCode): string
$blockDefinition->addAttribute('width', $data['block_size']);
$blockDefinition->addAttribute('height', $data['block_size']);
$blockDefinition->addAttribute('fill', '#'.sprintf('%02x%02x%02x', $qrCode->getForegroundColor()['r'], $qrCode->getForegroundColor()['g'], $qrCode->getForegroundColor()['b']));
$blockDefinition->addAttribute('fill-opacity', $this->getOpacity($qrCode->getForegroundColor()['a']));

// Background
$background = $svg->addChild('rect');
Expand All @@ -42,6 +43,7 @@ public function writeString(QrCodeInterface $qrCode): string
$background->addAttribute('width', $data['outer_width']);
$background->addAttribute('height', $data['outer_height']);
$background->addAttribute('fill', '#'.sprintf('%02x%02x%02x', $qrCode->getBackgroundColor()['r'], $qrCode->getBackgroundColor()['g'], $qrCode->getBackgroundColor()['b']));
$background->addAttribute('fill-opacity', $this->getOpacity($qrCode->getBackgroundColor()['a']));

foreach ($data['matrix'] as $row => $values) {
foreach ($values as $column => $value) {
Expand All @@ -57,6 +59,13 @@ public function writeString(QrCodeInterface $qrCode): string
return $svg->asXML();
}

private function getOpacity(int $alpha): float
{
$opacity = 1 - $alpha / 127;

return $opacity;
}

public static function getContentType(): string
{
return 'image/svg+xml';
Expand Down

0 comments on commit 1555853

Please sign in to comment.