Skip to content
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

Support querying the colors via OSC #3718

Closed
egmontkob opened this issue Nov 26, 2019 · 20 comments · Fixed by #17729
Closed

Support querying the colors via OSC #3718

egmontkob opened this issue Nov 26, 2019 · 20 comments · Fixed by #17729
Labels
Area-VT Virtual Terminal sequence support Help Wanted We encourage anyone to jump in on these. In-PR This issue has a related PR Issue-Task It's a feature request, but it doesn't really need a major design. Needs-Tag-Fix Doesn't match tag requirements Product-Conhost For issues in the Console codebase
Milestone

Comments

@egmontkob
Copy link

OSC 4, 10, 11 and friends, with a ? instead of a color, respond with the current value of that color in xterm, vte and quite a few other terminals, always using the rgb:RRRR/GGGG/BBBB notation, and the same terminator (ST vs. BEL) as in the query.

Examples:

echo -ne '\e]4;1;?\e\\'; cat

echo -ne '\e]11;?\a'; cat

This allows apps (e.g. vim) to automatically pick a color scheme depending on whether the user has a dark-on-bright on bright-on-dark color scheme. I don't know if it's actually being used in practice, though :D

@egmontkob egmontkob added the Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. label Nov 26, 2019
@ghost ghost added Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels Nov 26, 2019
@zadjii-msft zadjii-msft added Product-Conhost For issues in the Console codebase Area-VT Virtual Terminal sequence support Issue-Task It's a feature request, but it doesn't really need a major design. labels Nov 26, 2019
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label Nov 26, 2019
@zadjii-msft zadjii-msft removed the Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. label Nov 26, 2019
@zadjii-msft zadjii-msft added this to the 21H1 milestone Nov 26, 2019
@zadjii-msft zadjii-msft added the Help Wanted We encourage anyone to jump in on these. label Nov 26, 2019
@DHowett-MSFT DHowett-MSFT removed the Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting label Nov 30, 2019
@j4james
Copy link
Collaborator

j4james commented Mar 24, 2020

I should probably raise a separate issue for this, but I just wanted to note that there are standard DEC sequences for querying the color table (and setting it for that matter) which we may want to support as well.

@skyline75489
Copy link
Collaborator

Note that VTE responds with rgb:RRRR/GGGG/BBBB format. But iTerm2 responds with rgb:RR/GG/BB format. I don't have a daily Linux machine so I don't know what xterm is using.

@DHowett
Copy link
Member

DHowett commented Sep 10, 2020

I just tested urxvt and it responded with rgba:0000/0000/0000/f332 :)

@skyline75489
Copy link
Collaborator

Well that's awkward. So I have to assume that XTerm has support for rgba: (and at the same time,rgbi, according to @egmontkob) ?

Personally I'd prefer rgb:RR/GG/BB since it's close to #RRGGBB which is everywhere on the web.

@TBBle
Copy link

TBBle commented Sep 10, 2020

