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
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ composer require macocci7/bash-colorizer
> <a id="note5"></a>
*5: Partially effective<br>

e.g.) on VSCode Terminal
e.g.) on VSCode Terminal (theme:`Dark Modern`)

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

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


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

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

e.g.) foreground colors on VSCode Terminal:
e.g.) foreground colors on VSCode Terminal (theme:`Dark Modern`):

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

e.g.) background colors on VSCode Terminal:
e.g.) background colors on VSCode Terminal (theme:`Dark Modern`):

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

e.g.) underline colors on VSCode Terminal:
e.g.) underline colors on VSCode Terminal (theme:`Dark Modern`):

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

- 24bit (16777216) colors:

e.g.) foreground colors on VSCode Terminal:
e.g.) foreground colors on VSCode Terminal (theme:`Dark Modern`):

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

e.g.) background colors on VSCode Terminal:
e.g.) background colors on VSCode Terminal (theme:`Dark Modern`):

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

e.g.) underline colors on VSCode Terminal:
e.g.) underline colors on VSCode Terminal (theme:`Dark Modern`):

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

Expand All @@ -350,6 +350,7 @@ Example codes are in [playground](playground/) directory.
- [underline_256colors.php](playground/underline_256colors.php)
- [underline_24bitcolors.php](playground/underline_24bitcolors.php)
- [readable.php](playground/readable.php)
- [converter.php](playground/converter.php)

## 8. LICENSE

Expand Down
30 changes: 30 additions & 0 deletions playground/converter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use Macocci7\BashColorizer\Colorizer;
use Macocci7\BashColorizer\Converter;

// hex to rgb
$hex = '#abcdef';
$rgb = Converter::decimal($hex);

echo sprintf(
"%s => [%s, %s, %s]\n",
Colorizer::foreground($hex)->encode($hex),
Colorizer::foreground('red')->encode($rgb[0]),
Colorizer::foreground('green')->encode($rgb[1]),
Colorizer::foreground('blue')->encode($rgb[2]),
);

// decimal to rgb
$rgb = [254, 220, 186];
$hex = Converter::hex($rgb);

echo sprintf(
"[%s, %s, %s] => %s\n",
Colorizer::foreground('red')->encode($rgb[0]),
Colorizer::foreground('green')->encode($rgb[1]),
Colorizer::foreground('blue')->encode($rgb[2]),
Colorizer::foreground($hex)->encode($hex),
);
23 changes: 21 additions & 2 deletions src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Macocci7\BashColorizer;

use Macocci7\BashColorizer\Filters\Filter;

class Converter
{
/**
Expand All @@ -11,14 +13,14 @@ public static function decimal(string $rgb): array
{
$patternS = '/^#([0-9a-f])([0-9a-f])([0-9a-f])$/';
$patternL = '/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/';
if (preg_match($patternS, $rgb, $matches)) {
if (preg_match($patternS, strtolower($rgb), $matches)) {
// short format (#rgb)
return [
hexdec($matches[1] . $matches[1]),
hexdec($matches[2] . $matches[2]),
hexdec($matches[3] . $matches[3]),
];
} elseif (preg_match($patternL, $rgb, $matches)) {
} elseif (preg_match($patternL, strtolower($rgb), $matches)) {
// long format (#rrggbb)
return [
hexdec($matches[1]),
Expand All @@ -28,4 +30,21 @@ public static function decimal(string $rgb): array
}
return [];
}

/**
* @param int[] $rgb
*/
public static function hex(array $rgb): string|null
{
if (empty($rgb)) {
return null;
}
$rgb = Filter::rgb($rgb);
return sprintf(
"#%02s%02s%02s",
dechex($rgb[0]),
dechex($rgb[1]),
dechex($rgb[2]),
);
}
}
31 changes: 31 additions & 0 deletions tests/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@ public static function provide_decimal_can_return_correct_array(): array
'#000' => ['rgb' => '#000', 'expected' => [0, 0, 0]],
'#123' => ['rgb' => '#123', 'expected' => [17, 34, 51]],
'#fed' => ['rgb' => '#fed', 'expected' => [255, 238, 221]],
'#FED' => ['rgb' => '#FED', 'expected' => [255, 238, 221]],
'#19f' => ['rgb' => '#19f', 'expected' => [17, 153, 255]],
'#19F' => ['rgb' => '#19F', 'expected' => [17, 153, 255]],
'#000000' => ['rgb' => '#000000', 'expected' => [0, 0, 0]],
'#123456' => ['rgb' => '#123456', 'expected' => [18, 52, 86]],
'#ffeedd' => ['rgb' => '#ffeedd', 'expected' => [255, 238, 221]],
'#FFEEDD' => ['rgb' => '#FFEEDD', 'expected' => [255, 238, 221]],
'#123abc' => ['rgb' => '#123abc', 'expected' => [18, 58, 188]],
'#123ABC' => ['rgb' => '#123abc', 'expected' => [18, 58, 188]],

'fff' => ['rgb' => 'fff', 'expected' => []],
'fffffff' => ['rgb' => 'fffffff', 'expected' => []],
'#' => ['rgb' => '#', 'expected' => []],
'#1' => ['rgb' => '#1', 'expected' => []],
'#12' => ['rgb' => '#12', 'expected' => []],
'#efg' => ['rgb' => '#efg', 'expected' => []],
'#EFG' => ['rgb' => '#EFG', 'expected' => []],
'#1234' => ['rgb' => '#1234', 'expected' => []],
'#12345' => ['rgb' => '#12345', 'expected' => []],
'#bcdefg' => ['rgb' => '#bcdefg', 'expected' => []],
'#BCDEFG' => ['rgb' => '#BCDEFG', 'expected' => []],
'#1234567' => ['rgb' => '#1234567', 'expected' => []],
'#abcdefg' => ['rgb' => '#abcdefg', 'expected' => []],
];
Expand All @@ -39,4 +47,27 @@ public function test_decimal_can_return_correct_array(string $rgb, array $expect
{
$this->assertSame($expected, Converter::decimal($rgb));
}

public static function provide_hex_can_return_correct_hex_code(): array
{
return [
'empty' => ['rgb' => [], 'expected' => null],
'[0]' => ['rgb' => [0], 'expected' => '#000000'],
'[1]' => ['rgb' => [1], 'expected' => '#010000'],
'[1, 2]' => ['rgb' => [1, 2], 'expected' => '#010200'],
'[1, 2, 3]' => ['rgb' => [1, 2, 3], 'expected' => '#010203'],
'[0, 0, 10]' => ['rgb' => [0, 0, 10], 'expected' => '#00000a'],
'[0, 0, 171]' => ['rgb' => [0, 0, 171], 'expected' => '#0000ab'],
'[0, 171, 205]' => ['rgb' => [0, 171, 205], 'expected' => '#00abcd'],
'[171, 205, 239]' => ['rgb' => [171, 205, 239], 'expected' => '#abcdef'],
'[-1, 0, 1]' => ['rgb' => [-1, 0, 1], 'expected' => '#000001'],
'[254, 255, 256]' => ['rgb' => [254, 255, 256], 'expected' => '#feffff'],
];
}

#[DataProvider('provide_hex_can_return_correct_hex_code')]
public function test_hex_can_return_correct_hex_code(array $rgb, string|null $expected): void
{
$this->assertSame($expected, Converter::hex($rgb));
}
}
File renamed without changes.