PHPでBashターミナルへテキストをカラー出力するツール Bash Colorizer の使い方についての解説です。
検証済のターミナル
- VSCode Terminal (VSCode 1.99.1 / Windows 11)
- Windows Terminal 1.22.10731.0
前提条件
- PHP8.1以降インストール済
- Composer v2インストール済
- Bash v5 インストール済
- Ubuntu24.04.1 (WSL2)上で作業しています
インストール
プロジェクトフォルダ内でComposerコマンドを実行します。
composer require macocci7/bash-colorizer

基本的な使い方:テキスト出力
「src/colorizer.php」なるファイルを前提にコードを書いていきます。
▼まずはComposerの「autoload.php」をインポートしてから、use宣言します。
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Macocci7\BashColorizer\Colorizer;
▼普通にテキスト出力してみます。(色指定なし)
※静的呼出の例
Colorizer::echo("Hi, there!");
Colorizer::echo(" How's it going with you?", PHP_EOL);
※メソッドチェーンの例
Colorizer::echo("Hi, there!");
->echo(" How's it going with you?", PHP_EOL);
※インスタンス生成の例
$colorizer = new Colorizer;
$colorizer->echo("Hi, there!")
->echo(" How's it going with you?", PHP_EOL);
全て実行結果は同じで、普通の echo と同じです。

Colorizer の echo() メソッドは次のようになっており、
public static function echo(string $string, string $eol = ''): self
{
echo static::encode($string, $eol);
return new self(static::$config);
}
第二引数はEOLで、デフォルト空文字(改行しない)です。
出力に付け足しているだけなので、<br />などの指定もできますし、
\n\n\n\n とかの指定もできます。
基本的な使い方:出力設定
出力設定には3つの項目があります。
1.[ attributes ] ・・・属性(太字、イタリック、取消線、下線等)
2.[ foreground ] ・・・テキスト色
3.[ background ] ・・・背景色
配列に収めて丸ごと突っ込むと楽でしょう。
$config = [
'attributes' => ['italic', 'bold'],
'foreground' => 'black',
'background' => 'green',
];
※静的呼出の例
Colorizer::config($config);
Colorizer::echo("Hi, there!", PHP_EOL);
※メソッドチェーンの例
Colorizer::config($config)
->echo("Hi, there!", PHP_EOL);
※インスタンス生成の例
$colorizer = new Colorizer; // 生成例1(configなし)
$colorizer = new Colorizer($config); // 生成例2(configあり)
$colorizer = Colorizer::config($config); // 生成例3(configあり)
$colorizer->config($config) // configは何度でも上書きできる
->echo("Hi, there!", PHP_EOL);
どれも実行結果は同じです。

基本的な使い方:属性の設定
attributes() メソッドで属性の設定が可能です。
引数の配列内に複数指定可能です。
Colorizer::attributes(['underline', 'strike'])
->echo("Hi, there!", PHP_EOL);

