console colors in C++

//This is a header file taken from cplusplus.com
//http://www.cplusplus.com/articles/Eyhv0pDG/
//concol.h
#ifndef _INC_EKU_IO_CONCOL
#define _INC_EKU_IO_CONCOL

/*Header file to color text and background in windows console applications
Global variables - textcol,backcol,deftextcol,defbackcol,colorprotect*/

#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 //CONCOL

	HANDLE std_con_out;
	//Standard Output Handle
	bool colorprotect=false;
	//If colorprotect is true, background and text colors will never be the same
	concol textcol,backcol,deftextcol,defbackcol;
	/*textcol - current text color
	backcol - current back color
	deftextcol - original text color
	defbackcol - original back color*/

	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;
	}
	
}	//end of namespace eku

#endif	//_INC_EKU_IO_CONCOL

4.5
2
Sreng Pagna 110 points

                                    //This is one way to do it. Taken from stackoverflow.
system(&quot;color 70&quot;);
//It just runs a cmd command.

4.5 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
c++ console colour numbers c++ print colored characters on console color text console c++ c++ console color output console text color c/c++ windows console colors c++ c++ color console text how to add colors to console in c++ showing colour text in c++ console colour full console in c++ how to change console color in c++ c++ how to print colored text to console console attributes c++ colors cpp set console color console colour c++ c++ set color console c++ change console color simple c++ console individual color c++ colors console black color for console output c++ how to return a console color c++ colors in console c++ print in console with colors c++ default console text color in c++ how to change console text color in c++ c++ change color console change color c++ console add color to console C++ c++ console color text color console characters c++ c++ color console codes set console color in c++ c++ color text console change color console c++ how to change color console in c++ colors in console using c++ color text c++ console color c++ console different console text colors in C++ best console color in C++ set console color c++ c++ coloured console Colors in the console in C++ change console color text c++ cout console color in c++ cpp console coolor how to make text console with windows.h change text color c++ console color cpp c++ colored console setconsoletextattribute c++ colors change console colour c++ colors in c++ console c++ print colored text windows print in color windows.h c windows.h c print in color c++ printing in color in windowsd system(&quot;Color 1A&quot;); all colors cpp color text cmd cpp highlight console change color of console in c++ windows colors terminal c++ c++ color terminal windows run c++ in cmd but no color why my c++ output in cmd no color c++ color in cmd how to change letter color in console c++ Console color codes c++ consola colors c++ color console c++ color default console c++ color default consola c++ C++ console colors windows.h console color exercises windows.h console color examples color in the cmd code c++ windows cmd std::cout color change color of console text in c++ console text attribute in c++ c++ colored ascii c++ white console how to write with color to terminal with c++ colors c++ C++ what is col code c++ colors set console text c++ colors c++ colored text powershell rand color terminal cpp colors in windows.h c++ system color codes c++ red color c++ console text color multiplatform c++ how to change console color color in c++ console visual c++ console character color colors with c++ regular colors with c++ how to change the console text color in c++ color text for console C++ c++ command line color red text c++ reset console text attribute c++ c++ colored output windows c++ console app colour cpp use color cpp color codes system color std::cout color windows c++ system color A1 list c++ console text color c++ console colour console color C++ c++ console change text color how to set console output color in c++ how to change color of console in c++ add color ti c++ command line c++ set console color console colors c++ color writing c++ console color writing c++ cmd change color of console c++ c++ cout &lt;&lt; color text windows print colored text c++ windows loop through every system color c++ colors in c++ highlight text console C++ console color cpp c++ change text color console c++ cout color windows set console pink c++ how to change font color in c++ console c++ console font color how to give green color output in cmd cpp code c++ windows color console color in c++ output windows c++ change console attrivutes c++ text color console c++ windows color c++ cmd color text how to change terminal color in c++ color in c++ c++ console change color changing console color c++ how to change the color of the console text in c++ change text color in c++ console c++ color console change console color c++ color cycle c++ with white change console text color c++ C++ change std::cout color windows how to change output color in c++ windows c++ output color text windows c++ set console text color c++ cout in colour windows change terminal color c++ c++ log color change output color in c++ console c++ consoleatribute colors how to use color c++ c++ colors in console c++ windows set console color c++ change color of console text c++ change console color c++ console colours change console text mode c++ c++ console color colorize screen c++ colored c++ console console text color c++ what coloring does c++ use c++ coloured text in console can i change cmd colors in code c++ console colors in C++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source