Skip to content

Commit e21b3dc

Browse files
authored
Merge pull request #16 from macocci7/add_support_for_uppercase_hex_codes
Add support for uppercase hex codes
2 parents 585ee6e + a21a46e commit e21b3dc

5 files changed

Lines changed: 91 additions & 10 deletions

File tree

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ composer require macocci7/bash-colorizer
280280
> <a id="note5"></a>
281281
*5: Partially effective<br>
282282

283-
e.g.) on VSCode Terminal
283+
e.g.) on VSCode Terminal (theme:`Dark Modern`)
284284

285285
<img src="arts/available_attributes.png" width="220" height="515" />
286286

@@ -302,36 +302,36 @@ See more: [Select Graphic Rendition parameters | ANSI escape code | Wikipedia](h
302302
- `default`
303303

304304

305-
e.g.) on VSCode Terminal
305+
e.g.) on VSCode Terminal (theme:`Dark Modern`)
306306
|Foregound Colors|Background Colors|
307307
|---|---|
308308
|<img src="arts/available_foreground_colors.png" with="240" height="216" />|<img src="arts/available_background_colors.png" with="240" height="216" />|
309309

310310
- 256 colors [ 0 - 255 ]: `foreground`/`background`/`underline`
311311

312-
e.g.) foreground colors on VSCode Terminal:
312+
e.g.) foreground colors on VSCode Terminal (theme:`Dark Modern`):
313313

314314
<img src="arts/foreground_256colors.png" width="500" height="254" />
315315

316-
e.g.) background colors on VSCode Terminal:
316+
e.g.) background colors on VSCode Terminal (theme:`Dark Modern`):
317317

318318
<img src="arts/background_256colors.png" width="500" height="256" />
319319

320-
e.g.) underline colors on VSCode Terminal:
320+
e.g.) underline colors on VSCode Terminal (theme:`Dark Modern`):
321321

322322
<img src="arts/underline_256colors.png" width="500" height="252" />
323323

324324
- 24bit (16777216) colors:
325325

326-
e.g.) foreground colors on VSCode Terminal:
326+
e.g.) foreground colors on VSCode Terminal (theme:`Dark Modern`):
327327

328328
<img src="arts/foreground_24bitcolors.png" width="500" height="412" />
329329

330-
e.g.) background colors on VSCode Terminal:
330+
e.g.) background colors on VSCode Terminal (theme:`Dark Modern`):
331331

332332
<img src="arts/background_24bitcolors.png" width="500" height="406"/>
333333

334-
e.g.) underline colors on VSCode Terminal:
334+
e.g.) underline colors on VSCode Terminal (theme:`Dark Modern`):
335335

336336
<img src="arts/underline_24bitcolors.png" width="500" height="410"/>
337337

@@ -350,6 +350,7 @@ Example codes are in [playground](playground/) directory.
350350
- [underline_256colors.php](playground/underline_256colors.php)
351351
- [underline_24bitcolors.php](playground/underline_24bitcolors.php)
352352
- [readable.php](playground/readable.php)
353+
- [converter.php](playground/converter.php)
353354

354355
## 8. LICENSE
355356

playground/converter.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use Macocci7\BashColorizer\Colorizer;
6+
use Macocci7\BashColorizer\Converter;
7+
8+
// hex to rgb
9+
$hex = '#abcdef';
10+
$rgb = Converter::decimal($hex);
11+
12+
echo sprintf(
13+
"%s => [%s, %s, %s]\n",
14+
Colorizer::foreground($hex)->encode($hex),
15+
Colorizer::foreground('red')->encode($rgb[0]),
16+
Colorizer::foreground('green')->encode($rgb[1]),
17+
Colorizer::foreground('blue')->encode($rgb[2]),
18+
);
19+
20+
// decimal to rgb
21+
$rgb = [254, 220, 186];
22+
$hex = Converter::hex($rgb);
23+
24+
echo sprintf(
25+
"[%s, %s, %s] => %s\n",
26+
Colorizer::foreground('red')->encode($rgb[0]),
27+
Colorizer::foreground('green')->encode($rgb[1]),
28+
Colorizer::foreground('blue')->encode($rgb[2]),
29+
Colorizer::foreground($hex)->encode($hex),
30+
);

src/Converter.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Macocci7\BashColorizer;
44