属性の詳細については「設定できる属性」をご覧ください。
基本的な使い方:テキスト色の設定
foreground() メソッドでテキストの色指定が可能です。
引数は1つだけですが、受け入れ可能な型が3つあります。
▼1.文字列型 (string):
- 色の名称(black, red, green等 8色)
- 16進数カラーコード(例:#ffcc00, または #fc0)
Colorizer::foreground('green')
->echo("Hi, there!", PHP_EOL);

▼2.整数型 (int): 色番号(0 ~ 255) 256色
Colorizer::foreground(3)
->echo("Hi, there!", PHP_EOL);

▼3.整数配列型 (int[]): RGBの色番号(0 ~ 255) 24bit色
Colorizer::foreground([0, 0, 255])
->echo("Hi, there!", PHP_EOL);

色の詳細については「設定できる色」をご覧ください。
基本的な使い方:背景色の設定
background() メソッドで、テキストの背景色の設定が可能です。
こちらも同様に、引数は1つですが、受入可能な型が3つあります。
▼1.文字列型 (string):
- 色の名称(black, red, green等 8色)
- 16進数カラーコード(例: #ffcc00, または #fc0 )
Colorizer::background("red")
->echo("Hi, there!", PHP_EOL);

▼2.整数型 (int): 色番号(0 ~ 255) 256色
Colorizer::background(4)
->echo("Hi, there!", PHP_EOL);

▼3.整数配列型 (int[]): RGBの色番号(0 ~ 255) 24bit色
Colorizer::background([255, 182, 0])
->echo("Hi, there!", PHP_EOL);

色の詳細について「設定できる色」をご覧ください。
基本的な使い方:属性・テキスト色・背景色の設定
それぞれをメソッドチェーンで繋げることで、config() メソッドと同等のことができます。
Colorizer::attributes(['double-underline', 'italic'])
->foreground("yellow")
->background("blue")
->echo("Hi, there!", PHP_EOL);

基本的な使い方:下線の色指定
underline() メソッドでテキストの色指定が可能です。
※VSCode Terminalでは有効。Windows Terminalでは無効。
引数は1つだけですが、受け入れ可能な型が3つあります。
▼1.文字列型 (string):
- 16進数カラーコード(例:#ffcc00, または #fc0)
Colorizer::underline('#ffcc00') // または #fc0
->echo("Hi, there!", PHP_EOL);
▼2.整数型 (int): 色番号(0 ~ 255) 256色
Colorizer::underline(4)
->echo("Hi, there!", PHP_EOL);
▼3.整数配列型 (int[]): RGBの色番号(0 ~ 255) 24bit色
Colorizer::underline([255, 182, 0])
->echo("Hi, there!", PHP_EOL);
色の詳細について「設定できる色」をご覧ください。
基本的な使い方:カラー化した文字列の取得
encode() メソッドでカラー化した文字列の取得が可能です。
▼ echo の引数として渡す例
echo "[" . Colorizer::attributes(['double-underline', 'italic', 'bold'])
->foreground("blue")
->background("yellow")
->encode("Hi, there!") . "]" . PHP_EOL;

▼ sprintf() の引数として渡して echo で出力する例
echo sprintf(
"%s: %s%s",
Colorizer::attributes(['bold'])
->foreground("blue")
->background("yellow")
->encode(" Donaldo "),
"Hi, there!",
PHP_EOL
);

▼カラー化した文字列のエスケープシーケンスを可視化して出力する例
※エスケープシーケンスを文字列置換
echo Colorizer::attributes(["bold"])
->background([255, 255, 0])
->foreground([0, 128, 255])
->readable('Hi, There!', PHP_EOL);

この文字列に対して、bashの echo コマンドを -e オプション付で実行すると、色付きで表示されます。
echo -e '\033[1;38;2;0;128;255;48;2;255;255;0mHi, there!\033[m'

設定できる属性
設定できる属性は次の通りです。
属性名 | 説明 | VSCode Terminal | Windows Terminal |
---|---|---|---|
reset | リセット | 〇 | 〇 |
bold | 太字 | 〇 | × |
faint | 低輝度 | 〇 | 〇 |
italic | イタリック | 〇 | 〇 |
underline | 下線 | 〇 | 〇 |
blink | 点滅 | × | ▲ ※1 |
fast-blink | 高速点滅 | × | ▲ ※1、2 |
reverse | 反転 | 〇 | 〇 |
conceal | 非表示 | 〇 | 〇 |
strike | 取消線 | 〇 | 〇 |
gothic | ゴシック | × | × |
double-underline | 二重下線 | 〇 | 〇 |
normal | 太字解除 低輝度解除 | 〇 | 〇 |
no-italic | イタリック解除 | 〇 | 〇 |
no-underline | 下線解除 | 〇 | 〇 |
no-blink | 点滅解除 | ー ※3 | 〇 ※4 |
proportional-spacing | 等幅スペース | ー | ー |
no-reverse | 反転解除 | 〇 | 〇 |
no-conceal | 非表示解除 | 〇 | 〇 |
no-strike | 取消線解除 | 〇 | 〇 |
no-proportional-spacing | 等幅スペース解除 | ー | ー |
framed | 枠付 | × | × |
encircled | 丸付 | × | × |
overlined | 上線 | 〇 | 〇 |
no-framed-no-encircled | 枠解除、丸解除 | ー ※3 | ー ※3 |
no-overlined | 上線解除 | 〇 | 〇 |
underline-color | 下線色指定 | 〇 | ▲ ※5 |
※1:低輝度だと無効。
※2:高速ではない。blink と同じテンポ。
※3:対になる属性が無効のため不明。
※4:fast-blink にも有効
※5:一部有効
▼出力例(VSCode Terminal)

※「vendor/macocci7/bash-colorizer/playground」をプロジェクトフォルダにコピーして実行。
設定できる色
テキスト色、背景色ともに共通です。
▼名称で設定できる色:
名称 | 説明 |
---|---|
black | 黒 |
red | 赤 |
green | 緑 |
yellow | 黄 |
blue | 青 |
magenta | マジェンタ |
cyan | シアン |
white | 白 |
extended | 拡張色 (名称のみ) (効果なし) |
default | 標準 |
VSCode Terminal での例
▼ 256色での設定 ( 0 ~ 255 )
・テキスト色の例(VSCode Terminal)
playground/foreground_256colors.php

・背景色の例(VSCode Terminal)
playground/background_256colors.php

・下線色の例(VSCode Terminal)
playground/underline_256colors.php

▼ 24bit (16777216) 色 (RGBそれぞれ 0 ~ 255)
・テキスト色の例(VSCode Terminal)
playground/foreground_24bitcolors.php

・背景色の例(VSCode Terminal)
playground/background_24bitcolors.php

・下線色の例(VSCode Terminal)
playground/underline_24bitcolors.php

おまけ
の実行結果

の実行結果

以上です。
コメント