Fading terminal character

Hello,

I am trying to have a character on a terminal screen fade away, like this:
char fade

Right now the way I am doing it, with crossterm, is by rewriting the character over time, each time with a darker color.

But, as you can maybe see, it does not work, because the background color of the terminal may not be black.
So is there a way to achieve this effect ? I was thinking maybe crossterm would have a get_terminal_background_color function or something similar, but apparently not.

Terminals don't report their color scheme. The best you can do is draw the text (and other things surrounding it) using an explicitly black background color.

2 Likes

There is a way to query terminal colors in xterms and related, though I didn't see it in crossterm or termion at a brief glance (and am not really sure how broad the support is across terminals more generally). And some programs also guess a lot based on your environment.

I'm afraid I don't have time to contribute more right now, but here's a stackoverflow on the topic with a number of links one could chase.

I don't think any of the methods in the stackoverflow thread guarantee the expected output. For example, if I do echo $COLORFGBG, I get 7;0, but my terminal (iTerm2) has an image set as the background, so fading to black probably wouldn't make the text invisible. You'd need to set the actual text transparency. The docs don't indicate that this would even be supported, but they do have some notes on reporting the BG colors:

The xterm-defined OSC 4 control sequence has a mode where it reports the RGB value of a color. iTerm2 extends its reporting mode to add two additional color indices representing the default foreground and background color.

To get the background color:

OSC 4 ; -2; ? ST

And this gets the foreground color:

OSC 4 ; -1 ; ? ST

As far as I can tell, OSC 4 is doing 'set color', where -1/-2 represent background/forground respectively, and ? is indicating to query rather than set. Searching for OSC (operating system control) codes might reveal more, but it looks like that's only one of a number of different standards that may or may not be implemented for a given terminal. And even these example commands are proprietary, so you can't even expect this functionality from the standard.

1 Like

Humm, it seems there are no robust/crossplatform way to do this... I think I should just set the background color.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.