From the XParseColor docs (via the Xterm docs for OSC 4), the RR vs RRR vs RRRR is about the bit-depth of the value. So rgb:RR/GG/BB is 24-bit colour (8/8/8/0), and anything longer is really Wide Colour Gamut, and should be scaled down. (Hence things like #302351 being rgba:3000/2300/5100/ee00 in urxvt tips-and-tricks, assuming a linear tone-mapping function.

That said, I guess we don't have native WCG support in Terminal right now, and I expect we'd return the 24-bit colours we are working with, not the tone-mapped resulting colours if we happen to be running on a WCG-based display.

So I also support rgb:RR/GG/BB as long as we're working from a 24-bit palette.

I'm guessing rgba: is a common extension for alpha... It's not in the XParseColor docs, and I remember back in the day, the best reason to use rxvt over xterm was transparent backgrounds... Perhaps rgba: is (or was) an rxvt-specific extension?

Looks like it was rxvt-specific, and unlike rgb:, rgba: is specified to always have 16 bits per channel. Someone was very forward-thinking in the 90's... Or lazy when writing a string parser.

@j4james
Copy link
Collaborator

j4james commented Sep 10, 2020

I've tested the color table queries on 9 different terminal emulators - every one of them (including XTerm , Gnome Terminal, Konsole, and Rxvt) reported the colors as rgb:rrrr/gggg/bbbb (typically scaled up from the internal 24-bit representation). I see in the Rxvt source that there is a build option that will make it return rgba, but that didn't seem to be active on my test system - perhaps it's OS dependent? In any event, I'd think the rgb:rrrr/gggg/bbbb format is almost certainly the right choice if we want to be compatible with what most people are doing.

Regarding the other color formats, XTerm does support rgbi and the various cie variants, but the report format is always rgb.

@skyline75489
Copy link
Collaborator

Out of sheer curiosity, what is the reason that makes iTerm2 the weirdest kid in town?

CC @gnachman

@j4james
Copy link
Collaborator

j4james commented Sep 10, 2020

I believe iTerm2 has an option to choose between reporting as rgb:rrrr/gggg/bbbb and rgb:rr/gg/bb. They got it wrong initially, and then were forced to add the option to support the correct format because it was breaking in vim.

@gnachman
Copy link

I used 8 bit codes originally for reasons I no longer recall. I was made aware of the issue in this post: https://groups.google.com/g/iterm2-discuss/c/xXzeBY2aRjo/m/zHBZreQWCQAJ

Commit 5c5785f3632b8e90dd69f458411a8b8b17aa0599 changed the default to 16 bits. It is now enabled in 3.4.0 betas, which will exit beta when Big Sur is released.

@TBBle
Copy link

TBBle commented Sep 12, 2020

To be clear, per the spec, both rgb:rr/gg/bb and rgb:rrrr/gggg/bbbb are valid. So is rgb:rrr/ggg/bbb, and a few other options. The item2-discuss link from @gnachman shows that it was a bug in vim before 8.1, that it incorrectly implemented the XParseColor-compatible colour parser.

That said, the commonality of vim, particularly older vim, means it's probably worth being bug-compatible here from the start, even though it's being overly-precise.

My reading of the Xterm docs for OSC 4 is that if you call OSC 4 XXXX, then OSC 4 ? should be returning precisely that XXXX, but I can see how it can be read to allow returning some numerically-equivalent values, i.e. 48-bit RGB, even if the XXX was originally CIELab:....

@j4james
Copy link
Collaborator

j4james commented Sep 12, 2020

My reading of the Xterm docs for OSC 4 is that if you call OSC 4 XXXX, then OSC 4 ? should be returning precisely that XXXX

Yeah, it certainly does read like that. But in practice the spec is defined by the reference implementation, which in this case is XTerm. If you want to be "spec compliant" you need to match whatever XTerm does. And when you think about it, it makes sense for your terminal to be liberal in what it accepts (i.e. supporting as many formats as possible), but conservative in what it produces as a response (you can't reasonably expect every application that queries the palette to understand all the different formats).

@TBBle
Copy link

TBBle commented Sep 13, 2020

That's fair. And given that (for an older version, I didn't check anything newer) xterm OSC 4 n ? produces XQueryColor formatted as rgb:%04x/%04x/%04x, that seems a safe thing to do.

you can't reasonably expect every application that queries the palette to understand all the different formats

I agree in general. In this case however, because it's not "all the different formats", it's a format defined by a specific API (XParseColor), I would absolutely expect of myself that a parser was prepared to handle those documented formats. Apps on X11 of course have it easy, because they can just use XParseColor, but I'm unaware off-hand of anyone knocking together an X11-independent implementation of the API, which is a shame, but I guess the OSC colour codes are the only, highly-specific use-case for such a thing.

It may not be kind to specify an API behaviour in terms of the API it's implemented with underneath, but that is what we have here. (Edit: If anyone's looking for the implementation side of XParseColor, the colour space parsing is farmed out to functions pointed to by the struct XcmsColorSpace instances, and libXRender separately defines rgba: in XRenderParseColor).

That said, Microsoft Terminal cannot consume rgb:rrrr/gggg/bbbb for OSC 4 right now, so we probably need to fix that, or we end up in the situation where we cannot roundtrip our OSC 4 n ? back to OSC 4 x <string we saved earlier>.

