phpstan fixes with move away from __get to dedicated get

This commit is contained in:
Clemens Schwaighofer
2024-11-15 19:43:30 +09:00
parent 5213805a58
commit e5a9b149b1
12 changed files with 80 additions and 66 deletions

View File

@@ -112,6 +112,21 @@ class CieXyz
); );
} }
// MARK: helper convert any array to array{float, float, float}
/**
* This is a hack for phpstan until we write a proper matrix to class
* conversion wrapper function
*
* @param array<array<float|int>|float|int> $_array
* @return array{0:float,1:float,2:float}
*/
private static function convertArray(array $_array): array
{
/** @var array{0:float,1:float,2:float} */
return [$_array[0], $_array[1], $_array[2]];
}
// MARK: xyzD65 <-> xyzD50 // MARK: xyzD65 <-> xyzD50
/** /**
@@ -122,14 +137,14 @@ class CieXyz
*/ */
private static function xyzD65ToXyzD50(XYZ $xyz): XYZ private static function xyzD65ToXyzD50(XYZ $xyz): XYZ
{ {
return new XYZ(Math::multiplyMatrices( return new XYZ(self::convertArray(Math::multiplyMatrices(
a: [ a: [
[1.0479298208405488, 0.022946793341019088, -0.05019222954313557], [1.0479298208405488, 0.022946793341019088, -0.05019222954313557],
[0.029627815688159344, 0.990434484573249, -0.01707382502938514], [0.029627815688159344, 0.990434484573249, -0.01707382502938514],
[-0.009243058152591178, 0.015055144896577895, 0.7518742899580008], [-0.009243058152591178, 0.015055144896577895, 0.7518742899580008],
], ],
b: $xyz->returnAsArray(), b: $xyz->returnAsArray(),
), options: ["whitepoint" => 'D50']); )), options: ["whitepoint" => 'D50']);
} }
/** /**
@@ -140,14 +155,14 @@ class CieXyz
*/ */
private static function xyzD50ToXyxD65(XYZ $xyz): XYZ private static function xyzD50ToXyxD65(XYZ $xyz): XYZ
{ {
return new XYZ(Math::multiplyMatrices( return new XYZ(self::convertArray(Math::multiplyMatrices(
a: [ a: [
[0.9554734527042182, -0.023098536874261423, 0.0632593086610217], [0.9554734527042182, -0.023098536874261423, 0.0632593086610217],
[-0.028369706963208136, 1.0099954580058226, 0.021041398966943008], [-0.028369706963208136, 1.0099954580058226, 0.021041398966943008],
[0.012314001688319899, -0.020507696433477912, 1.3303659366080753], [0.012314001688319899, -0.020507696433477912, 1.3303659366080753],
], ],
b: $xyz->returnAsArray() b: $xyz->returnAsArray()
), options: ["whitepoint" => 'D65']); )), options: ["whitepoint" => 'D65']);
} }
// MARK: xyzD50 <-> Lab // MARK: xyzD50 <-> Lab
@@ -228,11 +243,11 @@ class CieXyz
]; ];
return new XYZ( return new XYZ(
array_map( self::convertArray(array_map(
fn ($k, $v) => $v * $d50[$k], fn ($k, $v) => $v * $d50[$k],
array_keys($xyz), array_keys($xyz),
array_values($xyz), array_values($xyz),
), )),
options: ["whitepoint" => 'D50'] options: ["whitepoint" => 'D50']
); );
} }
@@ -249,17 +264,17 @@ class CieXyz
private static function linRgbToXyzD65(RGB $rgb): XYZ private static function linRgbToXyzD65(RGB $rgb): XYZ
{ {
// if not linear, convert to linear // if not linear, convert to linear
if (!$rgb->linear) { if (!$rgb->get('linear')) {
$rgb = (new RGB($rgb->returnAsArray()))->toLinear(); $rgb = (new RGB($rgb->returnAsArray()))->toLinear();
} }
return new XYZ(Math::multiplyMatrices( return new XYZ(self::convertArray(Math::multiplyMatrices(
[ [
[0.41239079926595934, 0.357584339383878, 0.1804807884018343], [0.41239079926595934, 0.357584339383878, 0.1804807884018343],
[0.21263900587151027, 0.715168678767756, 0.07219231536073371], [0.21263900587151027, 0.715168678767756, 0.07219231536073371],
[0.01933081871559182, 0.11919477979462598, 0.9505321522496607], [0.01933081871559182, 0.11919477979462598, 0.9505321522496607],
], ],
$rgb->returnAsArray() $rgb->returnAsArray()
), options: ["whitepoint" => 'D65']); )), options: ["whitepoint" => 'D65']);
} }
/** /**
@@ -271,14 +286,14 @@ class CieXyz
private static function xyzD65ToLinRgb(XYZ $xyz): RGB private static function xyzD65ToLinRgb(XYZ $xyz): RGB
{ {
// xyz D65 to linrgb // xyz D65 to linrgb
return new RGB(Math::multiplyMatrices( return new RGB(self::convertArray(Math::multiplyMatrices(
a : [ a : [
[ 3.2409699419045226, -1.537383177570094, -0.4986107602930034 ], [ 3.2409699419045226, -1.537383177570094, -0.4986107602930034 ],
[ -0.9692436362808796, 1.8759675015077202, 0.04155505740717559 ], [ -0.9692436362808796, 1.8759675015077202, 0.04155505740717559 ],
[ 0.05563007969699366, -0.20397695888897652, 1.0569715142428786 ], [ 0.05563007969699366, -0.20397695888897652, 1.0569715142428786 ],
], ],
b : $xyz->returnAsArray() b : $xyz->returnAsArray()
), options: ["linear" => true]); )), options: ["linear" => true]);
} }
// MARK: xyzD65 <-> OkLab // MARK: xyzD65 <-> OkLab
@@ -291,14 +306,14 @@ class CieXyz
*/ */
private static function xyzD65ToOkLab(XYZ $xyz): Lab private static function xyzD65ToOkLab(XYZ $xyz): Lab
{ {
return new Lab(Math::multiplyMatrices( return new Lab(self::convertArray(Math::multiplyMatrices(
[ [
[0.2104542553, 0.7936177850, -0.0040720468], [0.2104542553, 0.7936177850, -0.0040720468],
[1.9779984951, -2.4285922050, 0.4505937099], [1.9779984951, -2.4285922050, 0.4505937099],
[0.0259040371, 0.7827717662, -0.8086757660], [0.0259040371, 0.7827717662, -0.8086757660],
], ],
array_map( array_map(
callback: fn ($v) => pow($v, 1 / 3), callback: fn ($v) => pow((float)$v, 1 / 3),
array: Math::multiplyMatrices( array: Math::multiplyMatrices(
a: [ a: [
[0.8190224432164319, 0.3619062562801221, -0.12887378261216414], [0.8190224432164319, 0.3619062562801221, -0.12887378261216414],
@@ -308,7 +323,7 @@ class CieXyz
b: $xyz->returnAsArray(), b: $xyz->returnAsArray(),
), ),
) )
), colorspace: 'OkLab'); )), colorspace: 'OkLab');
} }
/** /**
@@ -319,7 +334,7 @@ class CieXyz
*/ */
private static function okLabToXyzD65(Lab $lab): XYZ private static function okLabToXyzD65(Lab $lab): XYZ
{ {
return new XYZ(Math::multiplyMatrices( return new XYZ(self::convertArray(Math::multiplyMatrices(
a: [ a: [
[1.2268798733741557, -0.5578149965554813, 0.28139105017721583], [1.2268798733741557, -0.5578149965554813, 0.28139105017721583],
[-0.04057576262431372, 1.1122868293970594, -0.07171106666151701], [-0.04057576262431372, 1.1122868293970594, -0.07171106666151701],
@@ -337,7 +352,7 @@ class CieXyz
b: $lab->returnAsArray(), b: $lab->returnAsArray(),
), ),
), ),
), options: ["whitepoint" => 'D65']); )), options: ["whitepoint" => 'D65']);
} }
} }

