Example 1: c++ console color some digits
#include "colormod.h"
#include <iostream>
using namespace std;
int main() {
Color::Modifier red(Color::FG_RED);
Color::Modifier def(Color::FG_DEFAULT);
cout << "This ->" << red << "word" << def << "<- is red." << endl;
}
#include <ostream>
namespace Color {
enum Code {
FG_RED = 31,
FG_GREEN = 32,
FG_BLUE = 34,
FG_DEFAULT = 39,
BG_RED = 41,
BG_GREEN = 42,
BG_BLUE = 44,
BG_DEFAULT = 49
};
class Modifier {
Code code;
public:
Modifier(Code pCode) : code(pCode) {}
friend std::ostream&
operator<<(std::ostream& os, const Modifier& mod) {
return os << "\033[" << mod.code << "m";
}
};
}
Example 2: console colors in C++
#ifndef _INC_EKU_IO_CONCOL
#define _INC_EKU_IO_CONCOL
#include<windows.h>
#include<iosfwd>
namespace eku
{
#ifndef CONCOL
#define CONCOL
enum concol
{
black=0,
dark_blue=1,
dark_green=2,
dark_aqua,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
};
#endif
HANDLE std_con_out;
bool colorprotect=false;
concol textcol,backcol,deftextcol,defbackcol;
inline void update_colors()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(std_con_out,&csbi);
textcol = concol(csbi.wAttributes & 15);
backcol = concol((csbi.wAttributes & 0xf0)>>4);
}
inline void setcolor(concol textcolor,concol backcolor)
{
if(colorprotect && textcolor==backcolor)return;
textcol=textcolor;backcol=backcolor;
unsigned short wAttributes=((unsigned int)backcol<<4) | (unsigned int)textcol;
SetConsoleTextAttribute(std_con_out,wAttributes);
}
inline void settextcolor(concol textcolor)
{
if(colorprotect && textcolor==backcol)return;
textcol=textcolor;
unsigned short wAttributes=((unsigned int)backcol<<4) | (unsigned int)textcol;
SetConsoleTextAttribute(std_con_out,wAttributes);
}
inline void setbackcolor(concol backcolor)
{
if(colorprotect && textcol==backcolor)return;
backcol=backcolor;
unsigned short wAttributes=((unsigned int)backcol<<4) | (unsigned int)textcol;
SetConsoleTextAttribute(std_con_out,wAttributes);
}
inline void concolinit()
{
std_con_out=GetStdHandle(STD_OUTPUT_HANDLE);
update_colors();
deftextcol=textcol;defbackcol=backcol;
}
template<class elem,class traits>
inline std::basic_ostream<elem,traits>& operator<<(std::basic_ostream<elem,traits>& os,concol col)
{os.flush();settextcolor(col);return os;}
template<class elem,class traits>
inline std::basic_istream<elem,traits>& operator>>(std::basic_istream<elem,traits>& is,concol col)
{
std::basic_ostream<elem,traits>* p=is.tie();
if(p!=NULL)p->flush();
settextcolor(col);
return is;
}
}
#endif
Example 3: c++ console color some digits
#include <ostream>
namespace Color {
enum Code {
FG_RED = 31,
FG_GREEN = 32,
FG_BLUE = 34,
FG_DEFAULT = 39,
BG_RED = 41,
BG_GREEN = 42,
BG_BLUE = 44,
BG_DEFAULT = 49
};
class Modifier {
Code code;
public:
Modifier(Code pCode) : code(pCode) {}
friend std::ostream&
operator<<(std::ostream& os, const Modifier& mod) {
return os << "\033[" << mod.code << "m";
}
};
}