Skip to content

Commit

Permalink
fixes for alpha value conversions #37
Browse files Browse the repository at this point in the history
  • Loading branch information
ozdemirburak committed Nov 24, 2023
1 parent b7c3791 commit f6f9069
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 60 deletions.
44 changes: 2 additions & 42 deletions src/Color/Rgb.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,8 @@ public function toHexa(): Hexa
*/
public function toHsl(): Hsl
{
[$r, $g, $b, $min, $max] = $this->getHValues();
$l = ($max + $min) / 2;
if ($max === $min) {
$h = $s = 0;
} else {
$d = $max - $min;
$s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min);
$h = $this->getH($max, $r, $g, $b, $d);
}
$code = implode(',', [round($h * 360), round($s * 100), round($l * 100)]);
[$h, $s, $l] = $this->getHslValues();
$code = implode(',', [round($h * 360, 1), round($s * 100, 1), round($l * 100, 1)]);
return new Hsl($code);
}

Expand Down Expand Up @@ -143,36 +135,4 @@ public function __toString(): string
{
return 'rgb(' . implode(',', $this->values()) . ')';
}

/**
* @param float $max
* @param float $r
* @param float $g
* @param float $b
* @param float $d
*
* @return float
*/
private function getH($max, $r, $g, $b, $d): float
{
$h = match ($max) {
$r => ($g - $b) / $d + ($g < $b ? 6 : 0),
$g => ($b - $r) / $d + 2,
$b => ($r - $g) / $d + 4,
default => $max,
};
return $h / 6;
}