View File

@@ -55,13 +55,13 @@ class Color
private static function __labToLch(Lab $lab): array private static function __labToLch(Lab $lab): array
{ {
// cieLab to cieLch // cieLab to cieLch
$a = $lab->a; $a = (float)$lab->get('a');
$b = $lab->b; $b = (float)$lab->get('b');
$hue = atan2($b, $a) * 180 / pi(); $hue = atan2($b, $a) * 180 / pi();
return [ return [
$lab->L, (float)$lab->get('L'),
sqrt($a ** 2 + $b ** 2), sqrt($a ** 2 + $b ** 2),
$hue >= 0 ? $hue : $hue + 360, $hue >= 0 ? $hue : $hue + 360,
]; ];
@@ -76,9 +76,9 @@ class Color
private static function __lchToLab(LCH $lch): array private static function __lchToLab(LCH $lch): array
{ {
return [ return [
$lch->L, (float)$lch->get('L'),
$lch->C * cos($lch->H * pi() / 180), // a (float)$lch->get('C') * cos((float)$lch->get('H') * pi() / 180), // a
$lch->C * sin($lch->H * pi() / 180), // b (float)$lch->get('C') * sin((float)$lch->get('H') * pi() / 180), // b
]; ];
} }
@@ -94,9 +94,9 @@ class Color
*/ */
public static function rgbToHsl(RGB $rgb): HSL public static function rgbToHsl(RGB $rgb): HSL
{ {
$red = $rgb->R / 255; $red = (float)$rgb->get('R') / 255;
$green = $rgb->G / 255; $green = (float)$rgb->get('G') / 255;
$blue = $rgb->B / 255; $blue = (float)$rgb->get('B') / 255;
$min = min($red, $green, $blue); $min = min($red, $green, $blue);
$max = max($red, $green, $blue); $max = max($red, $green, $blue);
@@ -147,9 +147,9 @@ class Color
*/ */
public static function hslToRgb(HSL $hsl): RGB public static function hslToRgb(HSL $hsl): RGB
{ {
$hue = $hsl->H; $hue = (float)$hsl->get('H');
$sat = $hsl->S; $sat = (float)$hsl->get('S');
$lum = $hsl->L; $lum = (float)$hsl->get('L');
// calc to internal convert value for hue // calc to internal convert value for hue
$hue = (1 / 360) * $hue; $hue = (1 / 360) * $hue;
// convert to internal 0-1 format // convert to internal 0-1 format
@@ -201,9 +201,9 @@ class Color
*/ */
public static function rgbToHsb(RGB $rgb): HSB public static function rgbToHsb(RGB $rgb): HSB
{ {
$red = $rgb->R / 255; $red = (float)$rgb->get('R') / 255;
$green = $rgb->G / 255; $green = (float)$rgb->get('G') / 255;
$blue = $rgb->B / 255; $blue = (float)$rgb->get('B') / 255;
$MAX = max($red, $green, $blue); $MAX = max($red, $green, $blue);
$MIN = min($red, $green, $blue); $MIN = min($red, $green, $blue);
@@ -246,9 +246,9 @@ class Color
*/ */
public static function hsbToRgb(HSB $hsb): RGB public static function hsbToRgb(HSB $hsb): RGB
{ {
$H = $hsb->H; $H = (float)$hsb->get('H');
$S = $hsb->S; $S = (float)$hsb->get('S');
$V = $hsb->B; $V = (float)$hsb->get('B');
// convert to internal 0-1 format // convert to internal 0-1 format
$S /= 100; $S /= 100;
$V /= 100; $V /= 100;
@@ -352,8 +352,8 @@ class Color
*/ */
public static function hslToHsb(HSL $hsl): HSB public static function hslToHsb(HSL $hsl): HSB
{ {
$saturation = $hsl->S / 100; $saturation = (float)$hsl->get('S') / 100;
$lightness = $hsl->L / 100; $lightness = (float)$hsl->get('L') / 100;
// if lightness is 0, then we cannot return convert to hsb // if lightness is 0, then we cannot return convert to hsb
$value = $lightness + $saturation * min($lightness, 1 - $lightness); $value = $lightness + $saturation * min($lightness, 1 - $lightness);
// print "Orig: " . print_r($hsl, true) . "\n"; // print "Orig: " . print_r($hsl, true) . "\n";
@@ -366,7 +366,7 @@ class Color
200 * (1 - $lightness / $value); 200 * (1 - $lightness / $value);
$value *= 100; $value *= 100;
return new HSB([ return new HSB([
$hsl->H, (float)$hsl->get('H'),
$saturation, $saturation,
$value, $value,
]); ]);
@@ -381,9 +381,9 @@ class Color
public static function hsbToHsl(HSB $hsb): HSL public static function hsbToHsl(HSB $hsb): HSL
{ {
// hsv/toHsl // hsv/toHsl
$hue = $hsb->H; $hue = (float)$hsb->get('H');
$saturation = $hsb->S / 100; $saturation = (float)$hsb->get('S') / 100;
$value = $hsb->B / 100; $value = (float)$hsb->get('B') / 100;
$lightness = $value * (1 - $saturation / 2); $lightness = $value * (1 - $saturation / 2);
// check for B/W // check for B/W
@@ -443,9 +443,9 @@ class Color
{ {
// hsv\Hwb // hsv\Hwb
return new HWB([ return new HWB([
$hsb->H, // hue, (float)$hsb->get('H'), // hue,
$hsb->B * (100 - $hsb->S) / 100, // 2: brightness, 1: saturation (float)$hsb->get('B') * (100 - (float)$hsb->get('S')) / 100, // 2: brightness, 1: saturation
100 - $hsb->B, 100 - (float)$hsb->get('B'),
]); ]);
} }
@@ -457,9 +457,9 @@ class Color
*/ */
public static function hwbToHsb(HWB $hwb): HSB public static function hwbToHsb(HWB $hwb): HSB
{ {
$hue = $hwb->H; $hue = (float)$hwb->get('H');
$whiteness = $hwb->W / 100; $whiteness = (float)$hwb->get('W') / 100;
$blackness = $hwb->B / 100; $blackness = (float)$hwb->get('B') / 100;
$sum = $whiteness + $blackness; $sum = $whiteness + $blackness;
// print "S: B/W: " . $sum . " /W: " . $whiteness . " /B: " . $blackness . "\n"; // print "S: B/W: " . $sum . " /W: " . $whiteness . " /B: " . $blackness . "\n";
@@ -469,7 +469,7 @@ class Color
$value = $whiteness / $sum * 100; $value = $whiteness / $sum * 100;
} else { } else {
$value = 1 - $blackness; $value = 1 - $blackness;
$saturation = $value === 0 ? 0 : (1 - $whiteness / $value) * 100; $saturation = $value === 0.0 ? 0 : (1 - $whiteness / $value) * 100;
$value *= 100; $value *= 100;
} }

View File

@@ -124,7 +124,7 @@ class HSB implements Interface\CoordinatesInterface
* @param string $name * @param string $name
* @return float * @return float
*/ */
public function __get(string $name): float|string|bool public function get(string $name): float|string|bool
{ {
$name = strtoupper($name); $name = strtoupper($name);
if (!property_exists($this, $name)) { if (!property_exists($this, $name)) {

View File

@@ -123,7 +123,7 @@ class HSL implements Interface\CoordinatesInterface
* @param string $name * @param string $name
* @return float * @return float
*/ */
public function __get(string $name): float|string|bool public function get(string $name): float|string|bool
{ {
if (!property_exists($this, $name)) { if (!property_exists($this, $name)) {
throw new \ErrorException('Creation of dynamic property is not allowed', 0); throw new \ErrorException('Creation of dynamic property is not allowed', 0);

View File

@@ -123,7 +123,7 @@ class HWB implements Interface\CoordinatesInterface
* @param string $name * @param string $name
* @return float * @return float
*/ */
public function __get(string $name): float|string|bool public function get(string $name): float|string|bool
{ {
if (!property_exists($this, $name)) { if (!property_exists($this, $name)) {
throw new \ErrorException('Creation of dynamic property is not allowed', 0); throw new \ErrorException('Creation of dynamic property is not allowed', 0);

View File

@@ -31,7 +31,7 @@ interface CoordinatesInterface
* @param string $name * @param string $name
* @return float * @return float
*/ */
public function __get(string $name): float|string|bool; public function get(string $name): float|string|bool;
/** /**
* Returns the color as array * Returns the color as array

View File

@@ -60,7 +60,7 @@ class LCH implements Interface\CoordinatesInterface
* set from array * set from array
* where 0: Lightness, 1: Chroma, 2: Hue * where 0: Lightness, 1: Chroma, 2: Hue
* *
* @param string|{0:float,1:float,2:float} $colors * @param string|array{0:float,1:float,2:float} $colors
* @param string $colorspace [default=''] * @param string $colorspace [default='']
* @param array<string,string> $options [default=[]] * @param array<string,string> $options [default=[]]
* @return self * @return self
@@ -145,7 +145,7 @@ class LCH implements Interface\CoordinatesInterface
* @param string $name * @param string $name
* @return float * @return float
*/ */
public function __get(string $name): float|string|bool public function get(string $name): float|string|bool
{ {
if (!property_exists($this, $name)) { if (!property_exists($this, $name)) {
throw new \ErrorException('Creation of dynamic property is not allowed', 0); throw new \ErrorException('Creation of dynamic property is not allowed', 0);
@@ -214,9 +214,9 @@ class LCH implements Interface\CoordinatesInterface
$string .= '(' $string .= '('
. $this->L . $this->L
. ' ' . ' '
. $this->c . $this->C
. ' ' . ' '
. $this->h . $this->H
. Utils::setOpacity($opacity) . Utils::setOpacity($opacity)
. ');'; . ');';

View File

@@ -151,7 +151,7 @@ class Lab implements Interface\CoordinatesInterface
* @param string $name * @param string $name
* @return float * @return float
*/ */
public function __get(string $name): float|string|bool public function get(string $name): float|string|bool
{ {
if (!property_exists($this, $name)) { if (!property_exists($this, $name)) {
throw new \ErrorException('Creation of dynamic property is not allowed', 0); throw new \ErrorException('Creation of dynamic property is not allowed', 0);

View File

@@ -111,7 +111,7 @@ class RGB implements Interface\CoordinatesInterface
* @param string $name * @param string $name
* @return float|bool * @return float|bool
*/ */
public function __get(string $name): float|string|bool public function get(string $name): float|string|bool
{ {
if (!property_exists($this, $name)) { if (!property_exists($this, $name)) {
throw new \ErrorException('Creation of dynamic property is not allowed', 0); throw new \ErrorException('Creation of dynamic property is not allowed', 0);

View File

@@ -118,7 +118,7 @@ class XYZ implements Interface\CoordinatesInterface
* @param string $name * @param string $name
* @return float * @return float
*/ */
public function __get(string $name): float|string|bool public function get(string $name): float|string|bool
{ {
if (!property_exists($this, $name)) { if (!property_exists($this, $name)) {
throw new \ErrorException('Creation of dynamic property is not allowed', 0); throw new \ErrorException('Creation of dynamic property is not allowed', 0);

View File

@@ -15,11 +15,11 @@ use CoreLibs\Convert\Math;
class Utils class Utils
{ {
/** @var int deviation allowed for valid data checks, small */ /** @var float deviation allowed for valid data checks, small */
public const EPSILON_SMALL = 0.000000000001; public const EPSILON_SMALL = 0.000000000001;
/** @var int deviation allowed for valid data checks, medium */ /** @var float deviation allowed for valid data checks, medium */
public const EPSILON_MEDIUM = 0.0000001; public const EPSILON_MEDIUM = 0.0000001;
/** @var int deviation allowed for valid data checks, big */ /** @var float deviation allowed for valid data checks, big */
public const ESPILON_BIG = 0.0001; public const ESPILON_BIG = 0.0001;
public static function compare(float $lower, float $value, float $upper, float $epslion): bool public static function compare(float $lower, float $value, float $upper, float $epslion): bool

View File

@@ -113,7 +113,6 @@ class Math
break; break;
case '==': case '==':
return self::equalWithEpsilon($value, $limit, $epsilon); return self::equalWithEpsilon($value, $limit, $epsilon);
break;
case '>': case '>':
if ($value > ($limit + $epsilon)) { if ($value > ($limit + $epsilon)) {
return true; return true;
@@ -148,7 +147,7 @@ class Math
if (!is_array($a[0] ?? null)) { if (!is_array($a[0] ?? null)) {
// $a is vector, convert to [[a, b, c, ...]] // $a is vector, convert to [[a, b, c, ...]]
$a = [ $a ]; $a = [$a];
} }
if (!is_array($b[0])) { if (!is_array($b[0])) {
@@ -164,7 +163,7 @@ class Math
// transpose $b: // transpose $b:
$bCols = array_map( $bCols = array_map(
callback: fn ($k) => \array_map( callback: fn ($k) => \array_map(
(fn ($i) => $i[$k]), (fn ($i) => is_array($i) ? $i[$k] : 0),
$b, $b,
), ),
array: array_keys($b[0]), array: array_keys($b[0]),
@@ -176,7 +175,7 @@ class Math
array_reduce( array_reduce(
array: $row, array: $row,
callback: fn ($a, $v, $i = null) => $a + $v * ( callback: fn ($a, $v, $i = null) => $a + $v * (
$col[$i ?? array_search($v, $row)] ?? 0 $col[$i ?? array_search($v, $row) ?: 0]
), ),
initial: 0, initial: 0,
) : ) :
@@ -198,7 +197,7 @@ class Math
if ($p === 1) { if ($p === 1) {
// Avoid [[a], [b], [c], ...]]: // Avoid [[a], [b], [c], ...]]:
return array_map( return array_map(
callback: fn ($v) => $v[0], callback: fn ($v) => $v[0] ?? 0,
array: $product, array: $product,
); );
} }