Skip to content

[Console] Add support for true colors #36802

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

Merged
merged 1 commit into from
Jun 10, 2020
Merged

Conversation

fabpot
Copy link
Member

@fabpot fabpot commented May 13, 2020

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #26576, Fix #19844
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:

$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!

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

@fabpot fabpot force-pushed the console-true-color branch 2 times, most recently from 8ff5fb5 to c8b9489 Compare May 13, 2020 10:27
@nicolas-grekas nicolas-grekas added this to the next milestone May 13, 2020
}

if (!isset(static::$colors[$color])) {
throw new InvalidArgumentException(sprintf('Invalid "%s" color; expected one of (%s).', $color, implode(', ', array_keys(static::$colors))));
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we can point out here that the user should use one of the predefined colors or an hexadecimal one ?

@stof
Copy link
Member

stof commented May 14, 2020

If you have something else (RGB, HSV, ...), you need to first convert it to a CSS color.

I guess you need to reword your description, to talk about hex colors. CSS colors do support RGB and HSL (and more to come)

@fabpot
Copy link
Member Author

fabpot commented May 16, 2020

Anyone willing to test on Windows?

*/
final class Color
{
private static $colors = [
Copy link
Contributor

Choose a reason for hiding this comment

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

shoudn't these be consts instead ? (Same for $availableOptions)

@wouterj
Copy link
Member

wouterj commented May 16, 2020

Some tests with the Color class + the rainbow script on some windows command lines (I couldn't test the whole repository, as I haven't set-up my windows PC for development at all). As expected, no true color support, but seems like default colors work as expected.

Windows Powershell
powershell

Git Bash GUI
git bash

Command prompt
cmd

WSL
wsl

@fabpot fabpot force-pushed the console-true-color branch from fa9f895 to 6fe798b Compare June 10, 2020 02:03
@fabpot
Copy link
Member Author

fabpot commented Jun 10, 2020

Thanks @wouterj for testing on Windows.

@fabpot fabpot force-pushed the console-true-color branch from 6fe798b to 1788423 Compare June 10, 2020 02:37
@fabpot fabpot force-pushed the console-true-color branch from 1788423 to d066514 Compare June 10, 2020 02:55
@fabpot fabpot merged commit ea956b1 into symfony:master Jun 10, 2020
@fabpot fabpot deleted the console-true-color branch June 10, 2020 03:08
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.2 Oct 5, 2020
@fabpot fabpot mentioned this pull request Oct 5, 2020
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.

8 participants