-
Notifications
You must be signed in to change notification settings - Fork 107
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
Modification of colours rendering + 256 colours support #104
Conversation
Codecov Report
@@ Coverage Diff @@
## master #104 +/- ##
============================================
- Coverage 96.7% 96.32% -0.38%
- Complexity 297 307 +10
============================================
Files 22 23 +1
Lines 911 926 +15
============================================
+ Hits 881 892 +11
- Misses 30 34 +4
Continue to review full report at Codecov.
|
Just skimmed over it and the ideas all seem sounds to me, please continue! pinging @mikeymike here to checkout also, he wrote most of this stuff! |
Ok so I spent some time wondering what would be the best way to fallback to 8 colors scheme from a 256 colors code if the terminal did not support it. Properly (not using RGB) computing the difference between the colors to automatically select the closest one from the default 8 seems to be a bit expensive, so I went with the $fallback argument. It feel that it also gives more control over it. On another note, do we need some new Exception class to throw those "Invalid colour code" exceptions ? Note: Travis does not print the inverted lines correctly, but it should just be at the html log level (but they are correctly inverted). I added an issue to their project, hoping they'll be able to add the functionnality. |
@Lynesth could we just maybe calculate the fallbacks ourself and just store it as code? Like perform the calculations to figure which 8bit colour each of the 256 should map to and store it as a hardcoded array somewhere? that way we don't need to perform expensive computations at runtime |
@AydinHassan Yeah I guess I can do that. |
Closing/Reopening to trigger a Travis rebuild since php-school/terminal#9 was merged. |
There you go: https://gist.github.com/Lynesth/5b8d885a2f3c0bef180c6733813ef156 Where do you want it included ? We would need it both in MenuStyle and in CliMenuBuilder. Let me know. |
@Lynesth this is really cool! Yeah I agree a class with one static method should work. Maybe be dump it in class ColourMap {
public static function map256To8()
{}
} ? I'm open for other suggestions though. We can still fallback manually also, but I guess it will be optional then. |
src/CliMenuBuilder.php
Outdated
public function setBackgroundColour($colour, string $fallback = null) : self | ||
{ | ||
if (is_int($colour)) { | ||
if ($this->terminal->getColourSupport() < 256) { |
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.
Looks like we can extract this validation as it's the same in setForegroundColour
. Lets introduce a validateColour
method.
@mikeymike can you take a look at this at some point since you wrote most of this code? |
Disclaimer: It isn't finished yet
So this adds support for 256 colors if the terminal supports it (it needs php-school/terminal#8 to work).
For now it only works if such colours are set using their number (0 to 255, see image attached) and doesn't yet fallback to 8 colours if not supported by terminal. I also want to add a full table of names->code association so that we can set them using their name.
It also modifies the way colours escaped sequences are generated so that it only happens as little as possible (when colours are set) and not each time.
It also makes use of the inverted escape sequence to set the colours for selected elements and the reset escape sequence to reset all colours/blod/whatever to their default value.
Let me know what you think so that I continue on this, or not. As with my previous PRs, I'll fix the tests if it's accepted.
List of colours:
Ain't it sexy ?