/
Color.php
264 lines (232 loc) · 6.52 KB
/
Color.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
declare(strict_types=1);
/**
* Thermage - Totally RAD Terminal styling for PHP! (https://digital.flextype.org/thermage/)
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
*
* Licensed under The MIT License.
*
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*/
namespace Thermage\Base;
use InvalidArgumentException;
use function array_keys;
use function array_merge;
use function Glowy\Arrays\arrays as collection;
use function Glowy\Strings\strings;
use function getenv;
use function hexdec;
use function implode;
use function intval;
use function max;
use function min;
use function mt_rand;
use function preg_match;
use function round;
use function sprintf;
use function substr;
use function Thermage\terminal;
final class Color
{
/**
* COLORS
*
* @access private
*/
private const COLORS = [
'black' => 0,
'red' => 1,
'green' => 2,
'yellow' => 3,
'blue' => 4,
'magenta' => 5,
'cyan' => 6,
'white' => 7,
'default' => 9,
];
/**
* BRIGHT COLORS
*
* @access private
*/
private const BRIGHT_COLORS = [
'gray' => 0,
'bright-red' => 1,
'bright-green' => 2,
'bright-yellow' => 3,
'bright-blue' => 4,
'bright-magenta' => 5,
'bright-cyan' => 6,
'bright-white' => 7,
];
/**
* Set text foreground color.
*
* @param string $value Value.
* @param string $color Color.
*
* @return string Returns text with applied foreground color.
*
* @access public
*/
public function applyForegroundColor(string $value, string $color): string
{
$setCodes = implode(';', collection($this->parseColor($color))->toArray());
$unsetCodes = implode(';', collection(['39'])->toArray());
return sprintf(terminal()->getCsi() . '%sm', $setCodes) . $value . sprintf(terminal()->getCsi() . '%sm', $unsetCodes);
}
/**
* Set text background color.
*
* @param string $value Value.
* @param string $color Color.
*
* @return string Returns text with applied background color.
*
* @access public
*/
public function applyBackgroundColor(string $value, string $color): string
{
$setCodes = implode(';', collection($this->parseColor($color, true))->toArray());
$unsetCodes = implode(';', collection(['49'])->toArray());
return sprintf(terminal()->getCsi() . '%sm', $setCodes) . $value . sprintf(terminal()->getCsi() . '%sm', $unsetCodes);
}
/**
* Get random HEX color.
*
* @return string Returns random HEX color.
*
* @access public
*/
public function getRandomHexColor(): string
{
return sprintf('#%06X', mt_rand(0, 0xFFFFFF));
}
/**
* Get random RGB color.
*
* @return array Returns random RGB color.
*
* @access public
*/
public function getRandomRgbColor(): array
{
return [
'r' => mt_rand(0, 255),
'g' => mt_rand(0, 255),
'b' => mt_rand(0, 255),
];
}
/**
* Convert RGB Color to HEX.
*
* @param string $color Color.
*
* @return string Color.
*
* @access private
*/
public function convertRgbColorToHex(string $color): string
{
if (preg_match('/(\d{1,3})\,?\s?(\d{1,3})\,?\s?(\d{1,3})/', $color, $matches)) {
$color = sprintf('%02x%02x%02x', $matches[1], $matches[2], $matches[3]);
}
return $color;
}
/**
* Parse color.
*
* @param string $color Color.
* @param bool $background Is color for background.
*
* @return string Returns parsed color.
*
* @access public
*/
private function parseColor(string $color, bool $background = false): string
{
if ($color === '') {
return '';
}
if (strings($color)->startsWith('rgb(') && strings($color)->endsWith(')')) {
$color = '#' . $this->convertRgbColorToHex($color);
}
if (strings($color)->startsWith('#')) {
$color = substr($color, 1);
if (strings($color)->length() === 3) {
$color = $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2];
}
if (strings($color)->length() !== 6) {
throw new InvalidArgumentException(sprintf('Invalid "%s" color.', $color));
}
return ($background ? '4' : '3') . $this->convertHexColorToAnsi(hexdec($color));
}
if (isset(self::COLORS[$color])) {
return ($background ? '4' : '3') . self::COLORS[$color];
}
if (isset(self::BRIGHT_COLORS[$color])) {
return ($background ? '10' : '9') . self::BRIGHT_COLORS[$color];
}
throw new InvalidArgumentException(sprintf('Invalid "%s" color; expected one of (%s).', $color, implode(', ', array_merge(array_keys(self::COLORS), array_keys(self::BRIGHT_COLORS)))));
}
/**
* Convert HEX Color to ANSI.
*
* @param int $color Color.
*
* @return string Color.
*
* @access private
*/
private function convertHexColorToAnsi(int $color): string
{
$r = ($color >> 16) & 255;
$g = ($color >> 8) & 255;
$b = $color & 255;
if (getenv('COLORTERM') !== 'truecolor') {
return (string) $this->degradeHexColorToAnsi($r, $g, $b);
}
return sprintf('8;2;%d;%d;%d', $r, $g, $b);
}
/**
* Degrade HEX Color to ANSI.
*
* @param int $r Red.
* @param int $g Green.
* @param int $b Blue.
*
* @return int Color.
*
* @access private
*/
private function degradeHexColorToAnsi(int $r, int $g, int $b): int
{
if (0 === round($this->getSaturation($r, $g, $b) / 50)) {
return 0;
}
return (round($b / 255) << 2) | (round($g / 255) << 1) | round($r / 255);
}
/**
* Get saturation
*
* @param int $r Red.
* @param int $g Green.
* @param int $b Blue.
*
* @return int Saturation.
*
* @access private
*/
private function getSaturation(int $r, int $g, int $b): int
{
$r /= 255;
$g /= 255;
$b /= 255;
$v = max($r, $g, $b);
if (0 === $diff = $v - min($r, $g, $b)) {
return 0;
}
return intval((int) $diff * 100 / $v);
}
}