From 17ff3337ddd1588163fe6d863b8157dfa0a262b5 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 2 Mar 2024 11:29:28 +0100 Subject: [PATCH] Implement ColorInterface::isTransparent() --- src/Colors/Cmyk/Color.php | 5 ++++ src/Colors/Hsl/Color.php | 10 +++++++ src/Colors/Hsv/Color.php | 10 +++++++ src/Colors/Rgb/Color.php | 39 ++++++++++++++++------------ src/Interfaces/ColorInterface.php | 7 +++++ tests/Unit/Colors/Cmyk/ColorTest.php | 6 +++++ tests/Unit/Colors/Hsl/ColorTest.php | 6 +++++ tests/Unit/Colors/Hsv/ColorTest.php | 6 +++++ tests/Unit/Colors/Rgb/ColorTest.php | 15 +++++++++++ 9 files changed, 87 insertions(+), 17 deletions(-) diff --git a/src/Colors/Cmyk/Color.php b/src/Colors/Cmyk/Color.php index 6510493a..e46cdb53 100644 --- a/src/Colors/Cmyk/Color.php +++ b/src/Colors/Cmyk/Color.php @@ -90,4 +90,9 @@ class Color extends AbstractColor $this->yellow()->value(), ]); } + + public function isTransparent(): bool + { + return false; + } } diff --git a/src/Colors/Hsl/Color.php b/src/Colors/Hsl/Color.php index 136b11b8..2e3f5c0f 100644 --- a/src/Colors/Hsl/Color.php +++ b/src/Colors/Hsl/Color.php @@ -107,4 +107,14 @@ class Color extends AbstractColor { return $this->saturation()->value() == 0; } + + /** + * {@inheritdoc} + * + * @see ColorInterface::isTransparent() + */ + public function isTransparent(): bool + { + return false; + } } diff --git a/src/Colors/Hsv/Color.php b/src/Colors/Hsv/Color.php index da4f7251..f75f8a3b 100644 --- a/src/Colors/Hsv/Color.php +++ b/src/Colors/Hsv/Color.php @@ -107,4 +107,14 @@ class Color extends AbstractColor { return $this->saturation()->value() == 0; } + + /** + * {@inheritdoc} + * + * @see ColorInterface::isTransparent() + */ + public function isTransparent(): bool + { + return false; + } } diff --git a/src/Colors/Rgb/Color.php b/src/Colors/Rgb/Color.php index 1f500c66..6a4ac577 100644 --- a/src/Colors/Rgb/Color.php +++ b/src/Colors/Rgb/Color.php @@ -109,31 +109,26 @@ class Color extends AbstractColor */ public function toHex(string $prefix = ''): string { - if ($this->isFullyOpaque()) { + if ($this->isTransparent()) { return sprintf( - '%s%02x%02x%02x', + '%s%02x%02x%02x%02x', $prefix, $this->red()->value(), $this->green()->value(), - $this->blue()->value() + $this->blue()->value(), + $this->alpha()->value() ); } return sprintf( - '%s%02x%02x%02x%02x', + '%s%02x%02x%02x', $prefix, $this->red()->value(), $this->green()->value(), - $this->blue()->value(), - $this->alpha()->value() + $this->blue()->value() ); } - public function isFullyOpaque(): bool - { - return $this->alpha()->value() === 255; - } - /** * {@inheritdoc} * @@ -141,21 +136,21 @@ class Color extends AbstractColor */ public function toString(): string { - if ($this->isFullyOpaque()) { + if ($this->isTransparent()) { return sprintf( - 'rgb(%d, %d, %d)', + 'rgba(%d, %d, %d, %.1F)', $this->red()->value(), $this->green()->value(), - $this->blue()->value() + $this->blue()->value(), + $this->alpha()->normalize(), ); } return sprintf( - 'rgba(%d, %d, %d, %.1F)', + 'rgb(%d, %d, %d)', $this->red()->value(), $this->green()->value(), - $this->blue()->value(), - $this->alpha()->normalize(), + $this->blue()->value() ); } @@ -170,4 +165,14 @@ class Color extends AbstractColor return count(array_unique($values, SORT_REGULAR)) === 1; } + + /** + * {@inheritdoc} + * + * @see ColorInterface::isTransparent() + */ + public function isTransparent(): bool + { + return $this->alpha()->value() < $this->alpha()->max(); + } } diff --git a/src/Interfaces/ColorInterface.php b/src/Interfaces/ColorInterface.php index acdfdae7..9f66ed24 100644 --- a/src/Interfaces/ColorInterface.php +++ b/src/Interfaces/ColorInterface.php @@ -83,4 +83,11 @@ interface ColorInterface * @return bool */ public function isGreyscale(): bool; + + /** + * Determine if the current color is (semi) transparent + * + * @return bool + */ + public function isTransparent(): bool; } diff --git a/tests/Unit/Colors/Cmyk/ColorTest.php b/tests/Unit/Colors/Cmyk/ColorTest.php index 01a9003e..3ae51d7d 100644 --- a/tests/Unit/Colors/Cmyk/ColorTest.php +++ b/tests/Unit/Colors/Cmyk/ColorTest.php @@ -105,4 +105,10 @@ final class ColorTest extends BaseTestCase $color = new Color(100, 50, 20, 0); $this->assertEquals('cmyk(100%, 50%, 20%, 0%)', (string) $color); } + + public function testIsTransparent(): void + { + $color = new Color(100, 50, 50, 0); + $this->assertFalse($color->isTransparent()); + } } diff --git a/tests/Unit/Colors/Hsl/ColorTest.php b/tests/Unit/Colors/Hsl/ColorTest.php index b926b2c2..9f503bf5 100644 --- a/tests/Unit/Colors/Hsl/ColorTest.php +++ b/tests/Unit/Colors/Hsl/ColorTest.php @@ -102,4 +102,10 @@ final class ColorTest extends BaseTestCase $color = new Color(0, 0, 1); $this->assertTrue($color->isGreyscale()); } + + public function testIsTransparent(): void + { + $color = new Color(0, 1, 0); + $this->assertFalse($color->isTransparent()); + } } diff --git a/tests/Unit/Colors/Hsv/ColorTest.php b/tests/Unit/Colors/Hsv/ColorTest.php index 76d90e08..930c54ff 100644 --- a/tests/Unit/Colors/Hsv/ColorTest.php +++ b/tests/Unit/Colors/Hsv/ColorTest.php @@ -102,4 +102,10 @@ final class ColorTest extends BaseTestCase $color = new Color(0, 0, 1); $this->assertTrue($color->isGreyscale()); } + + public function testIsTransparent(): void + { + $color = new Color(1, 0, 0); + $this->assertFalse($color->isTransparent()); + } } diff --git a/tests/Unit/Colors/Rgb/ColorTest.php b/tests/Unit/Colors/Rgb/ColorTest.php index 4163662a..6fb2051e 100644 --- a/tests/Unit/Colors/Rgb/ColorTest.php +++ b/tests/Unit/Colors/Rgb/ColorTest.php @@ -148,4 +148,19 @@ final class ColorTest extends BaseTestCase $color = new Color(50, 50, 50); $this->assertTrue($color->isGreyscale()); } + + public function testIsTransparent(): void + { + $color = new Color(255, 255, 255); + $this->assertFalse($color->isTransparent()); + + $color = new Color(255, 255, 255, 255); + $this->assertFalse($color->isTransparent()); + + $color = new Color(255, 255, 255, 85); + $this->assertTrue($color->isTransparent()); + + $color = new Color(255, 255, 255, 0); + $this->assertTrue($color->isTransparent()); + } }