Skip to content

[Console] Add support of HSL functional notation for output colors #43128

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

alexandre-daubois
Copy link
Member

@alexandre-daubois alexandre-daubois commented Sep 21, 2021

Also add a missing test for RGB functional notation

Q A
Branch? 5.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets NA
License MIT
Doc PR symfony/symfony-docs#15839

Wait, another way to set colors in console? You already proposed RGB functional notation a few days ago!

Yes. But wait. That's not just "another way to set colors". Of course RGB notation suits most cases and is some kind of a "must" when it comes to colors. But HSL notation has some undeniable pros over RGB notation. There's a great (french) article speaking about it (https://iamvdo.me/blog/les-avantages-de-hsl-par-rapport-a-rgb). To make it simple, HSL is way more effective when it comes to manipulating color gradients in particular. By adding this feature, we're giving this possibility to developers using Console components to make even more gorgeous CLI applications.
I don't think we need to add (yet, at least) other variations like CMYK or HSV. I rarely came across them, and it seems they’re clearly not as popular as RGB and HSL.

About having to put % in the notation, I think it's a good choice for two reasons.

  • It greatly simplifies the regex and validation of input. Supporting saturation and lightness in an interval of [0, 1] means, if we want to be exhaustive, we need to check these values can be in multiple format: 0.5, .5, maybe 1.. Having the mandatory percent sign clearly tells the developer to use an integer in the interval [0, 100].
  • Also, that's the way to use and write hsl method in CSS.

Also add a missing test for RGB functional notation
@stof
Copy link
Member

stof commented Sep 21, 2021

  • It greatly simplifies the regex and validation of input. Supporting saturation and lightness in an interval of [0, 1] means, if we want to be exhaustive, we need to check these values can be in multiple format: 0.5, .5, maybe 1.. Having the mandatory percent sign clearly tells the developer to use an integer in the interval [0, 100].

  • Also, that's the way to use and write hsl method in CSS.

technically, CSS does not restrict you to integer percentages. .8% or 53.2% are valid values.

@alexandre-daubois
Copy link
Member Author

technically, CSS does not restrict you to integer percentages. .8% or 53.2% are valid values.

That's right! My comment is a bit misleading on this point. Do you mean we should support these values as well? I think it doesn't worth the additional complexity it would bring to code in my opinion.

@fabpot
Copy link
Member

fabpot commented Sep 21, 2021

I was on the verge of closing the other PR, and I maybe should have done that. Anything can be converted to #xxxxxx. So, I'm 👎 on this one. I don't see how it brings value in the context of the Symfony Console common usage.

[$hue, $saturation, $lightness] = \array_slice($matches, 1);

if ($hue > 359) {
throw new InvalidArgumentException(sprintf('Invalid hue; value should be between 0 and 359, got %d.', $hue));
Copy link
Member

Choose a reason for hiding this comment

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

Why this exception ? In CSS, hsl(4236, 50%, 50%) is valid. 4236deg is equivalent to 276deg as hue as far as the color is concerned (but it may make a difference regarding transitions, I'm not sure)

@stof
Copy link
Member

stof commented Sep 21, 2021

I tend to agree with @fabpot here. Given that this PR only deals with hsl to convert it back to hex RGB, which is already supported as input, the Console component does not really need to support HSL strings. You could use a separate PHP package providing such conversion (and maybe even providing a PHP function named hsl() instead of a function parsing a string) and call it in your code coloring the output

@alexandre-daubois
Copy link
Member Author

I was on the verge of closing the other PR, and I maybe should have done that. Anything can be converted to #xxxxxx. So, I'm 👎 on this one. I don't see how it brings value in the context of the Symfony Console common usage.

I can totally understand your point of view. Maybe this one's a bit too far given the console context. The last comment by @stof is also relevant. I'm closing it, thanks for the review anyways!

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.

4 participants