4

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!

2
  • 1
    en.wikipedia.org/wiki/ANSI_escape_code Commented Aug 1, 2014 at 21:51
  • I get that the 30+i and 40+i are different, but I can't chain them together when doing a print? Commented Aug 1, 2014 at 21:57

1 Answer 1

4

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[ options m

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.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.