3

I'm a beginner in C++ programming and I want to know how to set a text color for individual texts. I know how to set text colors using system("COLOR ..") but it applies color to ALL texts, not individual texts. There's a program I've been coding wherein when the text is "Yes" that "Yes" will be color green and when it is "No", that "No" will be color red. This is for console application.

cout<<"Available: ";
if(available == true){
//code for setting text colors to GREEN
}
else{
//code for setting text colors to RED
}
cout<<yesno;
//code for setting text colors back to WHITE

so the output will be like for example

Available: (textcolor="green")Yes(/textcolor)

Thank you for any help!

Rex
  • 33
  • 1
  • 4
  • 1
    C++ doesn't have a graphics library, so you're either talking about the console window or you're using a graphics library in which case you'll need to tell us which one. – Tas Feb 16 '16 at 11:29
  • @Tas I'm really really sorry, I overlooked that one. It is for console window – Rex Feb 16 '16 at 11:33
  • The color of text sent to `std::cout` is nothing to do with `C++` its controlled by whatever display device displays the text. In a console these usually accept various control codes. Which ones depends on the console. GOOGLE "ANSI escape codes", that's very common. – Galik Feb 16 '16 at 11:34
  • As @Tas mentioned on comment is right. Console out put has default color white on black screen. You can't use different color on output screen. For color combination go for c++ graphics programming. You can have numerous libraries and methods to change your different output color. – Kulamani Feb 16 '16 at 11:36
  • Possible duplicate of [Color console output with C++ in Windows](http://stackoverflow.com/questions/9262270/color-console-output-with-c-in-windows) – Tas Feb 16 '16 at 11:37

3 Answers3

3

In addition to the existing answers, if you need a portable way of doing it and hide setting colors behind an API. There's a single header library rlutil, which does that for you, wrapping ANSI and Windows color and other console manipulations:

rlutil::setColor(rlutil::GREY)
Dmitry Ledentsov
  • 3,620
  • 18
  • 28
1

You need to print your text using ANSI colour codes; but not all terminals support this - if colour sequences are not supported, garbage will show up.

Here is and example:

cout << "\033[1;31mbold red text\033[0m\n";

Here, \033 is the ESC character, ASCII 27. Followed by [, then one or two numbers separated by ;, and finally the letter m. See this table on Wikipedia for details.

Pandrei
  • 4,843
  • 3
  • 27
  • 44
0

Under Linux, you can do something like this:

#include <iostream>
using namespace std;
int main() {
    cout << "\033[1;30mblack" << endl
         << "\033[1;31mred" << endl
         << "\033[1;32mgreen" << endl
         << "\033[1;33myellow" << endl
         << "\033[1;34mblue" << endl
         << "\033[1;35mmagenta" << endl
         << "\033[0mback to normal" << endl;
    return 0;
}

Check this wiki for color table.

Under Windows, you can use SetConsoleTextAttribute, like this:

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);

Check this page for all the character attributes.

Ziming Song
  • 1,176
  • 1
  • 9
  • 22