How to change color of text output?

Feb 14, 2013 at 1:53am
I wanna change the three colors of text characters output on the exe screen with blue background, what functions do I use with console I/O?
Feb 14, 2013 at 2:00am
 
system("color 0c");


where 0 is the background color and c is the text color.

Colors are as follows:
1
2
3
4
5
6
7
8
    0 = Black       8 = Gray
    1 = Blue        9 = Light Blue
    2 = Green       A = Light Green
    3 = Aqua        B = Light Aqua
    4 = Red         C = Light Red
    5 = Purple      D = Light Purple
    6 = Yellow      E = Light Yellow
    7 = White       F = Bright White
Last edited on Feb 14, 2013 at 2:01am
Feb 14, 2013 at 2:45am
Apparently using "system()" is discouraged. There are many discussions in these forums about it and here's a link for detailed reasons: http://www.cplusplus.com/articles/j3wTURfi/

Instead I use this function (Warning, it may seem a bit confusing/overwhelming at first but it really isn't that bad):
1
2
3
4
void Color(int color)
{
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}

Just call this function whenever you need to change the color and pass a number as argument to the function. The name of the function is arbitrary. Ex. Color(7);

This is a copy of a program I made that I reference for colors. When you run it, it gives you the number in the color (both font and backround) that it stands for. Just use that number when calling the function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
#include<conio>
#include<windows>
using namespace std;

//COLORS LIST
//1: Blue
//2: Green
//3: Cyan
//4: Red
//5: Purple
//6: Yellow (Dark)
//7: Default white
//8: Gray/Grey
//9: Bright blue
//10: Brigth green
//11: Bright cyan
//12: Bright red
//13: Pink/Magenta
//14: Yellow
//15: Bright white
//Numbers after 15 include background colors

void Color(int color)
{
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}

main()
{
 int x;

 for(x=1;x<=256;x++)
  {
   Color(x);
   cout<<x<<endl;
  }

 getch();
 
 return 0;
}
Last edited on Feb 14, 2013 at 2:48am
Topic archived. No new replies allowed.