1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-24 14:32:52 +02:00

Refactor & fix bug in BaseTestCase::assertColor()

This commit is contained in:
Oliver Vogel
2024-05-05 11:48:13 +02:00
parent ab403d98c6
commit 41f2c96dc8
3 changed files with 32 additions and 23 deletions

View File

@@ -9,7 +9,6 @@ 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;
@@ -49,40 +48,50 @@ abstract class BaseTestCase extends MockeryTestCase
*/
protected function assertColor(int $r, int $g, int $b, int $a, ColorInterface $color, int $tolerance = 0)
{
// build errorMessage
$errorMessage = function (int $r, int $g, $b, int $a, ColorInterface $color): string {
$color = 'rgba(' . implode(', ', [
$color->channel(Red::class)->value(),
$color->channel(Green::class)->value(),
$color->channel(Blue::class)->value(),
$color->channel(Alpha::class)->value(),
]) . ')';
return implode(' ', [
'Failed asserting that color',
$color,
'equals',
'rgba(' . $r . ', ' . $g . ', ' . $b . ', ' . $a . ')'
]);
};
// build color channel value range
$range = function (int $base, int $tolerance): array {
return range(max($base - $tolerance, 0), min($base + $tolerance, 255));
};
$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()
$range($r, $tolerance),
$errorMessage($r, $g, $b, $a, $color)
);
$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()
$range($g, $tolerance),
$errorMessage($r, $g, $b, $a, $color)
);
$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()
$range($b, $tolerance),
$errorMessage($r, $g, $b, $a, $color)
);
$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()
$range($a, $tolerance),
$errorMessage($r, $g, $b, $a, $color)
);
}

View File

@@ -35,6 +35,6 @@ final class PlaceModifierTest extends GdTestCase
$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->assertColor(127, 83, 127, 255, $image->pickColor(10, 10), tolerance: 1);
$this->assertColor(127, 83, 127, 255, $image->pickColor(10, 10), tolerance: 0);
}
}

View File

@@ -35,6 +35,6 @@ final class PlaceModifierTest extends ImagickTestCase
$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->assertColor(127, 83, 127, 255, $image->pickColor(10, 10), tolerance: 1);
$this->assertColor(127, 83, 127, 255, $image->pickColor(10, 10), tolerance: 0);
}
}