diff --git a/src/Helpers/Weight.php b/src/Helpers/Weight.php index cad9954..9531056 100644 --- a/src/Helpers/Weight.php +++ b/src/Helpers/Weight.php @@ -6,6 +6,8 @@ use DragonCode\WhichColor\Data\RGB; +use function abs; + class Weight { protected array $values = [ diff --git a/src/Services/Color.php b/src/Services/Color.php index 3dcf82b..fe2e7e2 100644 --- a/src/Services/Color.php +++ b/src/Services/Color.php @@ -12,17 +12,17 @@ class Color /** * Coefficient / Number of mistakes / Count of items to check. * - * 0.00100 / 14% / 125 - * 0.00079 / 11% / 125 - * 0.00082 / 1.13% / 1331 - * 0.00084 / 0.45% / 1331 - * 0.000845 / 0.23% / 1331 - * 0.00085 / 0.23% / 1331 - * 0.00086 / 0.15% / 1331 - * 0.00087 / 0.15% / 1331 - * 0.00088 / 0.00% / 1331 - * 0.00096 / 16% / 125 - * 0.00092 / 17% / 125 + * - 0.00100 / 14% / 125 + * - 0.00079 / 11% / 125 + * - 0.00082 / 1.13% / 1331 + * - 0.00084 / 0.45% / 1331 + * - 0.000845 / 0.23% / 1331 + * - 0.00085 / 0.23% / 1331 + * - 0.00086 / 0.15% / 1331 + * - 0.00087 / 0.15% / 1331 + * - 0.00088 / 0.00% / 1331 + * - 0.00096 / 16% / 125 + * - 0.00092 / 17% / 125 */ protected float $weight = 0.00088; @@ -33,11 +33,9 @@ public function __construct( protected Converter $convert = new Converter ) {} - public function of(array|RGB|string|null $hex = null): self + public function of(array|RGB|string|null $hex = null): static { - $instance = new static; - - return $instance->setHex($hex); + return (new static)->setHex($hex); } /** @@ -56,7 +54,7 @@ public function lightIsBetter(): bool return ! $this->darkIsBetter(); } - public function setHex(array|RGB|string|null $hex): self + public function setHex(array|RGB|string|null $hex): static { if ($hex) { $this->rgb = $this->parseHex($hex); diff --git a/src/Services/Converter.php b/src/Services/Converter.php index 2d90e0e..d1277c6 100644 --- a/src/Services/Converter.php +++ b/src/Services/Converter.php @@ -4,11 +4,18 @@ namespace DragonCode\WhichColor\Services; +use const STR_PAD_LEFT; + use DragonCode\Support\Facades\Helpers\Arr; use DragonCode\Support\Facades\Helpers\Str; use DragonCode\Support\Facades\Instances\Instance; use DragonCode\WhichColor\Data\RGB; -use ReflectionException; + +use function dechex; +use function hexdec; +use function is_string; +use function str_pad; +use function str_split; class Converter { @@ -32,13 +39,11 @@ public function hex2rgb(array|RGB|string|null $hex = '#000000'): RGB /** * Convert RGB color to HEX code. - * - * @throws ReflectionException */ public function rgb2hex(array|RGB $rgb = []): string { return Arr::of($this->hex2rgb($rgb)->toArray()) - ->map(fn ($x) => str_pad(dechex($x), 2, '0', STR_PAD_LEFT)) + ->map(static fn ($x) => str_pad(dechex($x), 2, '0', STR_PAD_LEFT)) ->implode('') ->prepend('#') ->toString(); @@ -48,11 +53,11 @@ protected function parseString(string $hex): array { $hex = Str::after($hex, '#'); - $is_short = Str::length($hex) === 3; + $short = Str::length($hex) === 3; return array_map( - fn ($x) => hexdec($is_short ? $x . $x : $x), - str_split($hex, $is_short ? 1 : 2) + static fn ($x) => hexdec($short ? $x . $x : $x), + str_split($hex, $short ? 1 : 2) ); }