5+
use Macocci7\BashColorizer\Filters\Filter;
6+
57
class Converter
68
{
79
/**
@@ -11,14 +13,14 @@ public static function decimal(string $rgb): array
1113
{
1214
$patternS = '/^#([0-9a-f])([0-9a-f])([0-9a-f])$/';
1315
$patternL = '/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/';
14-
if (preg_match($patternS, $rgb, $matches)) {
16+
if (preg_match($patternS, strtolower($rgb), $matches)) {
1517
// short format (#rgb)
1618
return [
1719
hexdec($matches[1] . $matches[1]),
1820
hexdec($matches[2] . $matches[2]),
1921
hexdec($matches[3] . $matches[3]),
2022
];
21-
} elseif (preg_match($patternL, $rgb, $matches)) {
23+
} elseif (preg_match($patternL, strtolower($rgb), $matches)) {
2224
// long format (#rrggbb)
2325
return [
2426
hexdec($matches[1]),
@@ -28,4 +30,21 @@ public static function decimal(string $rgb): array
2830
}
2931
return [];
3032
}
33+
34+
/**
35+
* @param int[] $rgb
36+
*/
37+
public static function hex(array $rgb): string|null
38+
{
39+
if (empty($rgb)) {
40+
return null;
41+
}
42+
$rgb = Filter::rgb($rgb);
43+
return sprintf(
44+
"#%02s%02s%02s",
45+
dechex($rgb[0]),
46+
dechex($rgb[1]),
47+
dechex($rgb[2]),
48+
);
49+
}
3150
}

tests/ConverterTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,27 @@ public static function provide_decimal_can_return_correct_array(): array
1616
'#000' => ['rgb' => '#000', 'expected' => [0, 0, 0]],
1717
'#123' => ['rgb' => '#123', 'expected' => [17, 34, 51]],
1818
'#fed' => ['rgb' => '#fed', 'expected' => [255, 238, 221]],
19+
'#FED' => ['rgb' => '#FED', 'expected' => [255, 238, 221]],
20+
'#19f' => ['rgb' => '#19f', 'expected' => [17, 153, 255]],
21+
'#19F' => ['rgb' => '#19F', 'expected' => [17, 153, 255]],
1922
'#000000' => ['rgb' => '#000000', 'expected' => [0, 0, 0]],
2023
'#123456' => ['rgb' => '#123456', 'expected' => [18, 52, 86]],
2124
'#ffeedd' => ['rgb' => '#ffeedd', 'expected' => [255, 238, 221]],
25+
'#FFEEDD' => ['rgb' => '#FFEEDD', 'expected' => [255, 238, 221]],
26+
'#123abc' => ['rgb' => '#123abc', 'expected' => [18, 58, 188]],
27+
'#123ABC' => ['rgb' => '#123abc', 'expected' => [18, 58, 188]],
2228

2329
'fff' => ['rgb' => 'fff', 'expected' => []],
2430
'fffffff' => ['rgb' => 'fffffff', 'expected' => []],
2531
'#' => ['rgb' => '#', 'expected' => []],
2632
'#1' => ['rgb' => '#1', 'expected' => []],
2733
'#12' => ['rgb' => '#12', 'expected' => []],
2834
'#efg' => ['rgb' => '#efg', 'expected' => []],
35+
'#EFG' => ['rgb' => '#EFG', 'expected' => []],
2936
'#1234' => ['rgb' => '#1234', 'expected' => []],
3037
'#12345' => ['rgb' => '#12345', 'expected' => []],
3138
'#bcdefg' => ['rgb' => '#bcdefg', 'expected' => []],
39+
'#BCDEFG' => ['rgb' => '#BCDEFG', 'expected' => []],
3240
'#1234567' => ['rgb' => '#1234567', 'expected' => []],
3341
'#abcdefg' => ['rgb' => '#abcdefg', 'expected' => []],
3442
];
@@ -39,4 +47,27 @@ public function test_decimal_can_return_correct_array(string $rgb, array $expect
3947
{
4048
$this->assertSame($expected, Converter::decimal($rgb));
4149
}
50+
51+
public static function provide_hex_can_return_correct_hex_code(): array
52+
{
53+
return [
54+
'empty' => ['rgb' => [], 'expected' => null],
55+
'[0]' => ['rgb' => [0], 'expected' => '#000000'],
56+
'[1]' => ['rgb' => [1], 'expected' => '#010000'],
57+
'[1, 2]' => ['rgb' => [1, 2], 'expected' => '#010200'],
58+
'[1, 2, 3]' => ['rgb' => [1, 2, 3], 'expected' => '#010203'],
59+
'[0, 0, 10]' => ['rgb' => [0, 0, 10], 'expected' => '#00000a'],
60+
'[0, 0, 171]' => ['rgb' => [0, 0, 171], 'expected' => '#0000ab'],
61+
'[0, 171, 205]' => ['rgb' => [0, 171, 205], 'expected' => '#00abcd'],
62+
'[171, 205, 239]' => ['rgb' => [171, 205, 239], 'expected' => '#abcdef'],
63+
'[-1, 0, 1]' => ['rgb' => [-1, 0, 1], 'expected' => '#000001'],
64+
'[254, 255, 256]' => ['rgb' => [254, 255, 256], 'expected' => '#feffff'],
65+
];
66+
}
67+
68+
#[DataProvider('provide_hex_can_return_correct_hex_code')]
69+
public function test_hex_can_return_correct_hex_code(array $rgb, string|null $expected): void
70+
{
71+
$this->assertSame($expected, Converter::hex($rgb));
72+
}
4273
}

0 commit comments

Comments
 (0)