1
0
mirror of https://github.com/Intervention/image.git synced 2025-01-17 12:18:14 +01:00

Implement ColorInterface::isTransparent()

This commit is contained in:
Oliver Vogel 2024-03-02 11:29:28 +01:00
parent 7eb16e1aa5
commit 17ff3337dd
No known key found for this signature in database
GPG Key ID: 1B19D214C02D69BB
9 changed files with 87 additions and 17 deletions

View File

@ -90,4 +90,9 @@ class Color extends AbstractColor
$this->yellow()->value(),
]);
}
public function isTransparent(): bool
{
return false;
}
}

View File

@ -107,4 +107,14 @@ class Color extends AbstractColor
{
return $this->saturation()->value() == 0;
}
/**
* {@inheritdoc}
*
* @see ColorInterface::isTransparent()
*/
public function isTransparent(): bool
{
return false;
}
}

View File

@ -107,4 +107,14 @@ class Color extends AbstractColor
{
return $this->saturation()->value() == 0;
}
/**
* {@inheritdoc}
*
* @see ColorInterface::isTransparent()
*/
public function isTransparent(): bool
{
return false;
}
}

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}