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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#ifndef COLOR_HPP_INCLUDED
#define COLOR_HPP_INCLUDED
#include <windows.h>
bool textcolorprotect=true;
/* doesn't let textcolor be the same as background color if true */
enum colors
{
black = 0,
dark_blue = 1,
dark_green = 2,
dark_aqua = 3, dark_cyan = 3,
dark_red = 4,
dark_purple = 5, dark_pink = 5, dark_magenta = 5,
dark_yellow = 6,
dark_white = 7,
gray = 8,
blue = 9,
green = 10,
aqua = 11, cyan = 11,
red = 12,
purple = 13, pink = 13, magenta = 13,
yellow = 14,
white = 15
};
inline void setcolor(colors textcolor, colors backcolor);
inline void setcolor(int textcolor, int backcolor);
int textcolor(); /* returns current text color */
int backcolor(); /* returns current background color */
#define std_con_out GetStdHandle(STD_OUTPUT_HANDLE)
int textcolor()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(std_con_out, &csbi);
int a = csbi.wAttributes;
return a%16;
}
int backcolor()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(std_con_out, &csbi);
int a = csbi.wAttributes;
return (a/16)%16;
}
inline void setcolor(colors textcol, colors backcol)
{setcolor(int(textcol), int(backcol));}
inline void setcolor(int textcol, int backcol)
{
if(textcolorprotect)
{if((textcol%16)==(backcol%16))textcol++;}
textcol%=16; backcol%=16;
unsigned short wAttributes= ((unsigned)backcol<<4) | (unsigned)textcol;
SetConsoleTextAttribute(std_con_out, wAttributes);
}
#if defined(_INC_OSTREAM)||defined(_IOSTREAM_)
ostream& operator<<(ostream& os, colors c)
{os.flush();setcolor(c, backcolor()); return os;}
#endif
#if defined(_INC_ISTREAM)||defined(_IOSTREAM_)
istream& operator>>(istream& is, colors c)
{cout.flush();setcolor(c, backcolor()); return is;}
#endif
#endif // COLOR_HPP_INCLUDED
|