|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
namespace AlecRabbit\Spinner\Extras\Color\A; |
|
7
|
|
|
|
|
8
|
|
|
use AlecRabbit\Spinner\Contract\Option\OptionStyleMode; |
|
9
|
|
|
use AlecRabbit\Spinner\Exception\InvalidArgumentException; |
|
10
|
|
|
use AlecRabbit\Spinner\Extras\Color\Ansi4Color; |
|
11
|
|
|
use AlecRabbit\Spinner\Extras\Color\Ansi8Color; |
|
12
|
|
|
use AlecRabbit\Spinner\Extras\Color\Mixin\Ansi8ColorTableTrait; |
|
13
|
|
|
|
|
14
|
|
|
abstract class AColorToAnsiCodeConverter |
|
15
|
|
|
{ |
|
16
|
|
|
use Ansi8ColorTableTrait; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct( |
|
19
|
|
|
protected OptionStyleMode $styleMode, |
|
20
|
|
|
) { |
|
21
|
|
|
self::assert($this); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
protected static function assert(self $obj): void |
|
25
|
|
|
{ |
|
26
|
|
|
if ($obj->styleMode === OptionStyleMode::NONE) { |
|
27
|
|
|
throw new InvalidArgumentException( |
|
28
|
|
|
sprintf( |
|
29
|
|
|
'Unsupported style mode "%s".', |
|
30
|
|
|
$obj->styleMode->name, |
|
|
|
|
|
|
31
|
|
|
) |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @throws InvalidArgumentException |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function normalize(string $color): string |
|
40
|
|
|
{ |
|
41
|
|
|
$color = strtolower($color); |
|
42
|
|
|
|
|
43
|
|
|
$color = str_replace('#', '', $color); |
|
44
|
|
|
|
|
45
|
|
|
if (strlen($color) === 3) { |
|
46
|
|
|
$color = $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->assertColor($color); |
|
50
|
|
|
|
|
51
|
|
|
return '#' . $color; |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @throws InvalidArgumentException |
|
56
|
|
|
*/ |
|
57
|
|
|
private function assertColor(array|string $color): void |
|
58
|
|
|
{ |
|
59
|
|
|
if ($color === '') { |
|
|
|
|
|
|
60
|
|
|
throw new InvalidArgumentException('Empty color string.'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (strlen($color) !== 6) { |
|
|
|
|
|
|
64
|
|
|
throw new InvalidArgumentException(sprintf('Invalid color: "#%s".', $color)); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @throws InvalidArgumentException |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function convert8(string $color): string |
|
72
|
|
|
{ |
|
73
|
|
|
$index = Ansi8Color::getIndex($color); |
|
74
|
|
|
|
|
75
|
|
|
if ($index) { |
|
|
|
|
|
|
76
|
|
|
return '8;5;' . $index; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->convertHexColorToAnsiColorCode($color); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
abstract protected function convertHexColorToAnsiColorCode(string $hexColor): string; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @throws InvalidArgumentException |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function convert4(string $color): string |
|
88
|
|
|
{ |
|
89
|
|
|
$index = Ansi4Color::getIndex($color); |
|
90
|
|
|
|
|
91
|
|
|
if ($index !== null) { |
|
92
|
|
|
return (string)$index; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this->convertHexColorToAnsiColorCode($color); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function toInt(string $color): int |
|
99
|
|
|
{ |
|
100
|
|
|
return (int)hexdec(str_replace('#', '', $color)); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|