Skip to content

Commit db948dd

Browse files
author
Martin Brecht-Precht
committed
Made the foreground and background color configurable.
1 parent 37a4c8d commit db948dd

File tree

9 files changed

+493
-15
lines changed

9 files changed

+493
-15
lines changed

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,29 @@ $qrCodeData = $encoder
4747

4848
```{php}
4949
use QrCodeSuite\QrRender;
50+
use QrCodeSuite\QrRender\Color\RgbColor;
51+
use QrCodeSuite\QrRender\Color\CmykColor;
5052
5153
// Render the encoded QR code block data as RGB PNG
5254
$renderer = new QrRender\QrCodeRendererPng();
53-
$renderer->render($qrCodeData, 'path/to/qr-code.png');
55+
$renderer
56+
->setForegroundColor(new RgbColor(255, 0, 255))
57+
->setBackgroundColor(new RgbColor(51, 51, 51))
58+
->render($qrCodeData, 'path/to/qr-code.png');
5459
5560
// Render the encoded QR code block data as CMYK TIFF
5661
$renderer = new QrRender\QrCodeRendererTiff();
57-
$renderer->render($qrCodeData, 'path/to/qr-code.tif');
62+
$renderer
63+
->setForegroundColor(new CmykColor(0, 100, 0, 0))
64+
->setBackgroundColor(new CmykColor(0, 100, 100, 0))
65+
->render($qrCodeData, 'path/to/qr-code.tif');
5866
5967
// Render the encoded QR code block data as CMYK vectorized EPS
68+
// EPS has no background color. It is just the blocks on blank cnavas.
6069
$renderer = new QrRender\QrCodeRendererEps();
61-
$renderer->render($qrCodeData, 'path/to/qr-code.eps');
70+
$renderer
71+
->setForegroundColor(new CmykColor(0, 100, 0, 0))
72+
->render($qrCodeData, 'path/to/qr-code.eps');
6273
```
6374

6475
---
@@ -70,10 +81,10 @@ Contributing to our projects is always very appreciated.
7081

7182
## TODOs
7283

73-
- Decorate the code base with some unit tests.
84+
- ~~Decorate the code base with some unit tests.~~
7485
- Allow configuration of the QR codes like:
75-
- Foreground color
76-
- Background color
86+
- ~~Foreground color~~
87+
- ~~Background color~~
7788
- Image size of PNG and TIFF
7889

7990
## License

src/QrRender/Color/CmykColor.php

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
3+
namespace QrCodeSuite\QrRender\Color;
4+
5+
/**
6+
* Class CmykColor
7+
*
8+
* @package QrCodeSuite\QrRender\Color
9+
*/
10+
class CmykColor
11+
{
12+
13+
/**
14+
* @var int
15+
*/
16+
private $cyan = 0;
17+
18+
/**
19+
* @var int
20+
*/
21+
private $magenta = 0;
22+
23+
/**
24+
* @var int
25+
*/
26+
private $yellow = 0;
27+
28+
/**
29+
* @var int
30+
*/
31+
private $black = 0;
32+
33+
/**
34+
* CmykColor constructor.
35+
*
36+
* @param int $cyan
37+
* @param int $magenta
38+
* @param int $yellow
39+
* @param int $black
40+
*/
41+
public function __construct($cyan, $magenta, $yellow, $black)
42+
{
43+
$this
44+
->setCyan($cyan)
45+
->setMagenta($magenta)
46+
->setYellow($yellow)
47+
->setBlack($black);
48+
}
49+
50+
/**
51+
* @return int
52+
*/
53+
public function getCyan()
54+
{
55+
return $this->cyan;
56+
}
57+
58+
/**
59+
* @param int $cyan
60+
* @return $this
61+
*/
62+
public function setCyan($cyan)
63+
{
64+
if (!$this->validateColorChannelValue($cyan)) {
65+
throw new \InvalidArgumentException('Cyan color channel value invalid.');
66+
}
67+
$this->cyan = $cyan;
68+
return $this;
69+
}
70+
71+
/**
72+
* @return int
73+
*/
74+
public function getMagenta()
75+
{
76+
return $this->magenta;
77+
}
78+
79+
/**
80+
* @param int $magenta
81+
* @return $this
82+
*/
83+
public function setMagenta($magenta)
84+
{
85+
$this->magenta = $magenta;
86+
return $this;
87+
}
88+
89+
/**
90+
* @return int
91+
*/
92+
public function getYellow()
93+
{
94+
return $this->yellow;
95+
}
96+
97+
/**
98+
* @param int $yellow
99+
* @return $this
100+
*/
101+
public function setYellow($yellow)
102+
{
103+
$this->yellow = $yellow;
104+
return $this;
105+
}
106+
107+
/**
108+
* @return int
109+
*/
110+
public function getBlack()
111+
{
112+
return $this->black;
113+
}
114+
115+
/**
116+
* @param int $black
117+
* @return $this
118+
*/
119+
public function setBlack($black)
120+
{
121+
$this->black = $black;
122+
return $this;
123+
}
124+
125+
/**
126+
* @return string
127+
*/
128+
public function getImagickNotation()
129+
{
130+
return
131+
'cmyk('
132+
. $this->colorChannelValueToOct($this->getCyan()) . ','
133+
. $this->colorChannelValueToOct($this->getMagenta()) . ','
134+
. $this->colorChannelValueToOct($this->getYellow()) . ','
135+
. $this->colorChannelValueToOct($this->getBlack())
136+
. ')';
137+
}
138+
139+
/**
140+
* @return string
141+
*/
142+
public function getEpsNotation()
143+
{
144+
return
145+
$this->colorChannelValueToFloat($this->getCyan()) . ' '
146+
. $this->colorChannelValueToFloat($this->getMagenta()) . ' '
147+
. $this->colorChannelValueToFloat($this->getYellow()) . ' '
148+
. $this->colorChannelValueToFloat($this->getBlack());
149+
}
150+
151+
/**
152+
* @param int $int
153+
*/
154+
private function colorChannelValueToOct($int)
155+
{
156+
return decoct($int);
157+
}
158+
159+
/**
160+
* @param int $int
161+
*/
162+
private function colorChannelValueToFloat($int)
163+
{
164+
return decoct($int);
165+
}
166+
167+
/**
168+
* @param mixed $colorChannelValue
169+
* @return bool
170+
*/
171+
private function validateColorChannelValue($colorChannelValue)
172+
{
173+
if (!is_int($colorChannelValue)) {
174+
return false;
175+
}
176+
return ($colorChannelValue >= 0 && $colorChannelValue <= 100);
177+
}
178+
179+
}

