问题
I'm writing a program that needs to write text in different colors.
I have the escape code RED "\e[31m". However, if I make red on a red background, that won't work. So I also want to change the background color.
I've tried using this "\e[31m\e[94m" or even \e[4Xm to do text on a different background, however, this isn't working. Any ideas to get red text on a white background?
This is in the format std::cout << "COLORNAME" ....
Thanks!
回答1:
Multiple SGR options can be chained together, and in fact more compactly than you've been trying. The SGR syntax takes the general form of:
\e[optionsm
where options can consist of one* or more numbers separated by semicolons, which can include a number of things, but especially:
- 1: bold
- 30 – 37: set foreground color 0 – 7 (black, red, green, yellow, blue, magenta, cyan, white)
- 40 – 47: set background color 0 – 7 (ditto)
- 90 – 97: set foreground color 8 – 15 (bright versions of 0 – 7; "bright black" is a dark gray)
- 100 – 107: set background color 8 – 15 (ditto)
In your case, red text on a white background would be \e[31;47m.
(*: Or no numbers at all, in which case it resets all graphical options.)
来源:https://stackoverflow.com/questions/25089180/how-to-change-text-color-background-in-mac-terminal