mirror of
https://github.com/Intervention/image.git
synced 2025-08-11 16:34:00 +02:00
Implement ColorInterface::isTransparent()
This commit is contained in:
@@ -90,4 +90,9 @@ class Color extends AbstractColor
|
|||||||
$this->yellow()->value(),
|
$this->yellow()->value(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function isTransparent(): bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -107,4 +107,14 @@ class Color extends AbstractColor
|
|||||||
{
|
{
|
||||||
return $this->saturation()->value() == 0;
|
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;
|
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
|
public function toHex(string $prefix = ''): string
|
||||||
{
|
{
|
||||||
if ($this->isFullyOpaque()) {
|
if ($this->isTransparent()) {
|
||||||
return sprintf(
|
return sprintf(
|
||||||
'%s%02x%02x%02x',
|
'%s%02x%02x%02x%02x',
|
||||||
$prefix,
|
$prefix,
|
||||||
$this->red()->value(),
|
$this->red()->value(),
|
||||||
$this->green()->value(),
|
$this->green()->value(),
|
||||||
$this->blue()->value()
|
$this->blue()->value(),
|
||||||
|
$this->alpha()->value()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return sprintf(
|
return sprintf(
|
||||||
'%s%02x%02x%02x%02x',
|
'%s%02x%02x%02x',
|
||||||
$prefix,
|
$prefix,
|
||||||
$this->red()->value(),
|
$this->red()->value(),
|
||||||
$this->green()->value(),
|
$this->green()->value(),
|
||||||
$this->blue()->value(),
|
$this->blue()->value()
|
||||||
$this->alpha()->value()
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isFullyOpaque(): bool
|
|
||||||
{
|
|
||||||
return $this->alpha()->value() === 255;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
@@ -141,21 +136,21 @@ class Color extends AbstractColor
|
|||||||
*/
|
*/
|
||||||
public function toString(): string
|
public function toString(): string
|
||||||
{
|
{
|
||||||
if ($this->isFullyOpaque()) {
|
if ($this->isTransparent()) {
|
||||||
return sprintf(
|
return sprintf(
|
||||||
'rgb(%d, %d, %d)',
|
'rgba(%d, %d, %d, %.1F)',
|
||||||
$this->red()->value(),
|
$this->red()->value(),
|
||||||
$this->green()->value(),
|
$this->green()->value(),
|
||||||
$this->blue()->value()
|
$this->blue()->value(),
|
||||||
|
$this->alpha()->normalize(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return sprintf(
|
return sprintf(
|
||||||
'rgba(%d, %d, %d, %.1F)',
|
'rgb(%d, %d, %d)',
|
||||||
$this->red()->value(),
|
$this->red()->value(),
|
||||||
$this->green()->value(),
|
$this->green()->value(),
|
||||||
$this->blue()->value(),
|
$this->blue()->value()
|
||||||
$this->alpha()->normalize(),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,4 +165,14 @@ class Color extends AbstractColor
|
|||||||
|
|
||||||
return count(array_unique($values, SORT_REGULAR)) === 1;
|
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
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isGreyscale(): 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);
|
$color = new Color(100, 50, 20, 0);
|
||||||
$this->assertEquals('cmyk(100%, 50%, 20%, 0%)', (string) $color);
|
$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);
|
$color = new Color(0, 0, 1);
|
||||||
$this->assertTrue($color->isGreyscale());
|
$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);
|
$color = new Color(0, 0, 1);
|
||||||
$this->assertTrue($color->isGreyscale());
|
$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);
|
$color = new Color(50, 50, 50);
|
||||||
$this->assertTrue($color->isGreyscale());
|
$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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user