Skip to content

Commit

Permalink
Add tolerance paramter to BaseTestCase::assertColor()
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed May 5, 2024
1 parent b9d5911 commit 7f84576
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
55 changes: 53 additions & 2 deletions tests/BaseTestCase.php
Expand Up @@ -5,9 +5,14 @@
namespace Intervention\Image\Tests;

use Intervention\Image\Colors\Rgb\Channels\Alpha;
use Intervention\Image\Colors\Rgb\Channels\Blue;
use Intervention\Image\Colors\Rgb\Channels\Green;
use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Colors\Rgb\Color as RgbColor;
use Intervention\Image\Colors\Rgb\Colorspace;
use Intervention\Image\Interfaces\ColorInterface;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use PHPUnit\Framework\ExpectationFailedException;

abstract class BaseTestCase extends MockeryTestCase
{
Expand All @@ -30,9 +35,55 @@ public function getTestResourcePointer($filename = 'test.jpg')
return $pointer;
}

protected function assertColor($r, $g, $b, $a, ColorInterface $color)
/**
* Assert that given color equals the given color channel values in the given optional tolerance
*
* @param int $r
* @param int $g
* @param int $b
* @param int $a
* @param ColorInterface $color
* @param int $tolerance
* @throws ExpectationFailedException
* @return void
*/
protected function assertColor(int $r, int $g, int $b, int $a, ColorInterface $color, int $tolerance = 0)
{
$this->assertEquals([$r, $g, $b, $a], $color->toArray());
$this->assertContains(
$color->channel(Red::class)->value(),
range(max($r - $tolerance, 0), min($r + $tolerance, 255)),
'Failed asserting that color ' .
$color->convertTo(Colorspace::class)->toString() .
' equals '
. $color->convertTo(Colorspace::class)->toString()
);

$this->assertContains(
$color->channel(Green::class)->value(),
range(max($g - $tolerance, 0), min($g + $tolerance, 255)),
'Failed asserting that color ' .
$color->convertTo(Colorspace::class)->toString() .
' equals '
. $color->convertTo(Colorspace::class)->toString()
);

$this->assertContains(
$color->channel(Blue::class)->value(),
range(max($b - $tolerance, 0), min($b + $tolerance, 255)),
'Failed asserting that color ' .
$color->convertTo(Colorspace::class)->toString() .
' equals '
. $color->convertTo(Colorspace::class)->toString()
);

$this->assertContains(
$color->channel(Alpha::class)->value(),
range(max($a - $tolerance, 0), min($a + $tolerance, 255)),
'Failed asserting that color ' .
$color->convertTo(Colorspace::class)->toString() .
' equals '
. $color->convertTo(Colorspace::class)->toString()
);
}

protected function assertTransparency(ColorInterface $color)
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Drivers/Gd/Modifiers/PlaceModifierTest.php
Expand Up @@ -35,6 +35,6 @@ public function testColorChangeOpacityJpeg(): void
$image = $this->createTestImage(16, 16)->fill('0000ff');
$this->assertEquals('0000ff', $image->pickColor(10, 10)->toHex());
$image->modify(new PlaceModifier($this->getTestResourcePath('exif.jpg'), opacity: 50));
$this->assertEquals('7f537f', $image->pickColor(10, 10)->toHex());
$this->assertColor(127, 83, 127, 255, $image->pickColor(10, 10), tolerance: 0);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Drivers/Imagick/Modifiers/PlaceModifierTest.php
Expand Up @@ -35,6 +35,6 @@ public function testColorChangeOpacityJpeg(): void
$image = $this->createTestImage(16, 16)->fill('0000ff');
$this->assertEquals('0000ff', $image->pickColor(10, 10)->toHex());
$image->modify(new PlaceModifier($this->getTestResourcePath('exif.jpg'), opacity: 50));
$this->assertEquals('80537f', $image->pickColor(10, 10)->toHex());
$this->assertColor(127, 83, 127, 255, $image->pickColor(10, 10), tolerance: 0);
}
}

0 comments on commit 7f84576

Please sign in to comment.