Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
colorful.h
Go to the documentation of this file.
1
8
9/*
10Copyright (c) 2026 MrXie1109
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files (the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions:
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29*/
30
31#pragma once
32#include <iostream>
33
34namespace console {
40 namespace color {
41 // ------------------------------ 前景色 ------------------------------
43 static constexpr char Black[] = "\033[30m";
45 static constexpr char Red[] = "\033[31m";
47 static constexpr char Green[] = "\033[32m";
49 static constexpr char Yellow[] = "\033[33m";
51 static constexpr char Blue[] = "\033[34m";
53 static constexpr char Magenta[] = "\033[35m";
55 static constexpr char Cyan[] = "\033[36m";
57 static constexpr char White[] = "\033[37m";
59 static constexpr char BrightBlack[] = "\033[90m";
61 static constexpr char BrightRed[] = "\033[91m";
63 static constexpr char BrightGreen[] = "\033[92m";
65 static constexpr char BrightYellow[] = "\033[93m";
67 static constexpr char BrightBlue[] = "\033[94m";
69 static constexpr char BrightMagenta[] = "\033[95m";
71 static constexpr char BrightCyan[] = "\033[96m";
73 static constexpr char BrightWhite[] = "\033[97m";
75 static constexpr char Reset[] = "\033[0m";
76
77 // ------------------------------ 背景色 ------------------------------
79 static constexpr char BgBlack[] = "\033[40m";
81 static constexpr char BgRed[] = "\033[41m";
83 static constexpr char BgGreen[] = "\033[42m";
85 static constexpr char BgYellow[] = "\033[43m";
87 static constexpr char BgBlue[] = "\033[44m";
89 static constexpr char BgMagenta[] = "\033[45m";
91 static constexpr char BgCyan[] = "\033[46m";
93 static constexpr char BgWhite[] = "\033[47m";
95 static constexpr char BgBrightBlack[] = "\033[100m";
97 static constexpr char BgBrightRed[] = "\033[101m";
99 static constexpr char BgBrightGreen[] = "\033[102m";
101 static constexpr char BgBrightYellow[] = "\033[103m";
103 static constexpr char BgBrightBlue[] = "\033[104m";
105 static constexpr char BgBrightMagenta[] = "\033[105m";
107 static constexpr char BgBrightCyan[] = "\033[106m";
109 static constexpr char BgBrightWhite[] = "\033[107m";
110
111 // ------------------------------ 文本样式
112 // ------------------------------
114 static constexpr char Bold[] = "\033[1m";
116 static constexpr char Dim[] = "\033[2m";
118 static constexpr char Italic[] = "\033[3m";
120 static constexpr char Underline[] = "\033[4m";
122 static constexpr char Blink[] = "\033[5m";
124 static constexpr char Reverse[] = "\033[7m";
126 static constexpr char Hidden[] = "\033[8m";
128 static constexpr char Strikethrough[] = "\033[9m";
129 }
130
137 inline void
138 cc(const char *clr = color::Reset, std::ostream &os = std::cout) {
139 os << clr;
140 }
141
143 inline void
144 cc(const char *clr = color::Reset, std::wostream &os = std::wcout) {
145 os << clr;
146 }
147
153 std::ostream &os_;
154
155 public:
161 explicit ColorGuard(const char *color, std::ostream &os = std::cout) :
162 os_(os) {
163 os << color;
164 }
165
170
171 // 禁止拷贝
172 ColorGuard(const ColorGuard &) = delete;
173 ColorGuard &operator=(const ColorGuard &) = delete;
174 };
175
181 std::wostream &os_;
182
183 public:
189 explicit WColorGuard(
190 const char *color, std::wostream &os = std::wcout) : os_(os) {
191 os << color;
192 }
193
198
199 // 禁止拷贝
200 WColorGuard(const WColorGuard &) = delete;
202 };
203}
ColorGuard(const ColorGuard &)=delete
ColorGuard(const char *color, std::ostream &os=std::cout)
构造函数,应用指定颜色。
Definition colorful.h:161
~ColorGuard()
析构函数,自动恢复默认颜色。
Definition colorful.h:169
ColorGuard & operator=(const ColorGuard &)=delete
std::ostream & os_
输出流引用
Definition colorful.h:153
WColorGuard(const WColorGuard &)=delete
WColorGuard(const char *color, std::wostream &os=std::wcout)
构造函数,应用指定颜色。
Definition colorful.h:189
WColorGuard & operator=(const WColorGuard &)=delete
std::wostream & os_
输出流引用
Definition colorful.h:181
~WColorGuard()
析构函数,自动恢复默认颜色。
Definition colorful.h:197
包含控制台颜色、背景色和文本样式的 ANSI 转义序列常量。
static constexpr char Hidden[]
隐藏(不可见)
Definition colorful.h:126
static constexpr char BgBrightBlue[]
亮蓝色背景
Definition colorful.h:103
static constexpr char Reset[]
重置所有颜色/样式
Definition colorful.h:75
static constexpr char BgMagenta[]
品红背景
Definition colorful.h:89
static constexpr char Bold[]
粗体
Definition colorful.h:114
static constexpr char Reverse[]
反转(交换前景和背景)
Definition colorful.h:124
static constexpr char BrightCyan[]
亮青色前景
Definition colorful.h:71
static constexpr char BgRed[]
红色背景
Definition colorful.h:81
static constexpr char Underline[]
下划线
Definition colorful.h:120
static constexpr char BgBrightBlack[]
亮黑色背景
Definition colorful.h:95
static constexpr char Cyan[]
青色前景
Definition colorful.h:55
static constexpr char BrightBlack[]
亮黑色(灰)前景
Definition colorful.h:59
static constexpr char BgYellow[]
黄色背景
Definition colorful.h:85
static constexpr char BgBrightCyan[]
亮青色背景
Definition colorful.h:107
static constexpr char BgBrightYellow[]
亮黄色背景
Definition colorful.h:101
static constexpr char BrightYellow[]
亮黄色前景
Definition colorful.h:65
static constexpr char Blink[]
闪烁
Definition colorful.h:122
static constexpr char BgBrightRed[]
亮红色背景
Definition colorful.h:97
static constexpr char Yellow[]
黄色前景
Definition colorful.h:49
static constexpr char Green[]
绿色前景
Definition colorful.h:47
static constexpr char BrightRed[]
亮红色前景
Definition colorful.h:61
static constexpr char Blue[]
蓝色前景
Definition colorful.h:51
static constexpr char Red[]
红色前景
Definition colorful.h:45
static constexpr char BgGreen[]
绿色背景
Definition colorful.h:83
static constexpr char Italic[]
斜体
Definition colorful.h:118
static constexpr char BgBrightWhite[]
亮白色背景
Definition colorful.h:109
static constexpr char Dim[]
暗淡(降低亮度)
Definition colorful.h:116
static constexpr char BgWhite[]
白色背景
Definition colorful.h:93
static constexpr char BrightMagenta[]
亮品红前景
Definition colorful.h:69
static constexpr char BgCyan[]
青色背景
Definition colorful.h:91
static constexpr char BgBlack[]
黑色背景
Definition colorful.h:79
static constexpr char Black[]
黑色前景
Definition colorful.h:43
static constexpr char White[]
白色前景
Definition colorful.h:57
static constexpr char Strikethrough[]
删除线
Definition colorful.h:128
static constexpr char BrightGreen[]
亮绿色前景
Definition colorful.h:63
static constexpr char Magenta[]
品红前景
Definition colorful.h:53
static constexpr char BrightWhite[]
亮白色前景
Definition colorful.h:73
static constexpr char BgBrightGreen[]
亮绿色背景
Definition colorful.h:99
static constexpr char BrightBlue[]
亮蓝色前景
Definition colorful.h:67
static constexpr char BgBlue[]
蓝色背景
Definition colorful.h:87
static constexpr char BgBrightMagenta[]
亮品红背景
Definition colorful.h:105
本库所有组件所在的顶层命名空间。
void cc(const char *clr=color::Reset, std::ostream &os=std::cout)
修改颜色。
Definition colorful.h:138