Skip to content
← Back to Community
how do i change the color of the text on c++
Profile icon
jnrchewie

i want to change the text color like i did in space adventure but i dont know how to do it in c++
please help

Answered by Bookie0 [earned 5 cycles]
View Answer
Voters
Profile icon
JpBarron2010
Profile icon
SwateP
Profile icon
IslamicHyjacker
Profile icon
PdVivekanand
Profile icon
jnrchewie
Comments
hotnewtop
Profile icon
Bookie0

Check out a tutorial here (scroll down to colors):

ANSI Escapes Codes

Now you might be wondering, how can I change the colour of the console text? Squids can chnage colour, so why can't the console? Well you actually can!

There's something called an ANSI code. It let's you specify what colour your text should be! You can use it like this:

cout << "\033[COLOUR_CODEm" << "This text will be coloured!";

Notice how it begins with \033[, and ends with m. Here are the colour codes, for coloring text and the background of the text:

Screen Shot 2020-11-01 at 3.21.14 PM

Notice how it's only a number. So for example, if you want red text, you would do something like this:

cout << "\033[31m" << "This text is red" << endl;

Output:

Screen Shot 2020-10-28 at 1.55.43 PM

Or if we want a background of bright cyan:

cout << "\033[106m" << "This text has a background of bright cyan" << endl;

Output:

Screen Shot 2020-10-28 at 1.58.30 PM

We can also mix and match them! Here's a fun combo using bright yellow background, green background, blue text, and bright magenta text. We'll also use reset to reset the color back to the default (no background color and white plain text):

cout << "\033[103m" << "\033[34m" << "Wow" << "\033[0m" << " back to default" << "\033[102m" << "\033[95m" << " So cool!" << "\033[0m" << endl;

Output:

Screen Shot 2020-10-30 at 12.56.31 PM

you can also specify RGB values for ansi escape sequences
this can be done with
\033[38;2;<r>;<g>;<b>m
you can also set the RGB of the background using
\033[48;2;<r>;<g>;<b>m
(ive used ansi escape sequences alot)

~ from @Nettakrim

Only if the teletype supports true (24 bit) color.

~ from @CodeLongAndPros

Lemme know if you have any questions! :)

Profile icon
jnrchewie

@Bookie0 ok thank you

Profile icon
Bookie0

No problem! @jnrchewie mark as correct answer? (If it helped :)

Profile icon
jnrchewie

@Bookie0 sorry thought I did mark it

Profile icon
Bookie0

@jnrchewie ah okay. anyways good luck! :D

Profile icon
JustinArgue

Can you do this with chars?

Profile icon
PdVivekanand

@jnrchewie Thanks!