-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
516a4dc
to
2839e32
Compare
* Add additional foreground color option. | ||
* | ||
* @param string $name The color name | ||
* @param string|int $color The color code |
There was a problem hiding this comment.
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})) | ||
)); |
There was a problem hiding this comment.
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
The current code defines all available ANSI colors. |
There are the non standard high-intensity colors (90-97 / 100-107) and they are not available. |
What about adding them instead? |
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). |
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. |
Closing as there no activity. Feel free to reopen. |
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"; ```  Commits ------- d066514 [Console] Add support for true colors
It's currently quite tedious to add additional colors to the console components as it does require a custom implementation of
OutputFormatterInterface
as well asOutputFormatterStyleInterface
just to add some color codes.