mirror of
https://github.com/Intervention/image.git
synced 2025-01-17 20:28:21 +01:00
Implement ColorInterface::isTransparent()
This commit is contained in:
parent
7eb16e1aa5
commit
17ff3337dd
@ -90,4 +90,9 @@ class Color extends AbstractColor
|
||||
$this->yellow()->value(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function isTransparent(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -107,4 +107,14 @@ class Color extends AbstractColor
|
||||
{
|
||||
return $this->saturation()->value() == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see ColorInterface::isTransparent()
|
||||
*/
|
||||
public function isTransparent(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -107,4 +107,14 @@ class Color extends AbstractColor
|
||||
{
|
||||
return $this->saturation()->value() == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see ColorInterface::isTransparent()
|
||||
*/
|
||||
public function isTransparent(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user