Skip to content

Commit fe602a9

Browse files
committed
:octocat: move ANSI color escape code function to QRString::ansi8()
1 parent a19b7f4 commit fe602a9

File tree

3 files changed

+55
-50
lines changed

3 files changed

+55
-50
lines changed

examples/text.php

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use chillerlan\QRCode\{QRCode, QROptions};
1212
use chillerlan\QRCode\Data\QRMatrix;
13-
use chillerlan\QRCode\Output\QROutputInterface;
13+
use chillerlan\QRCode\Output\{QROutputInterface, QRString};
1414

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

@@ -21,30 +21,30 @@
2121
$options->outputType = QROutputInterface::STRING_TEXT;
2222
$options->eol = "\n";
2323
$options->textLineStart = str_repeat(' ', 6);
24-
$options->textDark = ansi8('██', 253);
25-
$options->textLight = ansi8('░░', 253);
24+
$options->textDark = QRString::ansi8('██', 253);
25+
$options->textLight = QRString::ansi8('░░', 253);
2626
$options->moduleValues = [
2727
// finder
28-
QRMatrix::M_FINDER_DARK => ansi8('██', 124),
29-
QRMatrix::M_FINDER => ansi8('░░', 124),
30-
QRMatrix::M_FINDER_DOT => ansi8('██', 124),
28+
QRMatrix::M_FINDER_DARK => QRString::ansi8('██', 124),
29+
QRMatrix::M_FINDER => QRString::ansi8('░░', 124),
30+
QRMatrix::M_FINDER_DOT => QRString::ansi8('██', 124),
3131
// alignment
32-
QRMatrix::M_ALIGNMENT_DARK => ansi8('██', 2),
33-
QRMatrix::M_ALIGNMENT => ansi8('░░', 2),
32+
QRMatrix::M_ALIGNMENT_DARK => QRString::ansi8('██', 2),
33+
QRMatrix::M_ALIGNMENT => QRString::ansi8('░░', 2),
3434
// timing
35-
QRMatrix::M_TIMING_DARK => ansi8('██', 184),
36-
QRMatrix::M_TIMING => ansi8('░░', 184),
35+
QRMatrix::M_TIMING_DARK => QRString::ansi8('██', 184),
36+
QRMatrix::M_TIMING => QRString::ansi8('░░', 184),
3737
// format
38-
QRMatrix::M_FORMAT_DARK => ansi8('██', 200),
39-
QRMatrix::M_FORMAT => ansi8('░░', 200),
38+
QRMatrix::M_FORMAT_DARK => QRString::ansi8('██', 200),
39+
QRMatrix::M_FORMAT => QRString::ansi8('░░', 200),
4040
// version
41-
QRMatrix::M_VERSION_DARK => ansi8('██', 21),
42-
QRMatrix::M_VERSION => ansi8('░░', 21),
41+
QRMatrix::M_VERSION_DARK => QRString::ansi8('██', 21),
42+
QRMatrix::M_VERSION => QRString::ansi8('░░', 21),
4343
// dark module
44-
QRMatrix::M_DARKMODULE => ansi8('██', 53),
44+
QRMatrix::M_DARKMODULE => QRString::ansi8('██', 53),
4545
// data
46-
QRMatrix::M_DATA_DARK => ansi8('██', 166),
47-
QRMatrix::M_DATA => ansi8('░░', 166),
46+
QRMatrix::M_DATA_DARK => QRString::ansi8('██', 166),
47+
QRMatrix::M_DATA => QRString::ansi8('░░', 166),
4848
];
4949

5050

@@ -54,12 +54,3 @@
5454
echo "\n\n\n$out\n\n\n";
5555

5656
exit;
57-
58-
59-
// a little helper to a create proper ANSI 8-bit color escape sequence
60-
function ansi8(string $str, int $color, bool $background = false):string{
61-
$color = max(0, min($color, 255));
62-
$background = ($background ? 48 : 38);
63-
64-
return sprintf("\x1b[%s;5;%sm%s\x1b[0m", $background, $color, $str);
65-
}

src/Output/QRString.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace chillerlan\QRCode\Output;
1515

16-
use function implode, is_string, json_encode;
16+
use function implode, is_string, json_encode, max, min, sprintf;
1717
use const JSON_THROW_ON_ERROR;
1818

