Skip to content

Commit

Permalink
Add ColorHelper to convert textual colors
Browse files Browse the repository at this point in the history
  • Loading branch information
casperbakker committed Oct 1, 2024
1 parent 8fdf289 commit 1a6071e
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/BarcodeGeneratorDynamicHTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Picqer\Barcode;

use Picqer\Barcode\Exceptions\UnknownTypeException;
use Picqer\Barcode\Helpers\ColorHelper;

class BarcodeGeneratorDynamicHTML extends BarcodeGenerator
{
Expand All @@ -21,7 +22,7 @@ public function getBarcode(string $barcode, $type, string $foregroundColor = 'bl
$barcodeData = $this->getBarcodeData($barcode, $type);

$renderer = new \Picqer\Barcode\Renderers\DynamicHtmlRenderer();
$renderer->setForegroundColor($foregroundColor);
$renderer->setForegroundColor(ColorHelper::getArrayFromColorString($foregroundColor));

return $renderer->render($barcodeData);
}
Expand Down
3 changes: 2 additions & 1 deletion src/BarcodeGeneratorHTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Picqer\Barcode;

use Picqer\Barcode\Exceptions\UnknownTypeException;
use Picqer\Barcode\Helpers\ColorHelper;

class BarcodeGeneratorHTML extends BarcodeGenerator
{
Expand All @@ -25,7 +26,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he
$width = round(($barcodeData->getWidth() * $widthFactor), 3);

$renderer = new \Picqer\Barcode\Renderers\HtmlRenderer();
$renderer->setForegroundColor($foregroundColor);
$renderer->setForegroundColor(ColorHelper::getArrayFromColorString($foregroundColor));

return $renderer->render($barcodeData, $width, $height);
}
Expand Down
2 changes: 1 addition & 1 deletion src/BarcodeGeneratorJPG.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he
}
}

return $renderer->render($barcodeData, $widthFactor, $height);
return $renderer->render($barcodeData, $barcodeData->getWidth() * $widthFactor, $height);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/BarcodeGeneratorPNG.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he
}
}

return $renderer->render($barcodeData, $widthFactor, $height);
return $renderer->render($barcodeData, $barcodeData->getWidth() * $widthFactor, $height);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/BarcodeGeneratorSVG.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Picqer\Barcode;

use Picqer\Barcode\Exceptions\UnknownTypeException;
use Picqer\Barcode\Helpers\ColorHelper;

