:octocat: QREps: support QROptions::$bgColor

This commit is contained in:
smiley
2023-07-16 14:02:55 +02:00
parent 130c3d3550
commit 17e8ccae62
2 changed files with 71 additions and 49 deletions
+37 -40
View File
@@ -7,50 +7,47 @@
*/
use chillerlan\QRCode\{QRCode, QROptions};
use chillerlan\QRCode\Common\EccLevel;
use chillerlan\QRCode\Data\QRMatrix;
use chillerlan\QRCode\Output\QROutputInterface;
require_once __DIR__.'/../vendor/autoload.php';
$options = new QROptions([
'version' => 7,
'outputType' => QROutputInterface::EPS,
'eccLevel' => EccLevel::L,
'scale' => 5,
'addQuietzone' => true,
'drawLightModules' => false,
'cachefile' => __DIR__.'/test.eps', // save to file
'moduleValues' => [
// finder
QRMatrix::M_FINDER_DARK => [0, 63, 255], // dark (true)
QRMatrix::M_FINDER_DOT => [0, 63, 255], // finder dot, dark (true)
QRMatrix::M_FINDER => [233, 233, 233], // light (false)
// alignment
QRMatrix::M_ALIGNMENT_DARK => [255, 0, 255],
QRMatrix::M_ALIGNMENT => [233, 233, 233],
// timing
QRMatrix::M_TIMING_DARK => [255, 0, 0],
QRMatrix::M_TIMING => [233, 233, 233],
// format
QRMatrix::M_FORMAT_DARK => [67, 159, 84],
QRMatrix::M_FORMAT => [233, 233, 233],
// version
QRMatrix::M_VERSION_DARK => [62, 174, 190],
QRMatrix::M_VERSION => [233, 233, 233],
// data
QRMatrix::M_DATA_DARK => [0, 0, 0],
QRMatrix::M_DATA => [233, 233, 233],
// darkmodule
QRMatrix::M_DARKMODULE => [0, 0, 0],
// separator
QRMatrix::M_SEPARATOR => [233, 233, 233],
// quietzone
QRMatrix::M_QUIETZONE => [233, 233, 233],
// logo (requires a call to QRMatrix::setLogoSpace()), see QRImageWithLogo
QRMatrix::M_LOGO => [233, 233, 233],
],
]);
$options = new QROptions;
$options->version = 7;
$options->outputType = QROutputInterface::EPS;
$options->scale = 5;
$options->drawLightModules = false;
$options->bgColor = [222, 222, 222];
$options->moduleValues = [
// finder
QRMatrix::M_FINDER_DARK => [0, 63, 255], // dark (true)
QRMatrix::M_FINDER_DOT => [0, 63, 255], // finder dot, dark (true)
QRMatrix::M_FINDER => [233, 233, 233], // light (false)
// alignment
QRMatrix::M_ALIGNMENT_DARK => [255, 0, 255],
QRMatrix::M_ALIGNMENT => [233, 233, 233],
// timing
QRMatrix::M_TIMING_DARK => [255, 0, 0],
QRMatrix::M_TIMING => [233, 233, 233],
// format
QRMatrix::M_FORMAT_DARK => [67, 159, 84],
QRMatrix::M_FORMAT => [233, 233, 233],
// version
QRMatrix::M_VERSION_DARK => [62, 174, 190],
QRMatrix::M_VERSION => [233, 233, 233],
// data
QRMatrix::M_DATA_DARK => [0, 0, 0],
QRMatrix::M_DATA => [233, 233, 233],
// darkmodule
QRMatrix::M_DARKMODULE => [0, 0, 0],
// separator
QRMatrix::M_SEPARATOR => [233, 233, 233],
// quietzone
QRMatrix::M_QUIETZONE => [233, 233, 233],
// logo (requires a call to QRMatrix::setLogoSpace()), see QRImageWithLogo
QRMatrix::M_LOGO => [233, 233, 233],
];
if(php_sapi_name() !== 'cli'){
@@ -59,6 +56,6 @@ if(php_sapi_name() !== 'cli'){
header('Content-Disposition: filename="qrcode.eps"');
}
echo (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
echo (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ', __DIR__.'/test.eps');
exit;
+34 -9
View File
@@ -52,7 +52,7 @@ class QREps extends QROutputAbstract{
*
* @inheritDoc
*/
protected function prepareModuleValue($value):array{
protected function prepareModuleValue($value):string{
$values = [];
foreach(array_values($value) as $i => $val){
@@ -65,14 +65,37 @@ class QREps extends QROutputAbstract{
$values[] = round((max(0, min(255, intval($val))) / 255), 6);
}
return $values;
return $this->formatColor($values);
}
/**
* @inheritDoc
*/
protected function getDefaultModuleValue(bool $isDark):array{
return ($isDark) ? [0.0, 0.0, 0.0] : [1.0, 1.0, 1.0];
protected function getDefaultModuleValue(bool $isDark):string{
return $this->formatColor(($isDark) ? [0.0, 0.0, 0.0] : [1.0, 1.0, 1.0]);
}
/**
* Set the color format
*
* 4 values in the color array will be interpreted as CMYK, 3 as RGB
*
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
*/
protected function formatColor(array $values):string{
$count = count($values);
if($count < 3){
throw new QRCodeOutputException('invalid color value');
}
$format = ($count === 4)
// CMYK
? '%f %f %f %f C'
// RGB
:'%f %f %f R';
return sprintf($format, ...$values);
}
/**
@@ -98,6 +121,11 @@ class QREps extends QROutputAbstract{
'%%EndProlog',
];
if($this->options->bgColor !== null && self::moduleValueIsValid($this->options->bgColor)){
$eps[] = $this->prepareModuleValue($this->options->bgColor);
$eps[] = sprintf('0 0 %1$s %1$s F', $this->length);
}
// create the path elements
$paths = $this->collectModules(fn(int $x, int $y):string => $this->module($x, $y));
@@ -107,11 +135,8 @@ class QREps extends QROutputAbstract{
continue;
}
// 4 values will be interpreted as CMYK, 3 as RGB
$val = $this->getModuleValue($M_TYPE);
$format = (count($val) === 4) ? '%f %f %f %f C' : '%f %f %f R';
$eps[] = sprintf($format, ...$val);
$eps[] = implode("\n", $path);
$eps[] = $this->getModuleValue($M_TYPE);
$eps[] = implode("\n", $path);
}
// end file