表格线识别通用库文档
载入中...
搜索中...
未找到
color.hpp
浏览该文件的文档.
1/*
2 * @Description: 颜色类 头文件及其实现
3 * @Version:
4 * @Autor: justliulong
5 * @date: 2023-12-06
6 * @LastEditors: dreamy-xay
7 * @LastEditTime: 2024-03-04
8 */
9#ifndef COMMON_COLOR_HPP
10#define COMMON_COLOR_HPP
11
12#include <string>
13
14#include "common/enum.h"
15#include "common/macro.h"
16#include "common/type.h"
17
18namespace cm {
19
27class Color {
28 public:
29
30 static std::string BLACK Cm_WEAK_ATTRIBUTE ; // 黑色
31 static std::string RED Cm_WEAK_ATTRIBUTE ; // 红色
32 static std::string GREEN Cm_WEAK_ATTRIBUTE ; // 绿色
33 static std::string YELLOW Cm_WEAK_ATTRIBUTE ; // 黄色
34 static std::string BLUE Cm_WEAK_ATTRIBUTE ; // 蓝色
35 static std::string MAGENTA Cm_WEAK_ATTRIBUTE ; // 洋红色
36 static std::string CYAN Cm_WEAK_ATTRIBUTE ; // 青色
37 static std::string WHITE Cm_WEAK_ATTRIBUTE ; // 白色
38
39 static std::string BLACK_BG Cm_WEAK_ATTRIBUTE ; // 黑色背景
40 static std::string RED_BG Cm_WEAK_ATTRIBUTE ; // 红色背景
41 static std::string GREEN_BG Cm_WEAK_ATTRIBUTE ; // 绿色背景
42 static std::string YELLOW_BG Cm_WEAK_ATTRIBUTE ; // 黄色背景
43 static std::string BLUE_BG Cm_WEAK_ATTRIBUTE ; // 蓝色背景
44 static std::string MAGENTA_BG Cm_WEAK_ATTRIBUTE ; // 洋红色背景
45 static std::string CYAN_BG Cm_WEAK_ATTRIBUTE ; // 青色背景
46 static std::string WHITE_BG Cm_WEAK_ATTRIBUTE ; // 白色背景
47 static std::string FONT_BOLD Cm_WEAK_ATTRIBUTE ; // 加粗
48 static std::string RESET Cm_WEAK_ATTRIBUTE ; // 重置所有颜色设置
49
50 // 可变参数模板实现
51 template <typename... Args>
52 static std::string SetColor(const std::string& string, Args... colors);
53
54 static void CancelColor();
55
56 static void RestoreColor();
57};
58
59//* 字体颜色
61std::string Color::BLACK = "\033[30m";
63std::string Color::RED = "\033[31m";
65std::string Color::GREEN = "\033[32m";
67std::string Color::YELLOW = "\033[33m";
69std::string Color::BLUE = "\033[34m";
71std::string Color::MAGENTA = "\033[35m";
73std::string Color::CYAN = "\033[36m";
75std::string Color::WHITE = "\033[37m";
76
77//* 背景颜色
79std::string Color::BLACK_BG = "\033[40m";
81std::string Color::RED_BG = "\033[41m";
83std::string Color::GREEN_BG = "\033[42m";
85std::string Color::YELLOW_BG = "\033[43m";
87std::string Color::BLUE_BG = "\033[44m";
89std::string Color::MAGENTA_BG = "\033[45m";
91std::string Color::CYAN_BG = "\033[46m";
93std::string Color::WHITE_BG = "\033[47m";
94
96std::string Color::FONT_BOLD = "\033[1m";
97
99std::string Color::RESET = "\033[0m";
100
116template <typename... Args>
117inline std::string Color::SetColor(const std::string& string, Args... colors) {
118 std::string color_sequence = "";
119 for (auto color : std::initializer_list<std::string>{colors...})
121 return color_sequence + string + RESET;
122}
123
133inline void Color::CancelColor() {
134 BLACK = RED = GREEN = YELLOW = BLUE = MAGENTA = CYAN = WHITE = BLACK_BG = RED_BG = GREEN_BG = YELLOW_BG = BLUE_BG = MAGENTA_BG = CYAN_BG = WHITE_BG = FONT_BOLD = RESET = ""; // 重置所有颜色设置
135}
136
144inline void Color::RestoreColor() {
145 // 字体颜色
146 BLACK = "\033[30m"; // 黑色
147 RED = "\033[31m"; // 红色
148 GREEN = "\033[32m"; // 绿色
149 YELLOW = "\033[33m"; // 黄色
150 BLUE = "\033[34m"; // 蓝色
151 MAGENTA = "\033[35m"; // 洋红色
152 CYAN = "\033[36m"; // 青色
153 WHITE = "\033[37m"; // 白色
154
155 // 背景颜色
156 BLACK_BG = "\033[40m"; // 黑色背景
157 RED_BG = "\033[41m"; // 红色背景
158 GREEN_BG = "\033[42m"; // 绿色背景
159 YELLOW_BG = "\033[43m"; // 黄色背景
160 BLUE_BG = "\033[44m"; // 蓝色背景
161 MAGENTA_BG = "\033[45m"; // 洋红色背景
162 CYAN_BG = "\033[46m"; // 青色背景
163 WHITE_BG = "\033[47m"; // 白色背景
164
165 // 加粗
166 FONT_BOLD = "\033[1m"; // 加粗
167
168 // 结束符
169 RESET = "\033[0m"; // 重置所有颜色设置
170}
171
172} // namespace cm
173
174#endif
颜色类
Definition color.hpp:27
static std::string YELLOW_BG
黄色背景
Definition color.hpp:42
static std::string BLACK
黑色
Definition color.hpp:30
static void RestoreColor()
恢复默认颜色设置
Definition color.hpp:144
static std::string BLUE_BG
蓝色背景
Definition color.hpp:43
static std::string RESET
结束符,重置所有颜色设置
Definition color.hpp:48
static std::string GREEN_BG
绿色背景
Definition color.hpp:41
static std::string BLACK_BG
黑色背景
Definition color.hpp:39
static std::string WHITE
白色
Definition color.hpp:37
static std::string BLUE
蓝色
Definition color.hpp:34
static std::string MAGENTA
洋红色
Definition color.hpp:35
static std::string MAGENTA_BG
洋红色背景
Definition color.hpp:44
static std::string CYAN
青色
Definition color.hpp:36
static std::string WHITE_BG
白色背景
Definition color.hpp:46
static std::string FONT_BOLD
加粗
Definition color.hpp:47
static std::string RED_BG
红色背景
Definition color.hpp:40
static void CancelColor()
取消所有颜色
Definition color.hpp:133
static std::string GREEN
绿色
Definition color.hpp:32
static std::string RED
红色
Definition color.hpp:31
static std::string SetColor(const std::string &string, Args... colors)
对传入的字符串设置颜色
Definition color.hpp:117
static std::string YELLOW
黄色
Definition color.hpp:33
static std::string CYAN_BG
青色背景
Definition color.hpp:45
点类
Definition point.hpp:52
#define Cm_WEAK_ATTRIBUTE
弱符号定义宏
Definition macro.h:82