PHPackages                             bebat/console-color - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. bebat/console-color

ActiveLibrary[CLI &amp; Console](/categories/cli)

bebat/console-color
===================

Apply colors &amp; styles to text for command line output

1.0.2(2y ago)41.4k[1 issues](https://github.com/bbatsche/ConsoleColor/issues)[2 PRs](https://github.com/bbatsche/ConsoleColor/pulls)1MITPHPPHP ^8.1CI passing

Since Nov 11Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/bbatsche/ConsoleColor)[ Packagist](https://packagist.org/packages/bebat/console-color)[ RSS](/packages/bebat-console-color/feed)WikiDiscussions develop Synced 3w ago

READMEChangelog (3)Dependencies (15)Versions (8)Used By (1)

Console Color
=============

[](#console-color)

[![Latest Stable Version](https://camo.githubusercontent.com/2539a1c7f78c5269cb9410979092a05c3adec08dc1af07d5e6580ce0e5a39f58/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62656261742f636f6e736f6c652d636f6c6f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bebat/console-color)[![Required PHP Version](https://camo.githubusercontent.com/330eaf7f56934c375954cb7b024df3b3e3f308a8a5d4dc92a8ad183a8ae95066/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f62656261742f636f6e736f6c652d636f6c6f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bebat/console-color)[![License](https://camo.githubusercontent.com/a5884ebc636807e05122387a33120b70319c7f380418f5057c3108437af9de29/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f62656261742f636f6e736f6c652d636f6c6f723f7374796c653d666c61742d737175617265)](LICENSE)[![Acceptance Test Status](https://camo.githubusercontent.com/7b07d2587f20b2ce3d72ade055c885debbf869eb7d8e9c464c9c3b803c941e95/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f62626174736368652f436f6e736f6c65436f6c6f722f616363657074616e63652e796d6c3f6272616e63683d646576656c6f70267374796c653d666c61742d737175617265)](https://github.com/bbatsche/ConsoleColor/actions/workflows/acceptance.yml)[![Code Coverage](https://camo.githubusercontent.com/7cd48ca2468fbcc5abae1e9b37580bb127db67894db766c4d2ac80fad6ee986b/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f62626174736368652f436f6e736f6c65436f6c6f723f7374796c653d666c61742d737175617265)](https://codecov.io/gh/bbatsche/ConsoleColor)

Console Color is a lightweight PHP 8.1+ library for adding color &amp; other styles to text in the command line.

- [Installation](#installation)
- [Basic Usage](#basic-usage)
    - [Environment Variables](#environment-variables)
- [Included Styles](#included-styles)
    - [Basic Styles](#basic-styles)
        - [Text](#text)
        - [Underline](#underline)
        - [Foreground &amp; Background Color](#foreground--background-color)
    - [256 &amp; True Color](#256--true-color)
    - [Composite Styles](#composite-styles)

Installation
------------

[](#installation)

The Console Color library can be installed via [Composer](https://getcomposer.org/):

```
composer require bebat/console-color
```

Basic Usage
-----------

[](#basic-usage)

To apply style to a text you need an instance of `BeBat\ConsoleColor\Style`. With that you can *apply* a style object to some text:

```
use BeBat\ConsoleColor\Style;

$style = new Style();

echo $style->apply('This text is green', Style\Color::Green) . PHP_EOL;
```

The `Style` class will try to determine if console output can be styled automatically (more details on this can be found in the Environment Variables section [below](#environment-variables)). By default, it looks at `STDOUT` and checks if it is a TTY. If you are using a different output type (such as a `php://` I/O stream) you may pass that to `Style()` to have it determine if that stream supports styling:

```
use BeBat\ConsoleColor\Style;

$handle = fopen($something, 'w');
$style = new Style($handle);

fwrite($handle, $style->apply(
    'If $something points to a console this text will have a cyan background. ' .
    'Otherwise it will just be plain.',
    Style\BackgroundColor::Cyan,
));
```

You may use the `force()` method to make `Style` *always* apply styles to text, regardless of whether it thinks the output resource supports it:

```
use BeBat\ConsoleColor\Style;

$handle = fopen($something, 'w');
$style = new Style($handle);
$style->force();

fwrite($handle, $style->apply(
    'This text will always have styling applied to it.', Style\Text::Bold,
));
```

### Environment Variables

[](#environment-variables)

In addition to checking if `STDOUT` is a TTY, `Style` will look at several environment variables to determine if styles should be applied, and what level of support the user's terminal has for styles.

- `FORCE_COLOR` - If the user has set `FORCE_COLOR` styling will be applied, even if `Style` doesn't think the output is a TTY.
- `NO_COLOR` - If the user has set `NO_COLOR` styling will be disabled. `NO_COLOR` takes precedence over `FORCE_COLOR`.
- `TERM` - `Style` will check `TERM` to see if it supports 256 colors.
- `COLORTERM` - If `COLORTERM` is set to `truecolor` then `Style` will apply RGB based colors.

Included Styles
---------------

[](#included-styles)

`Style::apply()` accepts an instance of [`StyleInterface`](src/StyleInterface.php), and Console Color includes a number of styles that implement this interface.

To see what styles your terminal supports and what they look like, run the included `sample.php` file.

### Basic Styles

[](#basic-styles)

`Style\Text`, `Style\Underline`, `Style\Color`, and `Style\BackgroundColor` are [backed enumerations](https://www.php.net/manual/en/language.enumerations.backed.php) which helps to make Console Color's API extremely clean. These styles have the broadest support in most terminals (with some exceptions).

#### Text

[](#text)

- `Style\Text::None` - no style
- `Style\Text::Bold` - bold text (may also "brighten" the color in some terminals)
- `Style\Text::Faint` - dim text color
- `Style\Text::Italic` - italics
- `Style\Text::Underline` - underline
- `Style\Text::Blink` - blinking text (use wisely 😉)
- `Style\Text::Reverse` - swap foreground and background colors
- `Style\Text::Concealed` - hides the text (it is still present and can be selected)
- `Style\Text::Strike` - strikethrough, not supported in all terminals
- `Style\Text::DoubleUnderline` - double or bold underline, not supported in all terminals
- `Style\Text::Overline` - line over text, not supported in all terminals
- `Style\Text::Superscript` - superscript text, rarely supported
- `Style\Text::Subscript` - subscript text, rarely supported

#### Underline

[](#underline)

Custom underline styles are not widely supported but some terminals do include them. Most, if not all, will fall back on just displaying a single underline.

- `Style\Underline::Single`
- `Style\Underline::Double`
- `Style\Underline::Wavy`
- `Style\Underline::Dotted`
- `Style\Underline::Dashed`

#### Foreground &amp; Background Color

[](#foreground--background-color)

`Style\Color` and `Style\BackgroundColor` both support the same values and have an identical API.

- `Style\Color::Black`
- `Style\Color::Red`
- `Style\Color::Green`
- `Style\Color::Yellow`
- `Style\Color::Blue`
- `Style\Color::Magenta`
- `Style\Color::Cyan`
- `Style\Color::White`
- `Style\Color::Default`
- `Style\Color::BrightBlack`
- `Style\Color::BrightRed`
- `Style\Color::BrightGreen`
- `Style\Color::BrightYellow`
- `Style\Color::BrightBlue`
- `Style\Color::BrightMagenta`
- `Style\Color::BrightCyan`
- `Style\Color::BrightWhite`

### 256 &amp; True Color

[](#256--true-color)

Most terminal programs support 256 color output, and many now also support 24-bit "true" colors. Console Color can apply those colors to the foreground, background, or underline color through the `Style\Color256` and `Style\ColorRGB` classes.

- `Style\Color256::foreground(int $code)` - apply a 256 color to the text
- `Style\Color256::background(int $code)` - apply a 256 color to the background
- `Style\Color256::underline(int $code)` - apply a 256 color to an underline (see composite styles [below](#composite-styles))
- `Style\ColorRGB::foreground(int $red, int $green, int $blue)` - apply an RGB color to the text
- `Style\ColorRGB::background(int $red, int $green, int $blue)` - apply an RGB color to the background
- `Style\ColorRGB::underline(int $red, int $green, int $blue)` - apply an RGB color to an underline (see composite styles [below](#composite-styles))

### Composite Styles

[](#composite-styles)

It's possible to apply more than one style to your text by using the `Style\Composite()` class. For example, to apply color to both the foreground and background:

```
use BeBat\ConsoleColor\Style;

$style = new Style();

echo $style->apply(
    'This text is white on red',
    new Style\Composite(Style\Color::BrightWhite, Style\BackgroundColor::Red),
) . PHP_EOL;
```

`Style\Composite()` is also required if you want to add color to an underline:

```
use BeBat\ConsoleColor\Style;

$style = new Style();

echo $style->apply(
    'This text has a typo',
    new Style\Composite(Style\Underline::Wavy, Style\ColorRGB::underline(255, 0, 0)),
) . PHP_EOL;
```

`Style\Composite()` can actually take as many styles as you need to apply:

```
use BeBat\ConsoleColor\Style;

$style = new Style();

echo $style->apply(
    "Please don't do this\n",
    new Style\Composite(
        Style\Text::Bold,
        Style\Text::Italic,
        Style\Text::Blink,
        Style\Color256::background(34),
        Style\Color256::foreground(227),
    ),
);
```

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance42

Moderate activity, may be stable

Popularity21

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~122 days

Total

3

Last Release

1077d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6918f1e527496229c5936c704543c4b4835171c5e8a31adae488b792fb38f633?d=identicon)[bbatsche](/maintainers/bbatsche)

---

Top Contributors

[![bbatsche](https://avatars.githubusercontent.com/u/500909?v=4)](https://github.com/bbatsche "bbatsche (40 commits)")

---

Tags

ansiclicolorcolortermcommand-lineconsolephpphp8truecolorttyvt100cliconsolecommand-linestylecoloransivt100truecolorttycolorterm

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/bebat-console-color/health.svg)

```
[![Health](https://phpackages.com/badges/bebat-console-color/health.svg)](https://phpackages.com/packages/bebat-console-color)
```

###  Alternatives

[symfony/console

Eases the creation of beautiful and testable command line interfaces

9.8k1.1B13.2k](/packages/symfony-console)[nunomaduro/collision

Cli error handling for console/command-line PHP applications.

4.6k348.7M10.4k](/packages/nunomaduro-collision)[nunomaduro/termwind

It's like Tailwind CSS, but for the console.

2.5k260.6M359](/packages/nunomaduro-termwind)[kevinlebrun/colors.php

Colors for PHP CLI scripts

3426.9M45](/packages/kevinlebrun-colorsphp)[nunomaduro/laravel-console-menu

Laravel Console Menu is an output method for your Laravel/Laravel Zero commands.

815424.6k52](/packages/nunomaduro-laravel-console-menu)[nunomaduro/laravel-console-task

Laravel Console Task is a output method for your Laravel/Laravel Zero commands.

2592.3M13](/packages/nunomaduro-laravel-console-task)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
