Colors palette changes using SetConsoleScreenBufferInfoEx should trigger equivalent OSC 4 sequences #10973

Closed
opened 2026-01-31 02:35:07 +00:00 by claunia · 7 comments
Owner

Originally created by @PhMajerus on GitHub (Oct 11, 2020).

Win32 CUI apps typically use SetConsoleScreenBufferInfoEx to change the 16 colors palette used by the console.
Now that conhost can be used to connect these apps to a terminal, as much as possible of the original Console API needs to get translated to VT control sequences.

In the current implementation, conhost records the palette changes, as GetConsoleScreenBufferInfoEx will show the colors have been updated, but it will be kept purely as an internal state and the connected terminal app will not change its colors palette.

For Win32 CUI apps that change their colors to work properly with terminals, for any color that has been changed in the CONSOLE_SCREEN_BUFFER_INFOEX structure, we need conhost to generate and send the equivalent inband OSC 4 control sequences (ESC ] 4) to the terminal app.
(https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#screen-colors)

The following screenshot shows before and after changing the console colors, note how the right shows the new palette as it requests the actual conhost values through API and shows them using RGB/24-bit colors control sequences, while the colors wheel isn't updated because it uses 16-colors control sequences relying on the terminal's colors palette.
image

Same after a function has been used to get the console colors and generate and send equivalent OSC 4 control sequences.
image

My applyConsoleColorsToTerminal function is a workaround that simply gets the 16 colors using GetConsoleScreenBufferInfoEx and generates the 16 equivalent OSC 4 sequences to apply them to the attached terminal.

Originally created by @PhMajerus on GitHub (Oct 11, 2020). Win32 CUI apps typically use SetConsoleScreenBufferInfoEx to change the 16 colors palette used by the console. Now that conhost can be used to connect these apps to a terminal, as much as possible of the original Console API needs to get translated to VT control sequences. In the current implementation, conhost records the palette changes, as GetConsoleScreenBufferInfoEx will show the colors have been updated, but it will be kept purely as an internal state and the connected terminal app will not change its colors palette. For Win32 CUI apps that change their colors to work properly with terminals, for any color that has been changed in the CONSOLE_SCREEN_BUFFER_INFOEX structure, we need conhost to generate and send the equivalent inband OSC 4 control sequences (ESC ] 4) to the terminal app. (https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#screen-colors) The following screenshot shows before and after changing the console colors, note how the right shows the new palette as it requests the actual conhost values through API and shows them using RGB/24-bit colors control sequences, while the colors wheel isn't updated because it uses 16-colors control sequences relying on the terminal's colors palette. ![image](https://user-images.githubusercontent.com/25664275/95681939-e0fb1d80-0be2-11eb-861f-b7eb590a3991.png) Same after a function has been used to get the console colors and generate and send equivalent OSC 4 control sequences. ![image](https://user-images.githubusercontent.com/25664275/95681944-e9535880-0be2-11eb-868b-c4dcf8f814c2.png) My applyConsoleColorsToTerminal function is a workaround that simply gets the 16 colors using GetConsoleScreenBufferInfoEx and generates the 16 equivalent OSC 4 sequences to apply them to the attached terminal.
claunia added the Issue-FeatureResolution-Duplicate labels 2026-01-31 02:35:07 +00:00
Author
Owner

@PhMajerus commented on GitHub (Oct 11, 2020):

Note it seems doing this could also fix #399, as the legacy console properly refreshes its whole client area when the background color gets changed through OSC 4.

@PhMajerus commented on GitHub (Oct 11, 2020): Note it seems doing this could also fix #399, as the legacy console properly refreshes its whole client area when the background color gets changed through OSC 4.
Author
Owner

@j4james commented on GitHub (Oct 11, 2020):

Isn't this just a duplicate of #2985?

@j4james commented on GitHub (Oct 11, 2020): Isn't this just a duplicate of #2985?
Author
Owner

@PhMajerus commented on GitHub (Oct 11, 2020):

Ah, sorry, didn't spot that one when looking for existing colors issues. Indeed, it is the same core issue, we need to propagate palette changes with OSC 4 to fix this and hopefully #399 as well.

@PhMajerus commented on GitHub (Oct 11, 2020): Ah, sorry, didn't spot that one when looking for existing colors issues. Indeed, it is the same core issue, we need to propagate palette changes with OSC 4 to fix this and hopefully #399 as well.
Author
Owner

@j4james commented on GitHub (Oct 11, 2020):

#399 is a conhost bug though. It's got nothing to do with conpty. I've actually hacked together fixes for both of these issues in the past, just as a proof of concept. Fixing them properly is more complicated though, which is why I never got around to submitting PRs.

@j4james commented on GitHub (Oct 11, 2020): #399 is a conhost bug though. It's got nothing to do with conpty. I've actually hacked together fixes for both of these issues in the past, just as a proof of concept. Fixing them properly is more complicated though, which is why I never got around to submitting PRs.
Author
Owner

@PhMajerus commented on GitHub (Oct 11, 2020):

Could be, as the console definitely should refresh its whole client area when the colors are changed, however, it seems it does it properly when an OSC 4 command is processed, so it finally updates on that second pass.
While not fixing the original bug, it is still better than the bug we've been living with for over a year and a half by now. I really hope we can get all those colors issues fixed for 21H1 release.

@PhMajerus commented on GitHub (Oct 11, 2020): Could be, as the console definitely should refresh its whole client area when the colors are changed, however, it seems it does it properly when an OSC 4 command is processed, so it finally updates on that second pass. While not fixing the original bug, it is still better than the bug we've been living with for over a year and a half by now. I really hope we can get all those colors issues fixed for 21H1 release.
Author
Owner

@DHowett commented on GitHub (Oct 11, 2020):

Thanks all! /dup #2985

@DHowett commented on GitHub (Oct 11, 2020): Thanks all! /dup #2985
Author
Owner

@ghost commented on GitHub (Oct 11, 2020):

Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!

@ghost commented on GitHub (Oct 11, 2020): Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#10973