skip to Main Content

I want to be able to use the terminal to see RGB coloring for just one "swatch" character (■) as this is pretty convenient. I also like the dark themes (In my case I use Dark+ theme). The problem is that I can’t see the true color of the swatch because the dark theme overrides it.

In Python terminal print('x1b[38;2;0;0;0m■x1b[0m') and in zsh terminal echo 'x1b[38;2;0;0;0m■x1b[0m' using black coloring (rgb (0,0,0)) both result in a medium gray:
enter image description here

enter image description here

But if I change the theme (in this case Light+ theme)or just go to zsh directly the character prints as black (as expected):

enter image description here

How can I get this one character (■) to print without the color theme over riding it (and hopefully, not permanently altering the color theme) regardless of the RGB values I pass? Ideally, I’d want a solution that can work temporarily (on the fly, print, back to set theme) from Python (not sure if this is possible; I do have code cli installed) without over riding the settings permanently. Though, for other future searchers, it may be instructive to see how to permanently override the color theme for just one character.

2

Answers


  1. The foreground color is changed to meet the contrast ratio specified. The default minimum contrast ratio is 4.5 (WCAG AA compliance [minimum]). Setting it to 1 in VS code settings will display the color as it is.

    "terminal.integrated.minimumContrastRatio": 1
    
    Login or Signup to reply.
  2. As @Dan has stated in their answer, what you’re observing is due to VS Code’s terminal minimum contrast ratio feature. You can get the behaviour you want by setting it to 1, which disables the feature and causes colours to be rendered as-is:

    "terminal.integrated.minimumContrastRatio": 1
    

    As for your bounty, where you want a way to apply this to only a specific Python process, I don’t imagine there would be a "beautiful" way to do that. Python just outputs ANSI escape codes and has no idea how or any control over how they get interpreted (and used for rendering) by whatever program (in this case, VS Code’s integrated terminal) is observing its output.

    You could write your python program to edit your VS Code settings on startup and tear-down.

    Or you could use use the preLaunchTask and postDebugTasks to do that settings.json editing. Doing it in the tasks.json might be easier since you can use the ${workspaceFolder} variable there to pass to your script, and have the script edit the workspace settings.json file.

    Either way, for doing the settings.json modification, the question is how you want to do that. Using Python, you’d need to use a JSON parser that supports comments.

    I don’t really have the motivation to implement that (I’m not that fluent in Python) (sorry!). If anyone comes along and does, I’d suggest that for the pre-launch task, you create a backup copy of the original settings.json file, and in post-debugging task, restore that file, instead of trying more complicated means.

    And importantly, be aware that since the rendering is controlled by VS Code (and configured by its settings), once you change the settings, what VS Code has rendered previously in the terminal will also be "retroactively" updated. Even if you go through all this trouble, the printed character will immediately go back to how it was rendered before after restoring the previous setting. Something something Cinderella.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search