瀏覽代碼

:octocat: move ANSI color escape code function to QRString::ansi8()

smiley 2 年之前
父節點
當前提交
fe602a9ab2
共有 3 個文件被更改,包括 55 次插入50 次删除
  1. 17 26
      examples/text.php
  2. 18 1
      src/Output/QRString.php
  3. 20 23
      tests/Data/QRMatrixTest.php

+ 17 - 26
examples/text.php

@@ -10,7 +10,7 @@
 
 use chillerlan\QRCode\{QRCode, QROptions};
 use chillerlan\QRCode\Data\QRMatrix;
-use chillerlan\QRCode\Output\QROutputInterface;
+use chillerlan\QRCode\Output\{QROutputInterface, QRString};
 
 require_once __DIR__.'/../vendor/autoload.php';
 
@@ -21,30 +21,30 @@ $options->quietzoneSize = 2;
 $options->outputType    = QROutputInterface::STRING_TEXT;
 $options->eol           = "\n";
 $options->textLineStart = str_repeat(' ', 6);
-$options->textDark      = ansi8('██', 253);
-$options->textLight     = ansi8('░░', 253);
+$options->textDark      = QRString::ansi8('██', 253);
+$options->textLight     = QRString::ansi8('░░', 253);
 $options->moduleValues  = [
 	// finder
-	QRMatrix::M_FINDER_DARK    => ansi8('██', 124),
-	QRMatrix::M_FINDER         => ansi8('░░', 124),
-	QRMatrix::M_FINDER_DOT     => ansi8('██', 124),
+	QRMatrix::M_FINDER_DARK    => QRString::ansi8('██', 124),
+	QRMatrix::M_FINDER         => QRString::ansi8('░░', 124),
+	QRMatrix::M_FINDER_DOT     => QRString::ansi8('██', 124),
 	// alignment
-	QRMatrix::M_ALIGNMENT_DARK => ansi8('██', 2),
-	QRMatrix::M_ALIGNMENT      => ansi8('░░', 2),
+	QRMatrix::M_ALIGNMENT_DARK => QRString::ansi8('██', 2),
+	QRMatrix::M_ALIGNMENT      => QRString::ansi8('░░', 2),
 	// timing
-	QRMatrix::M_TIMING_DARK    => ansi8('██', 184),
-	QRMatrix::M_TIMING         => ansi8('░░', 184),
+	QRMatrix::M_TIMING_DARK    => QRString::ansi8('██', 184),
+	QRMatrix::M_TIMING         => QRString::ansi8('░░', 184),
 	// format
-	QRMatrix::M_FORMAT_DARK    => ansi8('██', 200),
-	QRMatrix::M_FORMAT         => ansi8('░░', 200),
+	QRMatrix::M_FORMAT_DARK    => QRString::ansi8('██', 200),
+	QRMatrix::M_FORMAT         => QRString::ansi8('░░', 200),
 	// version
-	QRMatrix::M_VERSION_DARK   => ansi8('██', 21),
-	QRMatrix::M_VERSION        => ansi8('░░', 21),
+	QRMatrix::M_VERSION_DARK   => QRString::ansi8('██', 21),
+	QRMatrix::M_VERSION        => QRString::ansi8('░░', 21),
 	// dark module
-	QRMatrix::M_DARKMODULE     => ansi8('██', 53),
+	QRMatrix::M_DARKMODULE     => QRString::ansi8('██', 53),
 	// data
-	QRMatrix::M_DATA_DARK      => ansi8('██', 166),
-	QRMatrix::M_DATA           => ansi8('░░', 166),
+	QRMatrix::M_DATA_DARK      => QRString::ansi8('██', 166),
+	QRMatrix::M_DATA           => QRString::ansi8('░░', 166),
 ];
 
 
@@ -54,12 +54,3 @@ $out  = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9Wg
 echo "\n\n\n$out\n\n\n";
 
 exit;
-
-
-// a little helper to a create proper ANSI 8-bit color escape sequence
-function ansi8(string $str, int $color, bool $background = false):string{
-	$color      = max(0, min($color, 255));
-	$background = ($background ? 48 : 38);
-
-	return sprintf("\x1b[%s;5;%sm%s\x1b[0m", $background, $color, $str);
-}

+ 18 - 1
src/Output/QRString.php

@@ -13,7 +13,7 @@
 
 namespace chillerlan\QRCode\Output;
 
