Skip to content

Commit

Permalink
Remove PHP 7.1 support, allow more than two precision for alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
dtSniper authored and ozdemirburak committed Apr 14, 2020
1 parent 3f93afc commit 914eeef
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: php

php:
- 7.1
- 7.2
- 7.3
- 7.4

before_script:
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
],
"require": {
"php" : "~7.1"
"php" : "~7.2"
},
"require-dev": {
"phpunit/phpunit" : "~7.0|~8.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Color/Hex.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function validate($code)
*/
protected function initialize($color)
{
return list($this->red, $this->green, $this->blue) = str_split($color, 2);
return [$this->red, $this->green, $this->blue] = str_split($color, 2);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Color/Hsl.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Hsl extends BaseColor
*/
protected function initialize($color)
{
return list($this->hue, $this->saturation, $this->lightness) = explode(',', $color);
return [$this->hue, $this->saturation, $this->lightness] = explode(',', $color);
}

/**
Expand Down Expand Up @@ -59,7 +59,7 @@ public function toHsla()
*/
public function toHsv()
{
list($h, $s, $l) = $this->valuesInUnitInterval();
[$h, $s, $l] = $this->valuesInUnitInterval();
$t = $s * $l < 0.5 ? $l : 1 - $l;
$s = 2 * $t / ($l + $t);
$l += $t;
Expand Down
4 changes: 2 additions & 2 deletions src/Color/Hsla.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Hsla extends BaseColor
*/
protected function validate($code)
{
list($class, $index) = property_exists($this, 'lightness') ? ['hsl', 2] : ['hsv', 3];
[$class, $index] = property_exists($this, 'lightness') ? ['hsl', 2] : ['hsv', 3];
$color = str_replace(["{$class}a", '(', ')', ' ', '%'], '', DefinedColor::find($code, $index));
if (substr_count($color, ',') === 2) {
$color = "{$color},1.0";
Expand All @@ -40,7 +40,7 @@ protected function validate($code)
*/
protected function initialize($color)
{
list($this->hue, $this->saturation, $this->lightness, $this->alpha) = explode(',', $color);
[$this->hue, $this->saturation, $this->lightness, $this->alpha] = explode(',', $color);
$this->alpha = (double) $this->alpha;
}

Expand Down
18 changes: 9 additions & 9 deletions src/Color/Hsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Hsv extends BaseColor
*/
protected function initialize($color)
{
return list($this->hue, $this->saturation, $this->value) = explode(',', $color);
return [$this->hue, $this->saturation, $this->value] = explode(',', $color);
}

/**
Expand All @@ -47,7 +47,7 @@ public function toHex()
*/
public function toHsl()
{
list($h, $s, $v) = $this->valuesInUnitInterval();
[$h, $s, $v] = $this->valuesInUnitInterval();
$l = (2 - $s) * $v / 2;
$s = $l && $l < 1 ? $s * $v / ($l < 0.5 ? $l * 2 : 2 - $l * 2) : $s;
$code = implode(',', [round($h * 360), round($s * 100), round($l * 100)]);
Expand Down Expand Up @@ -77,30 +77,30 @@ public function toHsv()
*/
public function toRgb()
{
list($h, $s, $v) = $this->valuesInUnitInterval();
[$h, $s, $v] = $this->valuesInUnitInterval();
$i = floor($h * 6);
$f = $h * 6 - $i;
$p = $v * (1 - $s);
$q = $v * (1 - $f * $s);
$t = $v * (1 - (1 - $f) * $s);
switch ($i % 6) {
case 0:
list($r, $g, $b) = [$v, $t, $p];
[$r, $g, $b] = [$v, $t, $p];
break;
case 1:
list($r, $g, $b) = [$q, $v, $p];
[$r, $g, $b] = [$q, $v, $p];
break;
case 2:
list($r, $g, $b) = [$p, $v, $t];
[$r, $g, $b] = [$p, $v, $t];
break;
case 3:
list($r, $g, $b) = [$p, $q, $v];
[$r, $g, $b] = [$p, $q, $v];
break;
case 4:
list($r, $g, $b) = [$t, $p, $v];
[$r, $g, $b] = [$t, $p, $v];
break;
case 5:
list($r, $g, $b) = [$v, $p, $q];
[$r, $g, $b] = [$v, $p, $q];
break;
}
$code = implode(',', [round($r * 255), round($g * 255), round($b * 255)]);
Expand Down
10 changes: 5 additions & 5 deletions src/Color/Rgb.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function validate($code)
*/
protected function initialize($color)
{
return list($this->red, $this->green, $this->blue) = explode(',', $color);
return [$this->red, $this->green, $this->blue] = explode(',', $color);
}

/**
Expand All @@ -58,7 +58,7 @@ public function toHex()
*/
public function toHsl()
{
list($r, $g, $b, $min, $max) = $this->getHValues();
[$r, $g, $b, $min, $max] = $this->getHValues();
$l = ($max + $min) / 2;
if ($max === $min) {
$h = $s = 0;
Expand Down Expand Up @@ -86,7 +86,7 @@ public function toHsla()
*/
public function toHsv()
{
list($r, $g, $b, $min, $max) = $this->getHValues();
[$r, $g, $b, $min, $max] = $this->getHValues();
$v = $max;
$d = $max - $min;
$s = $max === 0 ? 0 : $d / $max;
Expand Down Expand Up @@ -153,10 +153,10 @@ private function getH($max, $r, $g, $b, $d)
*/
private function getHValues()
{
list($r, $g, $b) = $values = array_map(function ($value) {
[$r, $g, $b] = $values = array_map(function ($value) {
return $value / 255;
}, $this->values());
list($min, $max) = [min($values), max($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 @@ -41,11 +41,12 @@ protected function validate($code)
* @param string $color
*
* @return void
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
*/
protected function initialize($color)
{
$colors = explode(',', $color);
list($this->red, $this->green, $this->blue) = array_map('intval', $colors);
[$this->red, $this->green, $this->blue] = array_map('intval', $colors);
$this->alpha = (double) $colors[3];
$this->background = $this->defaultBackground();
}
Expand All @@ -69,7 +70,7 @@ public function values()
*/
public function toRgb()
{
list($red, $green, $blue) = array_map(function ($attribute) {
[$red, $green, $blue] = array_map(function ($attribute) {
$value = (1 - $this->alpha()) * $this->background->{$attribute}() + $this->alpha() * $this->{$attribute}();
return floor($value);
}, ['red', 'green', 'blue']);
Expand Down Expand Up @@ -141,6 +142,7 @@ public function background(Rgb $rgb)

/**
* @return \OzdemirBurak\Iris\Color\Rgb
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
*/
protected function defaultBackground()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/AlphaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function alpha($alpha = null)
*/
protected function validationRules()
{
return '/^(\d{1,3}),(\d{1,3}),(\d{1,3}),(\d\.\d{1,2})$/';
return '/^(\d{1,3}),(\d{1,3}),(\d{1,3}),(\d\.\d{1,})$/';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ trait HsTrait
*/
protected function validate($code)
{
list($class, $index) = property_exists($this, 'lightness') ? ['hsl', 2] : ['hsv', 3];
[$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 ($matches[1] > 360 || $matches[2] > 100 || $matches[3] > 100) {
Expand Down
8 changes: 4 additions & 4 deletions src/Traits/HslTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ public function lightness($lightness = null)
*/
public function convertToRgb()
{
list($h, $s, $l) = $this->valuesInUnitInterval();
[$h, $s, $l] = $this->valuesInUnitInterval();
if ($s === 0) {
$r = $g = $b = $l;
} else {
$q = $l < 0.5 ? $l * (1 + $s) :
$l + $s - $l * $s;
$p = 2 * $l - $q;
list($r, $g, $b) = [
$this->hueToRgb($p, $q, $h + 1/3),
[$r, $g, $b] = [
$this->hueToRgb($p, $q, $h + 1 / 3),
$this->hueToRgb($p, $q, $h),
$this->hueToRgb($p, $q, $h - 1/3)
$this->hueToRgb($p, $q, $h - 1 / 3)
];
}
$code = implode(',', [round($r * 255), round($g * 255), round($b * 255)]);
Expand Down
2 changes: 1 addition & 1 deletion tests/Color/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testRgbReturnType()
*/
public function testRgbaReturnType()
{
foreach (['rgba(255, 2, 56, 0.5)', '30, 53,122, 1', '112,112,122,0.3'] as $c) {
foreach (['rgba(255, 2, 56, 0.5)', '30, 53,122, 1', '112,112,122,0.3', '66,66,66,0.333'] as $c) {
$color = Factory::init($c);
$this->assertInstanceOf(Rgba::class, $color);
}
Expand Down

0 comments on commit 914eeef

Please sign in to comment.