/**
* @return array
*/
private function getHValues(): array
{
[$r, $g, $b] = $values = array_map(function ($value) {
return $value / 255;
}, $this->values());
[$min, $max] = [min($values), max($values)];
return [$r, $g, $b, $min, $max];
}
}
6 changes: 4 additions & 2 deletions src/Color/Rgba.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function validate(string $code): bool|string
$color = "{$color},1.0";
}
$color = $this->fixPrecision($color);
if (preg_match($this->validationRules(), $color, $matches)) {
if (preg_match('/^(\d{1,3}),(\d{1,3}),(\d{1,3}),(\d\.\d{1,})$/', $color, $matches)) {
if ($matches[1] > 255 || $matches[2] > 255 || $matches[3] > 255 || $matches[4] > 1) {
return false;
}
Expand Down Expand Up @@ -118,7 +118,9 @@ public function toHsl(): Hsl
*/
public function toHsla(): Hsla
{
return $this->toHsl()->toHsla()->alpha($this->alpha());
[$h, $s, $l] = $this->getHslValues();
[$h, $s, $l, $a] = [round($h * 360, 1), round($s * 100, 1), round($l * 100, 1), $this->alpha];
return new Hsla("{$h},{$s}%,{$l}%,{$a}");
}

/**
Expand Down
10 changes: 1 addition & 9 deletions src/Traits/AlphaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ public function alpha($alpha = null): float|static
return $this->alpha;
}

/**
* @return string
*/
protected function validationRules(): string
{
return '/^(\d{1,3}),(\d{1,3}),(\d{1,3}),(\d\.\d{1,})$/';
}

/**
* @param $color
*
Expand Down Expand Up @@ -61,6 +53,6 @@ protected function alphaHexToFloat(string $alpha): float
*/
protected function alphaFloatToHex(float $alpha): string
{
return dechex($alpha * 255);
return dechex((int) ($alpha * 255));
}
}
13 changes: 12 additions & 1 deletion src/Traits/HsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function validate(string $code): bool|string
{
[$class, $index] = property_exists($this, 'lightness') ? ['hsl', 2] : ['hsv', 3];
$color = str_replace([$class, '(', ')', ' ', '%'], '', DefinedColor::find($code, $index));
if (preg_match('/^(\d{1,3}),(\d{1,3}),(\d{1,3})$/', $color, $matches)) {
if (preg_match($this->validationRules(), $color, $matches)) {
if ($matches[1] > 360 || $matches[2] > 100 || $matches[3] > 100) {
return false;
}
Expand All @@ -34,6 +34,17 @@ protected function validate(string $code): bool|string
return false;
}

/**
* @return string
*/
protected function validationRules(): string
{
if (property_exists($this, 'alpha')) {
return '/^(\d{1,3}(?:\.\d+)?),(\d{1,3}%?(?:\.\d+)?),(\d{1,3}%?(?:\.\d+)?),(\d+(?:\.\d+)?)$/';
}
return '/^(\d{1,3}(?:\.\d+)?),(\d{1,3}%?(?:\.\d+)?),(\d{1,3}%?(?:\.\d+)?)$/';
}

/**
* @param float|string $hue
*
Expand Down
63 changes: 58 additions & 5 deletions src/Traits/RgbTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,20 @@ public function blue($blue = null): float|int|string|static
return !empty($this->castsInteger) ? (int) $this->blue : $this->blue;
}

/**
* @return array
*/
public function rgbValues(): array
{
return [$this->red(), $this->green(), $this->blue()];
}

/**
* @return array
*/
public function values(): array
{
return [
$this->red(),
$this->green(),
$this->blue()
];
return $this->rgbValues();
}

/**
Expand All @@ -86,4 +90,53 @@ protected function validateAndSet(string $property, float|int|string $value): vo
$this->{$property} = preg_match('/^[a-f0-9]{2}$/i', $value) ? $value : $this->{$property};
}
}

/**
* @return array
*/
protected function getHValues(): array
{
[$r, $g, $b] = $values = array_map(function ($value) {
return $value / 255;
}, $this->rgbValues());
[$min, $max] = [min($values), max($values)];
return [$r, $g, $b, $min, $max];
}

/**
* @return array
*/
protected function getHslValues(): array
{
[$r, $g, $b, $min, $max] = $this->getHValues();
$l = ($max + $min) / 2;
if ($max === $min) {
$h = $s = 0;
} else {
$d = $max - $min;
$s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min);
$h = $this->getH($max, $r, $g, $b, $d);
}
return [$h, $s, $l];
}

/**
* @param float $max
* @param float $r
* @param float $g
* @param float $b
* @param float $d
*
* @return float
*/
protected function getH($max, $r, $g, $b, $d): float
{
$h = match ($max) {
$r => ($g - $b) / $d + ($g < $b ? 6 : 0),
$g => ($b - $r) / $d + 2,
$b => ($r - $g) / $d + 4,
default => $max,
};
return $h / 6;
}
}
71 changes: 71 additions & 0 deletions tests/Color/AlphaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace OzdemirBurak\Iris\Tests\Color;

use OzdemirBurak\Iris\Color\Cmyk;
use OzdemirBurak\Iris\Color\Hex;
use OzdemirBurak\Iris\Color\Hexa;
use OzdemirBurak\Iris\Color\Hsl;
use OzdemirBurak\Iris\Color\Hsla;
use OzdemirBurak\Iris\Color\Hsv;
use OzdemirBurak\Iris\Color\Rgb;
use OzdemirBurak\Iris\Color\Rgba;
use PHPUnit\Framework\TestCase;

class AlphaTest extends TestCase
{
/**
* @group alpha
*/
public function testRgb()
{
[$rgba, $hex] = $this->getBaseValues();
$this->assertEquals($hex, $rgba->toRgb()->toHex());
$this->assertEquals($hex, $rgba->toHex());
}
/**
* @group alpha
*/
public function testHexa()
{
[$rgba, $hex] = $this->getBaseValues();
$this->assertEquals($hex, $rgba->toHexa()->toHex());
}
/**
* @group alpha
*/
public function testHsl()
{
[$rgba, $hex] = $this->getBaseValues();
$this->assertEquals($hex, $rgba->toHsl()->toHex());
}

/**
* @group alpha
*/
public function testHsla()
{
[$rgba, $hex] = $this->getBaseValues();
$this->assertEquals($hex, $rgba->toHsla()->toHex());
}

/**
* @group alpha
*/
public function testIssueValues()
{
[$rgb, $hsl] = [new Rgb('127,127,127'), new Hsl('0, 0%, 49.8%')];
$this->assertEquals($rgb->toHsl(), $hsl);
[$rgba, $hex] = $this->getBaseValues();
$this->assertEquals($rgba->toHsla(), new Hsla('0, 0%, 40%, 0.5'));

}

/**
* @return array
*/
protected function getBaseValues(): array
{
return [new Rgba('rgba(102, 102, 102, 0.5)'), new Hex('b2b2b2')];
}
}
1 change: 0 additions & 1 deletion tests/Color/RgbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function testInvalidColor()
new Rgb('333,0,666');
}


/**
* @group rgb-construction
*/
Expand Down

0 comments on commit f6f9069

Please sign in to comment.