Skip to content

Commit

Permalink
Remove PHP 7 support, will only support PHP 8. Added type hinting and…
Browse files Browse the repository at this point in the history
… return type declarations. Made BaseColor::back() and BaseColor::getColorModelName() public @rv1971. Use non-locale aware output for the alpha value @Jako. Updated .gitignore, and some code refactoring @kudashevs
  • Loading branch information
ozdemirburak committed Feb 8, 2022
1 parent 4724802 commit dd1ba40
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [ 7.3, 7.4, 8.0, 8.1 ]
php: [ 8.0, 8.1 ]
dependency-version: [ prefer-lowest, prefer-stable ]

name: P${{ matrix.php }} - ${{ matrix.dependency-version }}
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All Notable changes to `iris` will be documented in this file.

## 2022-02-08
- Remove PHP 7 support, will only support PHP 8
- Added type hinting and return type declarations
- Made BaseColor::back() and BaseColor::getColorModelName() public @rv1971
- Use non-locale aware output for the alpha value @Jako
- Updated .gitignore, and some code refactoring @kudashevs

## 2021-10-05
- Added Hexa support.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"php": "^7.3|^8.0"
"php": "^8.0"
},
"require-dev": {
"phpunit/phpunit" : "~8.0|~9.0",
Expand Down
42 changes: 21 additions & 21 deletions src/BaseColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,56 @@ abstract class BaseColor
/**
* @param string $code
*
* @return mixed
* @return bool|string
*/
abstract protected function validate($code);
abstract protected function validate(string $code): bool|string;

/**
* @param string $color
*
* @return mixed
* @return array
*/
abstract protected function initialize($color);
abstract protected function initialize(string $color): array;

/**
* @return array
*/
abstract public function values();
abstract public function values(): array;

/**
* @return \OzdemirBurak\Iris\Color\Hex
*/
abstract public function toHex();
abstract public function toHex(): Color\Hex;

/**
* @return \OzdemirBurak\Iris\Color\Hsl
*/
abstract public function toHsl();
abstract public function toHsl(): Color\Hsl;

/**
* @return \OzdemirBurak\Iris\Color\Hsla
*/
abstract public function toHsla();
abstract public function toHsla(): Color\Hsla;

/**
* @return \OzdemirBurak\Iris\Color\Hsv
*/
abstract public function toHsv();
abstract public function toHsv(): Color\Hsv;

/**
* @return \OzdemirBurak\Iris\Color\Rgb
*/
abstract public function toRgb();
abstract public function toRgb(): Color\Rgb;

/**
* @return \OzdemirBurak\Iris\Color\Rgba
*/
abstract public function toRgba();
abstract public function toRgba(): Color\Rgba;

/**
* @return string
*/
abstract public function __toString();
abstract public function __toString(): string;

/**
* Color constructor.
Expand All @@ -67,7 +67,7 @@ abstract public function __toString();
*
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
*/
public function __construct($code)
public function __construct(string $code)
{
if (($color = $this->validate($code)) === false) {
throw new InvalidColorException($this->getExceptionMessage() . ' => ' . $code);
Expand All @@ -80,7 +80,7 @@ public function __construct($code)
*
* @return mixed
*/
public function saturate($percent)
public function saturate(int $percent)
{
$color = $this->toHsl();
$saturation = $this->clamp(($color->saturation() + $percent) / 100);
Expand All @@ -92,7 +92,7 @@ public function saturate($percent)
*
* @return mixed
*/
public function desaturate($percent)
public function desaturate(int $percent)
{
$color = $this->toHsl();
$saturation = $this->clamp(($color->saturation() - $percent) / 100);
Expand All @@ -112,7 +112,7 @@ public function grayscale()
*
* @return mixed
*/
public function brighten($percent)
public function brighten(int $percent)
{
$percent *= -1;
$color = $this->toRgb();
Expand All @@ -127,7 +127,7 @@ public function brighten($percent)
*
* @return mixed
*/
public function lighten($percent)
public function lighten(int $percent)
{
$color = $this->toHsl();
$lightness = $this->clamp(($color->lightness() + $percent) / 100);
Expand All @@ -139,7 +139,7 @@ public function lighten($percent)
*
* @return mixed
*/
public function darken($percent)
public function darken(int $percent)
{
$color = $this->toHsl();
$lightness = $this->clamp(($color->lightness() - $percent) / 100);
Expand Down Expand Up @@ -170,7 +170,7 @@ public function isDark()
*
* @return mixed
*/
public function spin($percent)
public function spin(int $percent)
{
$color = $this->toHsl();
$hue = ($color->hue() + $percent) % 360;
Expand All @@ -183,7 +183,7 @@ public function spin($percent)
*
* @return mixed
*/
public function mix(BaseColor $color, $percent = 50)
public function mix(BaseColor $color, int $percent = 50)
{
$first = $this->toRgb();
$second = $color->toRgb();
Expand Down Expand Up @@ -290,7 +290,7 @@ public function back(BaseColor $color)
/**
* @return string
*/
protected function getExceptionMessage()
protected function getExceptionMessage(): string
{
return 'Invalid ' . strtoupper(substr(static::class, strrpos(static::class, '\\') + 1)) . ' value';
}
Expand Down
20 changes: 10 additions & 10 deletions src/Color/Hex.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Hex extends BaseColor
*
* @return string|bool
*/
protected function validate($code)
protected function validate(string $code): bool|string
{
$color = str_replace('#', '', DefinedColor::find($code));
if (strlen($color) === 3) {
Expand All @@ -29,23 +29,23 @@ protected function validate($code)
*
* @return array
*/
protected function initialize($color)
protected function initialize(string $color): array
{
return [$this->red, $this->green, $this->blue] = str_split($color, 2);
}

/**
* @return \OzdemirBurak\Iris\Color\Hex
*/
public function toHex()
public function toHex(): Hex
{
return $this;
}

/**
* @return \OzdemirBurak\Iris\Color\Hexa
*/
public function toHexa()
public function toHexa(): Hexa
{
return new Hexa((string)$this . 'FF');
}
Expand All @@ -54,7 +54,7 @@ public function toHexa()
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Hsl
*/
public function toHsl()
public function toHsl(): Hsl
{
return $this->toRgb()->toHsl();
}
Expand All @@ -63,7 +63,7 @@ public function toHsl()
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Hsla
*/
public function toHsla()
public function toHsla(): Hsla
{
return $this->toHsl()->toHsla();
}
Expand All @@ -72,7 +72,7 @@ public function toHsla()
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Hsv
*/
public function toHsv()
public function toHsv(): Hsv
{
return $this->toRgb()->toHsv();
}
Expand All @@ -81,7 +81,7 @@ public function toHsv()
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Rgb
*/
public function toRgb()
public function toRgb(): Rgb
{
$rgb = implode(',', array_map('hexdec', $this->values()));
return new Rgb($rgb);
Expand All @@ -91,15 +91,15 @@ public function toRgb()
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Rgba
*/
public function toRgba()
public function toRgba(): Rgba
{
return $this->toRgb()->toRgba();
}

/**
* @return string
*/
public function __toString()
public function __toString(): string
{
return '#' . implode('', $this->values());
}
Expand Down
22 changes: 11 additions & 11 deletions src/Color/Hexa.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Hexa extends BaseColor
*
* @return string|bool
*/
protected function validate($code)
protected function validate(string $code): bool|string
{
$color = str_replace('#', '', DefinedColor::find($code));
return preg_match('/^[a-f0-9]{6}([a-f0-9]{2})?$/i', $color) ? $color : false;
Expand All @@ -27,7 +27,7 @@ protected function validate($code)
*
* @return array
*/
protected function initialize($color)
protected function initialize(string $color): array
{
[$this->red, $this->green, $this->blue, $this->alpha] = array_merge(str_split($color, 2), ['ff']);
$this->alpha = $this->alphaHexToFloat($this->alpha ?? 'ff');
Expand All @@ -37,7 +37,7 @@ protected function initialize($color)
/**
* @return array
*/
public function values()
public function values(): array
{
return [
$this->red(),
Expand All @@ -50,15 +50,15 @@ public function values()
/**
* @return \OzdemirBurak\Iris\Color\Hex
*/
public function toHex()
public function toHex(): Hex
{
return new Hex(implode([$this->red(), $this->green(), $this->blue()]));
}

/**
* @return \OzdemirBurak\Iris\Color\Hexa
*/
public function toHexa()
public function toHexa(): Hexa
{
return $this;
}
Expand All @@ -67,7 +67,7 @@ public function toHexa()
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Hsl
*/
public function toHsl()
public function toHsl(): Hsl
{
return $this->toRgb()->toHsl();
}
Expand All @@ -76,7 +76,7 @@ public function toHsl()
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Hsla
*/
public function toHsla()
public function toHsla(): Hsla
{
return $this->toHsl()->toHsla()->alpha($this->alpha());
}
Expand All @@ -85,7 +85,7 @@ public function toHsla()
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Hsv
*/
public function toHsv()
public function toHsv(): Hsv
{
return $this->toRgb()->toHsv();
}
Expand All @@ -94,7 +94,7 @@ public function toHsv()
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Rgb
*/
public function toRgb()
public function toRgb(): Rgb
{
$rgb = implode(',', array_map('hexdec', [$this->red(), $this->green(), $this->blue()]));
return new Rgb($rgb);
Expand All @@ -104,15 +104,15 @@ public function toRgb()
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Rgba
*/
public function toRgba()
public function toRgba(): Rgba
{
return $this->toRgb()->toRgba()->alpha($this->alpha());
}

/**
* @return string
*/
public function __toString()
public function __toString(): string
{
[$r, $g, $b, $a] = $this->values();
return '#' . implode('', [$r, $g, $b, $this->alphaFloatToHex($a)]);
Expand Down
Loading

0 comments on commit dd1ba40

Please sign in to comment.