class BarcodeGeneratorSVG extends BarcodeGenerator
{
Expand All @@ -25,7 +26,7 @@ public function getBarcode(string $barcode, $type, float $widthFactor = 2, float
$width = round(($barcodeData->getWidth() * $widthFactor), 3);

$renderer = new \Picqer\Barcode\Renderers\SvgRenderer();
$renderer->setForegroundColor($foregroundColor);
$renderer->setForegroundColor(ColorHelper::getArrayFromColorString($foregroundColor));

return $renderer->render($barcodeData, $width, $height);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Exceptions/UnknownColorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Picqer\Barcode\Exceptions;

class UnknownColorException extends BarcodeException {}
46 changes: 46 additions & 0 deletions src/Helpers/ColorHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Picqer\Barcode\Helpers;

use Picqer\Barcode\Exceptions\UnknownColorException;

class ColorHelper
{
// Convert textual color values, to array of 3 colors 0-255
// Can be "red", "#333" or "#009945" styles
public static function getArrayFromColorString(string $color): array {
if ($color == 'black') {
return [0, 0, 0];
} elseif ($color == 'white') {
return [255, 255, 255];
} elseif ($color == 'red') {
return [255, 0, 0];
} elseif ($color == 'green') {
return [0, 255, 0];
} elseif ($color == 'blue') {
return [0, 0, 255];
} elseif ($color == 'yellow') {
return [255, 255, 0];
}

// #333 style
if (str_starts_with($color, '#') && strlen($color) === 4) {
return [
hexdec(substr($color, 1, 1) . substr($color, 1, 1)),
hexdec(substr($color, 2, 1) . substr($color, 2, 1)),
hexdec(substr($color, 3, 1) . substr($color, 3, 1)),
];
}

// #009933 style
if (str_starts_with($color, '#') && strlen($color) === 7) {
return [
hexdec(substr($color, 1, 2)),
hexdec(substr($color, 2, 2)),
hexdec(substr($color, 3, 2)),
];
}

throw new UnknownColorException('Only basic string-based colors are supported in v3.');
}
}
2 changes: 1 addition & 1 deletion tests/HtmlRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function test_html_barcode_generator_with_background()
$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897');

$renderer = new Picqer\Barcode\Renderers\HtmlRenderer();
$renderer->setBackgroundColor('red');
$renderer->setBackgroundColor([255, 0, 0]);
$generated = $renderer->render($barcode, $barcode->getWidth() * 2);

$this->assertStringEqualsFile('tests/verified-files/081231723897-code128-red-background.html', $generated);
Expand Down
16 changes: 8 additions & 8 deletions tests/JpgRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function test_jpg_barcode_generator_can_generate_code_128_barcode()

$renderer = new Picqer\Barcode\Renderers\JpgRenderer();
$renderer->useGd();
$generated = $renderer->render($barcode);
$generated = $renderer->render($barcode, $barcode->getWidth() * 2);

$imageInfo = getimagesizefromstring($generated);

Expand All @@ -27,7 +27,7 @@ public function test_jpg_barcode_generator_can_generate_code_39_barcode()

$renderer = new Picqer\Barcode\Renderers\JpgRenderer();
$renderer->useGd();
$generated = $renderer->render($barcode, 1);
$generated = $renderer->render($barcode, $barcode->getWidth());

$imageInfo = getimagesizefromstring($generated);

Expand All @@ -43,7 +43,7 @@ public function test_jpg_barcode_generator_can_use_different_height()

$renderer = new Picqer\Barcode\Renderers\JpgRenderer();
$renderer->useGd();
$generated = $renderer->render($barcode, 2, 45);
$generated = $renderer->render($barcode, $barcode->getWidth() * 2, 45);

$imageInfo = getimagesizefromstring($generated);

Expand All @@ -59,7 +59,7 @@ public function test_jpg_barcode_generator_can_use_different_width_factor()

$renderer = new Picqer\Barcode\Renderers\JpgRenderer();
$renderer->useGd();
$generated = $renderer->render($barcode, 5);
$generated = $renderer->render($barcode, $barcode->getWidth() * 5);

$imageInfo = getimagesizefromstring($generated);

Expand All @@ -81,7 +81,7 @@ public function test_jpg_barcode_generator_can_generate_code_128_barcode_imagick

$renderer = new Picqer\Barcode\Renderers\JpgRenderer();
$renderer->useImagick();
$generated = $renderer->render($barcode);
$generated = $renderer->render($barcode, $barcode->getWidth() * 2);

$imageInfo = getimagesizefromstring($generated);

Expand All @@ -101,7 +101,7 @@ public function test_jpg_barcode_generator_can_generate_code_39_barcode_imagick(

$renderer = new Picqer\Barcode\Renderers\JpgRenderer();
$renderer->useImagick();
$generated = $renderer->render($barcode, 1);
$generated = $renderer->render($barcode, $barcode->getWidth());

$imageInfo = getimagesizefromstring($generated);

Expand All @@ -121,7 +121,7 @@ public function test_jpg_barcode_generator_can_use_different_height_imagick()

$renderer = new Picqer\Barcode\Renderers\JpgRenderer();
$renderer->useGd();
$generated = $renderer->render($barcode, 2, 45);
$generated = $renderer->render($barcode, $barcode->getWidth() * 2, 45);

$imageInfo = getimagesizefromstring($generated);

Expand All @@ -141,7 +141,7 @@ public function test_jpg_barcode_generator_can_use_different_width_factor_imagic

$renderer = new Picqer\Barcode\Renderers\JpgRenderer();
$renderer->useGd();
$generated = $renderer->render($barcode, 5);
$generated = $renderer->render($barcode, $barcode->getWidth() * 5);

$imageInfo = getimagesizefromstring($generated);

Expand Down
14 changes: 7 additions & 7 deletions tests/PngRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function test_png_barcode_generator_can_generate_code_39_barcode()

$renderer = new Picqer\Barcode\Renderers\PngRenderer();
$renderer->useGd();
$generated = $renderer->render($barcode, 1);
$generated = $renderer->render($barcode, $barcode->getWidth());

$imageInfo = getimagesizefromstring($generated);

Expand All @@ -38,7 +38,7 @@ public function test_png_barcode_generator_can_use_different_height()

$renderer = new Picqer\Barcode\Renderers\PngRenderer();
$renderer->useGd();
$generated = $renderer->render($barcode, 2, 45);
$generated = $renderer->render($barcode, $barcode->getWidth() * 2, 45);

$imageInfo = getimagesizefromstring($generated);

Expand All @@ -54,7 +54,7 @@ public function test_png_barcode_generator_can_use_different_width_factor()

$renderer = new Picqer\Barcode\Renderers\PngRenderer();
$renderer->useGd();
$generated = $renderer->render($barcode, 5);
$generated = $renderer->render($barcode, $barcode->getWidth() * 5);

$imageInfo = getimagesizefromstring($generated);

Expand All @@ -76,7 +76,7 @@ public function test_png_barcode_generator_can_generate_code_128_barcode_imagick

$renderer = new Picqer\Barcode\Renderers\PngRenderer();
$renderer->useImagick();
$generated = $renderer->render($barcode);
$generated = $renderer->render($barcode, $barcode->getWidth() * 2);

$this->assertEquals('PNG', substr($generated, 1, 3));
}
Expand All @@ -91,7 +91,7 @@ public function test_png_barcode_generator_can_generate_code_39_barcode_imagick(

$renderer = new Picqer\Barcode\Renderers\PngRenderer();
$renderer->useImagick();
$generated = $renderer->render($barcode, 1);
$generated = $renderer->render($barcode, $barcode->getWidth());

$imageInfo = getimagesizefromstring($generated);

Expand All @@ -111,7 +111,7 @@ public function test_png_barcode_generator_can_use_different_height_imagick()

$renderer = new Picqer\Barcode\Renderers\PngRenderer();
$renderer->useGd();
$generated = $renderer->render($barcode, 2, 45);
$generated = $renderer->render($barcode, $barcode->getWidth() * 2, 45);

$imageInfo = getimagesizefromstring($generated);

Expand All @@ -131,7 +131,7 @@ public function test_png_barcode_generator_can_use_different_width_factor_imagic

$renderer = new Picqer\Barcode\Renderers\PngRenderer();
$renderer->useGd();
$generated = $renderer->render($barcode, 5);
$generated = $renderer->render($barcode, $barcode->getWidth() * 5);

$imageInfo = getimagesizefromstring($generated);

Expand Down
2 changes: 1 addition & 1 deletion tests/SvgRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function test_svg_barcode_generator_can_use_background_color()
$barcode = (new Picqer\Barcode\Types\TypeEan13())->getBarcode('081231723897');

$renderer = new Picqer\Barcode\Renderers\SvgRenderer();
$renderer->setBackgroundColor('red');
$renderer->setBackgroundColor([255, 0, 0]);
$generated = $renderer->render($barcode, 190);

$this->assertStringEqualsFile('tests/verified-files/081231723897-ean13-red-background.svg', $generated);
Expand Down

0 comments on commit 1a6071e

Please sign in to comment.