Skip to content

[Console] added ability to add custom colors #19844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

LostKobrakai
Copy link

Q A
Branch? "master"
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

It's currently quite tedious to add additional colors to the console components as it does require a custom implementation of OutputFormatterInterface as well as OutputFormatterStyleInterface just to add some color codes.

* Add additional foreground color option.
*
* @param string $name The color name
* @param string|int $color The color code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The color code -> The ANSI color code ?

$type,
$name,
implode(', ', array_keys(static::${$property}))
));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be on one line

@fabpot
Copy link
Member

fabpot commented Sep 14, 2016

The current code defines all available ANSI colors.

@LostKobrakai
Copy link
Author

There are the non standard high-intensity colors (90-97 / 100-107) and they are not available.

@fabpot
Copy link
Member

fabpot commented Sep 14, 2016

What about adding them instead?

@LostKobrakai
Copy link
Author

I didn't really expect that non-standards would get merged but having the ability to add arbitrary items to the arrays would allow for more flexibility in terms of custom coloring as well (custom unset value, custom names).

@nicolas-grekas nicolas-grekas added this to the 3.x milestone Dec 6, 2016
@TerjeBr
Copy link

TerjeBr commented Mar 17, 2017

Here is reference to all possible escape codes https://en.wikipedia.org/wiki/ANSI_escape_code#graphics

May be we should open up to use ESC[38;5;...;m and ESC[48;5;...;m too? That will make it possible to choose from 256 colors.

@nicolas-grekas
Copy link
Member

Closing as there no activity. Feel free to reopen.

fabpot added a commit that referenced this pull request Jun 10, 2020
This PR was merged into the 5.2-dev branch.

Discussion
----------

[Console] Add support for true colors

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #26576, Fix #19844 <!-- prefix each issue number with "Fix #", if any -->
| License       | MIT
| Doc PR        | -

This PR adds support for true colors in the Console component.

Instead of adding many ways to add more colors than the current "default" ones, I've opted to only add true color support via Hex CSS colors. If you have something else (RGB, HSV, ...), you need to first convert it to a CSS color. I've also decided to not support the ANSI 256 colors as most terminals support true colors nowadays.

If true colors are not supported by the terminal, we fall back to the "nearest" default color.

`<fg=green;bg=blue>` is now equivalent to `<fg=#00ff00;bg=#00f>`.

The `Color` class is usable outside of the Console framework as well:

```php
$color = new Color('black', 'white');
echo $color->apply("foo");
echo "\n";

$color = new Color('red', 'yellow');
echo $color->apply("foo");
echo "\n";

$color = new Color('#000000', '#ffffff');
echo $color->apply("foo");
$color = new Color('#000', '#fff', ['underscore', 'reverse']);
echo $color->apply("bar");
echo "\n";
```

Rainbow time!

```php
function rainbowColor($i) {
    $h = (int) ($i / 43);
    $f = (int) ($i - 43 * $h);
    $t = (int) ($f * 255 / 43);
    $q = 255 - $t;

    if ($h == 0) {
        return new Color('', sprintf('#FF%02x00', $t));
    } elseif ($h == 1) {
        return new Color('', sprintf('#%02xFF00', $q));
    } elseif ($h == 2) {
        return new Color('', sprintf('#00FF%02x', $t));
    } elseif ($h == 3) {
        return new Color('', sprintf('#00%02xFF', $q));
    } elseif ($h == 4) {
        return new Color('', sprintf('#%02x00FF', $t));
    } elseif ($h == 5) {
        return new Color('', sprintf('#FF00%02x', $q));
    }
}

for ($i = 0; $i < 128; $i++) {
    echo rainbowColor($i)->apply(' ');
}
echo "\n";

for ($i = 255; $i >= 128; $i--) {
    echo rainbowColor($i)->apply(' ');
}
echo "\n";
```

![image](https://user-images.githubusercontent.com/47313/81796170-59af5e00-950d-11ea-8203-18c13ae8a07e.png)

Commits
-------

d066514 [Console] Add support for true colors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants