diff --git a/src/Drivers/Imagick/Color.php b/src/Drivers/Imagick/Color.php index 904b1c7a..a04e0754 100644 --- a/src/Drivers/Imagick/Color.php +++ b/src/Drivers/Imagick/Color.php @@ -55,4 +55,14 @@ class Color extends AbstractColor implements ColorInterface $this->alpha() ]; } + + public function toInt(): int + { + $r = $this->red(); + $g = $this->green(); + $b = $this->blue(); + $a = intval(round($this->alpha() * 255)); + + return intval(($a << 24) + ($r << 16) + ($g << 8) + $b); + } } diff --git a/src/Interfaces/ColorInterface.php b/src/Interfaces/ColorInterface.php index 3960c23b..e1486987 100644 --- a/src/Interfaces/ColorInterface.php +++ b/src/Interfaces/ColorInterface.php @@ -10,4 +10,5 @@ interface ColorInterface public function alpha(): float; public function toArray(): array; public function toHex(): string; + public function toInt(): int; } diff --git a/tests/Drivers/Gd/InputHandlerTest.php b/tests/Drivers/Gd/InputHandlerTest.php index 41fdf954..22fa5dc8 100644 --- a/tests/Drivers/Gd/InputHandlerTest.php +++ b/tests/Drivers/Gd/InputHandlerTest.php @@ -99,4 +99,12 @@ class InputHandlerTest extends TestCase $this->assertEquals(51, $result->green()); $this->assertEquals(51, $result->blue()); } + + public function testHandleTransparent(): void + { + $handler = new InputHandler(); + $input = 'transparent'; + $result = $handler->handle($input); + $this->assertInstanceOf(Color::class, $result); + } } diff --git a/tests/Drivers/Imagick/ColorTest.php b/tests/Drivers/Imagick/ColorTest.php index 2768c7f6..bf889f0c 100644 --- a/tests/Drivers/Imagick/ColorTest.php +++ b/tests/Drivers/Imagick/ColorTest.php @@ -84,4 +84,25 @@ class ColorTest extends TestCase $this->assertEquals('b53717', $color->toHex()); $this->assertEquals('#b53717', $color->toHex('#')); } + + public function testToInt(): void + { + $color = $this->getTestColor(255, 255, 255); + $this->assertEquals($color->toInt(), 4294967295); + + $color = $this->getTestColor(255, 255, 255, 1); + $this->assertEquals($color->toInt(), 4294967295); + + $color = $this->getTestColor(181, 55, 23, 0.2); + $this->assertEquals($color->toInt(), 867514135); + + $color = $this->getTestColor(255, 255, 255, 0.5); + $this->assertEquals($color->toInt(), 2164260863); + + $color = $this->getTestColor(181, 55, 23, 1); + $this->assertEquals($color->toInt(), 4290066199); + + $color = $this->getTestColor(0, 0, 0, 0); + $this->assertEquals($color->toInt(), 0); + } }