Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rgba to hlsa and hexa #47

Closed
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
9 changes: 8 additions & 1 deletion src/Color/Hsla.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,14 @@ public function toHex(): Hex
*/
public function toHexa(): Hexa
{
return $this->toHex()->toHexa()->alpha($this->alpha());
$old_alpha = $this->alpha();

return $this
->clone()
->alpha(1)
->toHex()
->toHexa()
->alpha($old_alpha);
}

/**
Expand Down
18 changes: 16 additions & 2 deletions src/Color/Rgba.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ public function toHex(): Hex
*/
public function toHexa(): Hexa
{
return $this->toRgb()->toHex()->toHexa()->alpha($this->alpha());
$old_alpha = $this->alpha();

return $this
->clone()
->alpha(1)
->toRgb()
->toHexa()
->alpha($old_alpha);
}

/**
Expand All @@ -118,7 +125,14 @@ public function toHsl(): Hsl
*/
public function toHsla(): Hsla
{
return $this->toHsl()->toHsla()->alpha($this->alpha());
$old_alpha = $this->alpha();

return $this
->clone()
->alpha(1)
->toHsl()
->toHsla()
->alpha($old_alpha);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/AlphaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ protected function alphaHexToFloat(string $alpha): float
*/
protected function alphaFloatToHex(float $alpha): string
{
return dechex($alpha * 255);
return dechex(intval($alpha * 255));
}
}
2 changes: 1 addition & 1 deletion tests/Color/HslaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function testHslaConversion()
$hsla = new Hsla('hsla(150,100%,50%,0.3)');
$this->assertEquals(new Hex('b2ffd8'), $hsla->toHex());
$this->assertEquals(new Rgba('0,255,128,0.3'), $hsla->toRgba());
$this->assertEquals(new Hexa('b2ffd84c'), $hsla->toHexa());
$this->assertEquals(new Hexa('00ff804c'), $hsla->toHexa());
}


Expand Down
2 changes: 1 addition & 1 deletion tests/Color/RgbaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testRgbaConversion()
{
$rgba = new Rgba('rgba(11,22,33,0.2)');
$this->assertEquals(new Hex('ced0d2'), $rgba->toHex());
$this->assertEquals(new Hexa('ced0d233'), $rgba->toHexa());
$this->assertEquals(new Hexa('0b162133'), $rgba->toHexa());
$rgba = new Rgba('rgba(93,111,222,0.33)');
$this->assertEquals(new Hex('a7add1'), $rgba->background((new Hex('ccc'))->toRgb())->toHex());
}
Expand Down
Loading