Our local implementation of XParseColor could do with some love too.

@skyline75489
Copy link
Collaborator

I’m working on it. See #7578 if you have time 😄

@skyline75489
Copy link
Collaborator

@mboruta1 I was working on this area previously. The PRs related are #7578 and #8999. It might help you with the “poking around” of the codebase 😃

@mboruta1
Copy link

mboruta1 commented Apr 9, 2021

Thank you @skyline75489 :)

ghost pushed a commit that referenced this issue Nov 23, 2021
This PR merges the default colors and cursor color into the main color
table, enabling us to simplify the `ConGetSet` and `ITerminalApi`
interfaces, with just two methods required for getting and setting any
form of color palette entry.

The is a follow-up to the color table standardization in #11602, and a
another small step towards de-duplicating `AdaptDispatch` and
`TerminalDispatch` for issue #3849. It should also make it easier to
support color queries (#3718) and a configurable bold color (#5682) in
the future.

On the conhost side, default colors could originally be either indexed
positions in the 16-color table, or separate standalone RGB values. With
the new system, the default colors will always be in the color table, so
we just need to track their index positions.

To make this work, those positions need to be calculated at startup
based on the loaded registry/shortcut settings, and updated when
settings are changed (this is handled in
`CalculateDefaultColorIndices`). But the plus side is that it's now much
easier to lookup the default color values for rendering.

For now the default colors in Windows Terminal use hardcoded positions,
because it doesn't need indexed default colors like conhost. But in the
future I'd like to extend the index handling to both terminals, so we
can eventually support the VT525 indexed color operations.

As for the cursor color, that was previously stored in the `Cursor`
class, which meant that it needed to be copied around in various places
where cursors were being instantiated. Now that it's managed separately
in the color table, a lot of that code is no longer required.

## Validation
Some of the unit test initialization code needed to be updated to setup
the color table and default index values as required for the new system.
There were also some adjustments needed to account for API changes, in
particular for methods that now take index values for the default colors
in place of COLORREFs. But for the most part, the essential behavior of
the tests remains unchanged.

I've also run a variety of manual tests looking at the legacy console
APIs as well as the various VT color sequences, and checking that
everything works as expected when color schemes are changed, both in
Windows Terminal and conhost, and in the latter case with both indexed
colors and RGB values.

Closes #11768
@zadjii-msft zadjii-msft modified the milestones: Windows vNext, Backlog Jan 4, 2022
@ClaireCJS
Copy link

I, too, would love to see this.

I'm cycling colors with a python script, and can't start the color cycle at the existing color without knowing what it is.

Re-creating an old dos program called glow.exe, basically.

dhwthompson added a commit to dhwthompson/dotfiles that referenced this issue Aug 1, 2023
Specific use-case here: Windows Terminal doesn't yet appear to support
querying the background color with ANSI control codes, so vim can't
auto-detect whether the background is light or dark and defaults to
light mode.

microsoft/terminal#3718
@microsoft-github-policy-service microsoft-github-policy-service bot added the In-PR This issue has a related PR label Aug 17, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Tag-Fix Doesn't match tag requirements label Aug 21, 2024
@rollingmoai
Copy link

rollingmoai commented Aug 24, 2024

OSC 4, 10, 11 and friends, with a ? instead of a color, respond with the current value of that color in xterm, vte and quite a few other terminals, always using the rgb:RRRR/GGGG/BBBB notation, and the same terminator (ST vs. BEL) as in the query.

Examples:

echo -ne '\e]4;1;?\e\\'; cat

echo -ne '\e]11;?\a'; cat

I'm trying out the latest pre-release (https://github.com/microsoft/terminal/releases/tag/v1.22.2362.0) and these examples don't work? (Git Bash/mintty works however)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-VT Virtual Terminal sequence support Help Wanted We encourage anyone to jump in on these. In-PR This issue has a related PR Issue-Task It's a feature request, but it doesn't really need a major design. Needs-Tag-Fix Doesn't match tag requirements Product-Conhost For issues in the Console codebase
Projects
None yet
Development

Successfully merging a pull request may close this issue.