Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Traits/AlphaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait AlphaTrait
public function alpha($alpha = null): float|static
{
if ($alpha !== null) {
$this->alpha = min((float) $alpha, 1.0);
$this->alpha = max(0.0, min((float) $alpha, 1.0));
return $this;
}
return round($this->alpha, 2);
Expand Down
39 changes: 39 additions & 0 deletions tests/Color/AlphaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,45 @@ public function testIssueValues()

}

#[Group('alpha')]
public function testNegativeAlphaIsClampedToZero()
{
foreach ($this->alphaColors() as $color) {
$color->alpha(-0.3);
$this->assertEquals(0.0, $color->alpha(), get_class($color));
// the serialized string must be a valid input to its own constructor
$class = get_class($color);
$this->assertEquals(0.0, (new $class((string) $color))->alpha());
}
}

#[Group('alpha')]
public function testAlphaAboveOneStillClampsToOne()
{
foreach ($this->alphaColors() as $color) {
$color->alpha(1.7);
$this->assertEquals(1.0, $color->alpha(), get_class($color));
$class = get_class($color);
$this->assertEquals(1.0, (new $class((string) $color))->alpha());
}
}

#[Group('alpha')]
public function testInRangeAlphaPassesThroughUnchanged()
{
foreach ([0.0, 0.5, 1.0] as $value) {
foreach ($this->alphaColors() as $color) {
$color->alpha($value);
$this->assertEquals($value, $color->alpha(), get_class($color));
}
}
}

protected function alphaColors(): array
{
return [new Rgba('10,20,30,0.5'), new Hsla('120,50%,50%,0.5'), new Hexa('112233ff')];
}

protected function getBaseValues(): array
{
return [new Rgba('rgba(102, 102, 102, 0.5)'), new Hex('b2b2b2')];
Expand Down