I found an old game I wrote using ncurses
. I noticed that after closing the game and running another ncurses
based program, the colours had changed for that program as well. This is entirely expected behaviour that I wasn't aware of when I wrote the game several years ago.
Since ncurses
doesn't know the initial palette, there is no way to reset the palette to its initial values after quitting the game. With this in mind, what is the best way to deal with colours in ncurses
?
Here's what I've considered:
- Only override colours that are higher up in the palette. If you start changing colours from position 0, it's very likely that you will change colours that other programs are using. If you start at 100 (for instance), it's much less likely that your changes will interfere with another program. I suppose not changing the 16 colour ANSI palette is the key here.
- Manually inspect the default palette and choose default colours that fit the game. Besides being cumbersome and perhaps not letting you set the exact colour you want, there is no guarantee that the default palette will be the same on all systems. There might be a library to automatically get the corresponding
xterm
colour. It seems like https://github.com/vim-scripts/CSApprox is doing something like that. I would assume that most modern systems will run terminals with the same palette, so it might be fine, depending on what systems you are targeting. The default palette ofxterm
is available here: https://jonasjacek.github.io/colors/ - Use
color_content
to get the original colour. I tried it and it didn't work.ncurses
can only get the colour content of colours that were set byinit_color
. - Restore the palette to the default
xterm
palette. I suppose if the terminal supports 256 colours, there's a good chance that it is using the defaultxterm
palette. However, the user might have changed it, so it might still interfere with the users configuration. - Use API specific to
xterm
or other terminal emulator for getting colours. I don't know if any such API exists, but it would be a potential solution, with the major drawback that it will only support that specific terminal emulator.
I might implement both 1 and 2, and let the user choose the behaviour by passing a flag. I don't know of any terminal programs that use rich colours, so not sure how many programs out there actually face this problem, but there's bound to be at least a few. If you have any examples of how other programs deal with this, that would be very helpful.