Skip to content

Commit

Permalink
bugfix, test coverage improved and ready for v1 :D
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Fiorani committed Sep 28, 2018
1 parent 9ea557f commit e52f7c5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<testsuite name="PHP Legofy Integration Test Suite">
<directory>tests/Integration</directory>
</testsuite>
<!--testsuite name="PHP Cloudflare AMP Validator Unit Test Suite">
<testsuite name="PHP Legofy Unit Test Suite">
<directory>tests/Unit</directory>
</testsuite-->
</testsuite>
</testsuites>
<filter>
<whitelist>
Expand Down
7 changes: 2 additions & 5 deletions src/Legofy.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ public function convertToLego($resource, float $resolutionMultipler = 1, bool $l
{
$image = ImageManagerStatic::make($resource);

$imageOriginalWidth = $image->width();
$imageOriginalHeight = $image->height();

// Apply resolution multipler
$image->resize(
$image->getWidth() * $resolutionMultipler,
Expand Down Expand Up @@ -110,7 +107,7 @@ public function convertToLego($resource, float $resolutionMultipler = 1, bool $l

private function getAverageBrickColor(): AbstractColor
{
if (false == is_null($this->brickAverageColor)) {
if (false === is_null($this->brickAverageColor)) {
return $this->brickAverageColor;
}

Expand All @@ -130,7 +127,7 @@ private function colorizeBrick(AbstractColor $color): Image
return ImageManagerStatic::canvas(
$this->getBrick()->getWidth(),
$this->getBrick()->getHeight(),
$color->getHex()
$color->getHex('')
)->insert(
(clone $this->getBrick())->colorize(
// Picked color subtracted by the average brick color to avoid the image getting brighter
Expand Down
8 changes: 5 additions & 3 deletions src/Pallete/ColorPalette.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,13 @@ public function pickClosestColor(AbstractColor $color): AbstractColor
{
$distances = [];

$colorArray = $color->getArray();

foreach ($this->palette as $colorIdentifier => $colorSchema) {
$distance =
abs($colorSchema[0] - $color->r) +
abs($colorSchema[1] - $color->g) +
abs($colorSchema[2] - $color->b);
abs($colorSchema[0] - $colorArray[0]) +
abs($colorSchema[1] - $colorArray[1]) +
abs($colorSchema[2] - $colorArray[2]);

$distances[$distance] = [$colorSchema[0], $colorSchema[1], $colorSchema[2]];
}
Expand Down
57 changes: 57 additions & 0 deletions tests/Unit/Palette/ColorPaletteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types=1);

namespace Tests\RicardoFiorani\Unit\Palette;

use Intervention\Image\Gd\Color;
use PHPUnit\Framework\TestCase;
use RicardoFiorani\Legofy\Pallete\ColorPalette;

class ColorPaletteTest extends TestCase
{
/**
* @dataProvider colorProvider
*/
public function testGetsRightColor(array $colors, array $input, $expected)
{
$colorPalette = new ColorPalette($colors);

$outputColor = $colorPalette->pickClosestColor(new Color($input));
$outputColorArray = $outputColor->getArray();

$this->assertEquals($expected[0], $outputColorArray[0]);
$this->assertEquals($expected[1], $outputColorArray[1]);
$this->assertEquals($expected[2], $outputColorArray[2]);
}

public function colorProvider()
{
return [
//Whole set
[
//Palette
[
'024' => [254, 196, 1,],
'106' => [231, 100, 25,],
'021' => [222, 1, 14,],
],
//input color
[253, 194, 2],
//expected color
[254, 196, 1,]
],
//Whole set
[
//Palette
[
'024' => [254, 196, 1,],
'106' => [231, 100, 25,],
'021' => [222, 1, 14,],
],
//input color
[224, 5, 22],
//expected color
[222, 1, 14,]
],
];
}
}

0 comments on commit e52f7c5

Please sign in to comment.