Last active
January 23, 2020 07:52
-
-
Save Fuwn/c97717cba2e637fa69bb51fc1cd79587 to your computer and use it in GitHub Desktop.
Windows specific console out text colour changer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: | |
// std::cout << COLOUR_HERE << "Text"; | |
// Example: | |
// std::cout << red << "Text"; | |
// | |
// You can also make new colours by combing the base RGB colours. | |
// FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_BLUE are the base colours. | |
// FOREGROUND_INTENSITY gives vibrance. The colour name can be changed by | |
// changing; inline std::ostream& THIS_VALUE_HERE(std::ostream& s) | |
#pragma once | |
#include <iostream> | |
#include <windows.h> | |
inline std::ostream& blue(std::ostream& s) | |
{ | |
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE | |
| FOREGROUND_GREEN | FOREGROUND_INTENSITY); | |
return s; | |
} | |
inline std::ostream& red(std::ostream& s) | |
{ | |
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
SetConsoleTextAttribute(hStdout, | |
FOREGROUND_RED | FOREGROUND_INTENSITY); | |
return s; | |
} | |
inline std::ostream& green(std::ostream& s) | |
{ | |
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
SetConsoleTextAttribute(hStdout, | |
FOREGROUND_GREEN | FOREGROUND_INTENSITY); | |
return s; | |
} | |
inline std::ostream& yellow(std::ostream& s) | |
{ | |
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
SetConsoleTextAttribute(hStdout, | |
FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); | |
return s; | |
} | |
inline std::ostream& white(std::ostream& s) | |
{ | |
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
SetConsoleTextAttribute(hStdout, | |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); | |
return s; | |
} | |
inline std::ostream& purple(std::ostream& s) | |
{ | |
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
SetConsoleTextAttribute(hStdout, | |
FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY); | |
return s; | |
} | |
struct color { | |
color(WORD attribute) :m_color(attribute) {}; | |
WORD m_color; | |
}; | |
template <class _Elem, class _Traits> | |
std::basic_ostream<_Elem, _Traits>& | |
operator<<(std::basic_ostream<_Elem, _Traits>& i, color& c) | |
{ | |
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
SetConsoleTextAttribute(hStdout, c.m_color); | |
return i; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment