TrueReality  v0.1.1912
TextColor.cpp
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * @author Maxim Serebrennik
20 */
22 
23 
24 #if defined(_WIN32)
25  #include <windows.h>
26 #endif
27 
28 #include <iostream>
29 
30 
31 namespace trUtil::Console
32 {
34  void TextColor(TXT_COLOR TextColor)
35  {
36 #if defined(_WIN32)
37  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Get handle to standard output
38 #endif
39 
40  switch (TextColor)
41  {
42  case BRIGHT_RED:
43 #if defined(_WIN32)
44  SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
45 #else
46  std::cout << "\033[0;31m";
47 #endif //defined(_WIN32))
48  break;
49  case RED:
50 #if defined(_WIN32)
51  SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
52 #else
53  std::cout << "\033[2;31m";
54 #endif
55  break;
56  case BRIGHT_GREEN:
57 #if defined(_WIN32)
58  SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
59 #else
60  std::cout << "\033[0;32m";
61 #endif
62  break;
63  case GREEN:
64 #if defined(_WIN32)
65  SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
66 #else
67  std::cout << "\033[2;32m";
68 #endif
69  break;
70  case BRIGHT_BLUE:
71 #if defined(_WIN32)
72  SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
73 #else
74  std::cout << "\033[0;34m";
75 #endif
76  break;
77  case BLUE:
78 #if defined(_WIN32)
79  SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
80 #else
81  std::cout << "\033[3;34m";
82 #endif
83  break;
84  case BRIGHT_YELLOW:
85 #if defined(_WIN32)
86  SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
87 #else
88  std::cout << "\033[0;33m";
89 #endif
90  break;
91  case YELLOW:
92 #if defined(_WIN32)
93  SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED);
94 #else
95  std::cout << "\033[2;33m";
96 #endif
97  break;
98  case WHITE:
99 #if defined(_WIN32)
100  SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
101 #else
102  std::cout << "\033[0;37m";
103 #endif
104  break;
105  case BRIGHT_CYAN:
106 #if defined(_WIN32)
107  SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
108 #else
109  std::cout << "\033[0;36m";
110 #endif
111  break;
112  case CYAN:
113 #if defined(_WIN32)
114  SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE);
115 #else
116  std::cout << "\033[2;36m";
117 #endif
118  break;
119  case BRIGHT_MAGENTA:
120 #if defined(_WIN32)
121  SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
122 #else
123  std::cout << "\033[0;35m";
124 #endif
125  break;
126  case MAGENTA:
127 #if defined(_WIN32)
128  SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_BLUE);
129 #else
130  std::cout << "\033[2;35m";
131 #endif
132  break;
133  case GRAY:
134 #if defined(_WIN32)
135 #else
136  std::cout << "\033[2;37m";
137  break;
138 #endif
139  default:
140 #if defined(_WIN32)
141  SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
142 #else
143  std::cout << "\033[0m";
144 #endif
145  break;
146  }
147  }
148 
151  {
152  //Print a sample of colors on the screen
153  TextColor(DEFAULT); std::cout << "DEFAULT" << std::endl;
154  TextColor(BRIGHT_RED); std::cout << "BRIGHT_RED" << std::endl;
155  TextColor(RED); std::cout << "RED" << std::endl;
156  TextColor(BRIGHT_GREEN); std::cout << "BRIGHT_GREEN" << std::endl;
157  TextColor(GREEN); std::cout << "GREEN" << std::endl;
158  TextColor(BRIGHT_BLUE); std::cout << "BRIGHT_BLUE" << std::endl;
159  TextColor(BLUE); std::cout << "BLUE" << std::endl;
160  TextColor(BRIGHT_YELLOW); std::cout << "BRIGHT_YELLOW" << std::endl;
161  TextColor(YELLOW); std::cout << "YELLOW" << std::endl;
162  TextColor(WHITE); std::cout << "WHITE" << std::endl;
163  TextColor(BRIGHT_CYAN); std::cout << "BRIGHT_CYAN" << std::endl;
164  TextColor(CYAN); std::cout << "CYAN" << std::endl;
165  TextColor(BRIGHT_MAGENTA); std::cout << "BRIGHT_MAGENTA" << std::endl;
166  TextColor(MAGENTA); std::cout << "MAGENTA" << std::endl;
167  TextColor(GRAY); std::cout << "GRAY" << std::endl;
168 
169 
170  //Change the color back to normal
172 
173  }
174 }
BRIGHT_GREEN
Definition: TextColor.h:41
RED
Definition: TextColor.h:41
BLUE
Definition: TextColor.h:41
void TR_UTIL_EXPORT PrintAllColors()
Prints an example of all available colors This is usually used for testing and development.
Definition: TextColor.cpp:150
BRIGHT_CYAN
Definition: TextColor.h:41
GREEN
Definition: TextColor.h:41
WHITE
Definition: TextColor.h:41
void TR_UTIL_EXPORT TextColor(TXT_COLOR TextColor)
Changes Text color in the console window....
Definition: TextColor.cpp:34
MAGENTA
Definition: TextColor.h:41
BRIGHT_YELLOW
Definition: TextColor.h:41
CYAN
Definition: TextColor.h:41
DEFAULT
Definition: TextColor.h:41
YELLOW
Definition: TextColor.h:41
BRIGHT_MAGENTA
Definition: TextColor.h:41
BRIGHT_RED
Definition: TextColor.h:41
BRIGHT_BLUE
Definition: TextColor.h:41