-use function implode, is_string, json_encode;
+use function implode, is_string, json_encode, max, min, sprintf;
 use const JSON_THROW_ON_ERROR;
 
 /**
@@ -87,4 +87,21 @@ class QRString extends QROutputAbstract{
 		return json_encode($this->matrix->getMatrix(), JSON_THROW_ON_ERROR);
 	}
 
+	//
+
+	/**
+	 * a little helper to create a proper ANSI 8-bit color escape sequence
+	 *
+	 * @see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
+	 * @see https://en.wikipedia.org/wiki/Block_Elements
+	 *
+	 * @codeCoverageIgnore
+	 */
+	public static function ansi8(string $str, int $color, bool $background = null):string{
+		$color      = max(0, min($color, 255));
+		$background = ($background === true) ? 48 : 38;
+
+		return sprintf("\x1b[%s;5;%sm%s\x1b[0m", $background, $color, $str);
+	}
+
 }

+ 20 - 23
tests/Data/QRMatrixTest.php

@@ -50,41 +50,38 @@ final class QRMatrixTest extends TestCase{
 		$opt->outputType  = QROutputInterface::STRING_TEXT;
 		$opt->eol         = "\n";
 		$opt->moduleValues = [
-			// this is not ideal but it seems it's not possible anymore to colorize emoji via ansi codes
-			// 🔴 🟠 🟡 🟢 🔵 🟣 ⚫️ ⚪️ 🟤
-			// 🟥 🟧 🟨 🟩 🟦 🟪 ⬛ ⬜ 🟫
 			// finder
-			QRMatrix::M_FINDER_DARK    => '🟥', // dark (true)
-			QRMatrix::M_FINDER         => '🔴', // light (false)
-			QRMatrix::M_FINDER_DOT     => '🟥', // finder dot, dark (true)
+			QRMatrix::M_FINDER_DARK    => QRString::ansi8('██', 124), // dark (true)
+			QRMatrix::M_FINDER         => QRString::ansi8('░░', 124), // light (false)
+			QRMatrix::M_FINDER_DOT     => QRString::ansi8('██', 124), // finder dot, dark (true)
 			// alignment
-			QRMatrix::M_ALIGNMENT_DARK => '🟧',
-			QRMatrix::M_ALIGNMENT      => '🟠',
+			QRMatrix::M_ALIGNMENT_DARK => QRString::ansi8('██', 2),
+			QRMatrix::M_ALIGNMENT      => QRString::ansi8('░░', 2),
 			// timing
-			QRMatrix::M_TIMING_DARK    => '🟨',
-			QRMatrix::M_TIMING         => '🟡',
+			QRMatrix::M_TIMING_DARK    => QRString::ansi8('██', 184),
+			QRMatrix::M_TIMING         => QRString::ansi8('░░', 184),
 			// format
-			QRMatrix::M_FORMAT_DARK    => '🟪',
-			QRMatrix::M_FORMAT         => '🟣',
+			QRMatrix::M_FORMAT_DARK    => QRString::ansi8('██', 200),
+			QRMatrix::M_FORMAT         => QRString::ansi8('░░', 200),
 			// version
-			QRMatrix::M_VERSION_DARK   => '🟩',
-			QRMatrix::M_VERSION        => '🟢',
+			QRMatrix::M_VERSION_DARK   => QRString::ansi8('██', 21),
+			QRMatrix::M_VERSION        => QRString::ansi8('░░', 21),
 			// data
-			QRMatrix::M_DATA_DARK      => '🟦',
-			QRMatrix::M_DATA           => '🔵',
+			QRMatrix::M_DATA_DARK      => QRString::ansi8('██', 166),
+			QRMatrix::M_DATA           => QRString::ansi8('░░', 166),
 			// dark module
-			QRMatrix::M_DARKMODULE     => '🟫',
+			QRMatrix::M_DARKMODULE     => QRString::ansi8('██', 53),
 			// separator
-			QRMatrix::M_SEPARATOR      => '⚪️',
+			QRMatrix::M_SEPARATOR      => QRString::ansi8('░░', 219),
 			// quiet zone
-			QRMatrix::M_QUIETZONE      => '⬜',
+			QRMatrix::M_QUIETZONE      => QRString::ansi8('░░', 195),
 			// logo space
-			QRMatrix::M_LOGO           => '⬜',
+			QRMatrix::M_LOGO           => QRString::ansi8('░░', 105),
 			// empty
-			QRMatrix::M_NULL           => '🟤',
+			QRMatrix::M_NULL           => QRString::ansi8('░░', 231),
 			// data
-			QRMatrix::M_TEST_DARK      => '⬛',
-			QRMatrix::M_TEST           => '⚫️',
+			QRMatrix::M_TEST_DARK      => QRString::ansi8('██', 127),
+			QRMatrix::M_TEST           => QRString::ansi8('░░', 127),
 		];
 
 		$out = (new QRString($opt, $matrix))->dump();