Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Helpers/Weight.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use DragonCode\WhichColor\Data\RGB;

use function abs;

class Weight
{
protected array $values = [
Expand Down
30 changes: 14 additions & 16 deletions src/Services/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

/**
Expand All @@ -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);
Expand Down
19 changes: 12 additions & 7 deletions src/Services/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
Expand All @@ -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)
);
}

Expand Down