src/QrRender/Color/RgbColor.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
namespace QrCodeSuite\QrRender\Color;
4+
5+
/**
6+
* Class RgbColor
7+
*
8+
* @package QrCodeSuite\QrRender\Color
9+
*/
10+
class RgbColor
11+
{
12+
13+
/**
14+
* @var int
15+
*/
16+
private $red = 0;
17+
18+
/**
19+
* @var int
20+
*/
21+
private $green = 0;
22+
23+
/**
24+
* @var int
25+
*/
26+
private $blue = 0;
27+
28+
/**
29+
* RgbColor constructor.
30+
*
31+
* @param int $red
32+
* @param int $green
33+
* @param int $blue
34+
*/
35+
public function __construct($red, $green, $blue)
36+
{
37+
$this
38+
->setRed($red)
39+
->setGreen($green)
40+
->setBlue($blue);
41+
}
42+
43+
/**
44+
* @return int
45+
*/
46+
public function getRed()
47+
{
48+
return $this->red;
49+
}
50+
51+
/**
52+
* @param int $red
53+
* @return $this
54+
*/
55+
public function setRed($red)
56+
{
57+
if (!$this->validateColorChannelValue($red)) {
58+
throw new \InvalidArgumentException('Red color channel value invalid.');
59+
}
60+
$this->red = $red;
61+
return $this;
62+
}
63+
64+
/**
65+
* @return int
66+
*/
67+
public function getGreen()
68+
{
69+
return $this->green;
70+
}
71+
72+
/**
73+
* @param int $green
74+
* @return $this
75+
*/
76+
public function setGreen($green)
77+
{
78+
if (!$this->validateColorChannelValue($green)) {
79+
throw new \InvalidArgumentException('Green color channel value invalid.');
80+
}
81+
$this->green = $green;
82+
return $this;
83+
}
84+
85+
/**
86+
* @return int
87+
*/
88+
public function getBlue()
89+
{
90+
return $this->blue;
91+
}
92+
93+
/**
94+
* @param int $blue
95+
* @return $this
96+
*/
97+
public function setBlue($blue)
98+
{
99+
if (!$this->validateColorChannelValue($blue)) {
100+
throw new \InvalidArgumentException('Blue color channel value invalid.');
101+
}
102+
$this->blue = $blue;
103+
return $this;
104+
}
105+
106+
/**
107+
* @return string
108+
*/
109+
public function getHex()
110+
{
111+
return
112+
'#'
113+
. $this->colorChannelValueToHex($this->getRed())
114+
. $this->colorChannelValueToHex($this->getGreen())
115+
. $this->colorChannelValueToHex($this->getBlue());
116+
}
117+
118+
/**
119+
* @param mixed $colorChannelValue
120+
* @return bool
121+
*/
122+
private function validateColorChannelValue($colorChannelValue)
123+
{
124+
if (!is_int($colorChannelValue)) {
125+
return false;
126+
}
127+
return ($colorChannelValue >= 0 && $colorChannelValue <= 255);
128+
}
129+
130+
/**
131+
* @param int $int
132+
* @return mixed
133+
*/
134+
private function colorChannelValueToHex($int)
135+
{
136+
$hex = (string)dechex($int);
137+
return str_pad($hex, 2, '0', STR_PAD_LEFT);
138+
}
139+
140+
}

0 commit comments

Comments
 (0)