Skip to content
Sign upLog in
← Back to Community

How do I make colored text in C++?

Profile icon
Joshuahaojie

Just a beginner in C++, how do I make the most effective code for C++ to make colored text ?

Answered by PattanAhmed [earned 5 cycles]
View Answer
Voters
Profile icon
ZM-10CS-713268
Profile icon
nxzpro1
Profile icon
Joshuahaojie
Comments
hotnewtop
Profile icon
PattanAhmed

@Joshuahaojie
Hi,
Here are some colors that you can use in C++

#include <iostream> #define RESET "\033[0m" #define BLACK "\033[30m" /* Black */ #define RED "\033[31m" /* Red */ #define GREEN "\033[32m" /* Green */ #define YELLOW "\033[33m" /* Yellow */ #define BLUE "\033[34m" /* Blue */ #define MAGENTA "\033[35m" /* Magenta */ #define CYAN "\033[36m" /* Cyan */ #define WHITE "\033[37m" /* White */ #define BOLDBLACK "\033[1m\033[30m" /* Bold Black */ #define BOLDRED "\033[1m\033[31m" /* Bold Red */ #define BOLDGREEN "\033[1m\033[32m" /* Bold Green */ #define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ #define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */ #define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */ #define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */ #define BOLDWHITE "\033[1m\033[37m" /* Bold White */ int main() { std::cout << BLUE << "Hello World!\n"; }

Thanks!

Hope this helps

Profile icon
Joshuahaojie

@PattanAhmed
Thanks man ! Also, is there any way to change background color?

Profile icon
PattanAhmed

@Joshuahaojie
Welcome!
I think this website might help you now:-
Click here
Thanks!

Profile icon
Coder100

Working repl here

Profile icon
Coder100

Hi! I put them in a struct:

struct ColorStruct { std::string reset = "\x1b[0m"; std::string bold = "\x1b[1m"; std::string italic = "\x1b[3m"; std::string underline = "\x1b[4m"; std::string inverse = "\x1b[7m"; std::string black = "\x1b[30m"; std::string red = "\x1b[31m"; std::string green = "\x1b[32m"; std::string yellow = "\x1b[33m"; std::string blue = "\x1b[34m"; std::string magenta = "\x1b[35m"; std::string cyan = "\x1b[36m"; std::string white = "\x1b[37m"; std::string gray = "\x1b[90m"; std::string bright_red = "\x1b[91m"; std::string bright_green = "\x1b[92m"; std::string bright_yellow = "\x1b[93m"; std::string bright_blue = "\x1b[94m"; std::string bright_magenta = "\x1b[95m"; std::string bright_cyan = "\x1b[96m"; std::string bright_white = "\x1b[97m"; std::string bg_black = "\x1b[40m"; std::string bg_red = "\x1b[41m"; std::string bg_green = "\x1b[42m"; std::string bg_yellow = "\x1b[43m"; std::string bg_blue = "\x1b[44m"; std::string bg_magenta = "\x1b[45m"; std::string bg_cyan = "\x1b[46m"; std::string bg_white = "\x1b[47m"; std::string bg_gray = "\x1b[100m"; std::string bg_bright_red = "\x1b[101m"; std::string bg_bright_green = "\x1b[102m"; std::string bg_bright_yellow = "\x1b[103m"; std::string bg_bright_blue = "\x1b[104m"; std::string bg_bright_magenta = "\x1b[105m"; std::string bg_bright_cyan = "\x1b[106m"; std::string bg_bright_white = "\x1b[107m"; }; ColorStruct colors; std::cout << colors.red << "red text" << colors.reset << " normal text";

Read more here

Profile icon
Coder100

Hi! I put them in a struct:

Profile icon
k9chelsea2