1919
/**
@@ -87,4 +87,21 @@ protected function json():string{
8787
return json_encode($this->matrix->getMatrix(), JSON_THROW_ON_ERROR);
8888
}
8989

90+
//
91+
92+
/**
93+
* a little helper to create a proper ANSI 8-bit color escape sequence
94+
*
95+
* @see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
96+
* @see https://en.wikipedia.org/wiki/Block_Elements
97+
*
98+
* @codeCoverageIgnore
99+
*/
100+
public static function ansi8(string $str, int $color, bool $background = null):string{
101+
$color = max(0, min($color, 255));
102+
$background = ($background === true) ? 48 : 38;
103+
104+
return sprintf("\x1b[%s;5;%sm%s\x1b[0m", $background, $color, $str);
105+
}
106+
90107
}

tests/Data/QRMatrixTest.php

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,41 +50,38 @@ public static function debugMatrix(QRMatrix $matrix):void{
5050
$opt->outputType = QROutputInterface::STRING_TEXT;
5151
$opt->eol = "\n";
5252
$opt->moduleValues = [
53-
// this is not ideal but it seems it's not possible anymore to colorize emoji via ansi codes
54-
// 🔴 🟠 🟡 🟢 🔵 🟣 ⚫️ ⚪️ 🟤
55-
// 🟥 🟧 🟨 🟩 🟦 🟪 ⬛ ⬜ 🟫
5653
// finder
57-
QRMatrix::M_FINDER_DARK => '🟥', // dark (true)
58-
QRMatrix::M_FINDER => '🔴', // light (false)
59-
QRMatrix::M_FINDER_DOT => '🟥', // finder dot, dark (true)
54+
QRMatrix::M_FINDER_DARK => QRString::ansi8('██', 124), // dark (true)
55+
QRMatrix::M_FINDER => QRString::ansi8('░░', 124), // light (false)
56+
QRMatrix::M_FINDER_DOT => QRString::ansi8('██', 124), // finder dot, dark (true)
6057
// alignment
61-
QRMatrix::M_ALIGNMENT_DARK => '🟧',
62-
QRMatrix::M_ALIGNMENT => '🟠',
58+
QRMatrix::M_ALIGNMENT_DARK => QRString::ansi8('██', 2),
59+
QRMatrix::M_ALIGNMENT => QRString::ansi8('░░', 2),
6360
// timing
64-
QRMatrix::M_TIMING_DARK => '🟨',
65-
QRMatrix::M_TIMING => '🟡',
61+
QRMatrix::M_TIMING_DARK => QRString::ansi8('██', 184),
62+
QRMatrix::M_TIMING => QRString::ansi8('░░', 184),
6663
// format
67-
QRMatrix::M_FORMAT_DARK => '🟪',
68-
QRMatrix::M_FORMAT => '🟣',
64+
QRMatrix::M_FORMAT_DARK => QRString::ansi8('██', 200),
65+
QRMatrix::M_FORMAT => QRString::ansi8('░░', 200),
6966
// version
70-
QRMatrix::M_VERSION_DARK => '🟩',
71-
QRMatrix::M_VERSION => '🟢',
67+
QRMatrix::M_VERSION_DARK => QRString::ansi8('██', 21),
68+
QRMatrix::M_VERSION => QRString::ansi8('░░', 21),
7269
// data
73-
QRMatrix::M_DATA_DARK => '🟦',
74-
QRMatrix::M_DATA => '🔵',
70+
QRMatrix::M_DATA_DARK => QRString::ansi8('██', 166),
71+
QRMatrix::M_DATA => QRString::ansi8('░░', 166),
7572
// dark module
76-
QRMatrix::M_DARKMODULE => '🟫',
73+
QRMatrix::M_DARKMODULE => QRString::ansi8('██', 53),
7774
// separator
78-
QRMatrix::M_SEPARATOR => '⚪️',
75+
QRMatrix::M_SEPARATOR => QRString::ansi8('░░', 219),
7976
// quiet zone
80-
QRMatrix::M_QUIETZONE => '',
77+
QRMatrix::M_QUIETZONE => QRString::ansi8('░░', 195),
8178
// logo space
82-
QRMatrix::M_LOGO => '',
79+
QRMatrix::M_LOGO => QRString::ansi8('░░', 105),
8380
// empty
84-
QRMatrix::M_NULL => '🟤',
81+
QRMatrix::M_NULL => QRString::ansi8('░░', 231),
8582
// data
86-
QRMatrix::M_TEST_DARK => '',
87-
QRMatrix::M_TEST => '⚫️',
83+
QRMatrix::M_TEST_DARK => QRString::ansi8('██', 127),
84+
QRMatrix::M_TEST => QRString::ansi8('░░', 127),
8885
];
8986

9087
$out = (new QRString($opt, $matrix))->dump();

0 commit comments

Comments
 (0)