如何将彩色字符打印到支持彩色字符的Linux终端?
如何判断终端是否支持颜色码?
如何将彩色字符打印到支持彩色字符的Linux终端?
如何判断终端是否支持颜色码?
当前回答
你可以使用ANSI颜色代码。
使用这些函数。
enum c_color{BLACK=30,RED=31,GREEN=32,YELLOW=33,BLUE=34,MAGENTA=35,CYAN=36,WHITE=37};
enum c_decoration{NORMAL=0,BOLD=1,FAINT=2,ITALIC=3,UNDERLINE=4,RIVERCED=26,FRAMED=51};
void pr(const string str,c_color color,c_decoration decoration=c_decoration::NORMAL){
cout<<"\033["<<decoration<<";"<<color<<"m"<<str<<"\033[0m";
}
void prl(const string str,c_color color,c_decoration decoration=c_decoration::NORMAL){
cout<<"\033["<<decoration<<";"<<color<<"m"<<str<<"\033[0m"<<endl;
}
其他回答
基础知识
我写了一个c++类,可以用来设置输出的前景色和背景色。这个示例程序用作打印This ->单词<-是红色的示例。格式化它,使单词的前景色为红色。
#include "colormod.h" // namespace Color
#include <iostream>
using namespace std;
int main() {
Color::Modifier red(Color::FG_RED);
Color::Modifier def(Color::FG_DEFAULT);
cout << "This ->" << red << "word" << def << "<- is red." << endl;
}
源
#include <ostream>
namespace Color {
enum Code {
FG_RED = 31,
FG_GREEN = 32,
FG_BLUE = 34,
FG_DEFAULT = 39,
BG_RED = 41,
BG_GREEN = 42,
BG_BLUE = 44,
BG_DEFAULT = 49
};
class Modifier {
Code code;
public:
Modifier(Code pCode) : code(pCode) {}
friend std::ostream&
operator<<(std::ostream& os, const Modifier& mod) {
return os << "\033[" << mod.code << "m";
}
};
}
先进的
您可能希望向该类添加其他特性。它是,例如,可以添加颜色洋红色,甚至样式像粗体。要做到这一点,只需在Code枚举中添加另一个条目。这是一个很好的参考。
从我的理解,一个典型的ANSI颜色代码
"\033[{FORMAT_ATTRIBUTE};{FORGROUND_COLOR};{BACKGROUND_COLOR}m{TEXT}\033[{RESET_FORMATE_ATTRIBUTE}m"
由(名称和编解码器)组成
FORMAT ATTRIBUTE { "Default", "0" }, { "Bold", "1" }, { "Dim", "2" }, { "Italics", "3"}, { "Underlined", "4" }, { "Blink", "5" }, { "Reverse", "7" }, { "Hidden", "8" } FORGROUND COLOR { "Default", "39" }, { "Black", "30" }, { "Red", "31" }, { "Green", "32" }, { "Yellow", "33" }, { "Blue", "34" }, { "Magenta", "35" }, { "Cyan", "36" }, { "Light Gray", "37" }, { "Dark Gray", "90" }, { "Light Red", "91" }, { "Light Green", "92" }, { "Light Yellow", "93" }, { "Light Blue", "94" }, { "Light Magenta", "95" }, { "Light Cyan", "96" }, { "White", "97" } BACKGROUND COLOR { "Default", "49" }, { "Black", "40" }, { "Red", "41" }, { "Green", "42" }, { "Yellow", "43" }, { "Blue", "44" }, { "Megenta", "45" }, { "Cyan", "46" }, { "Light Gray", "47" }, { "Dark Gray", "100" }, { "Light Red", "101" }, { "Light Green", "102" }, { "Light Yellow", "103" }, { "Light Blue", "104" }, { "Light Magenta", "105" }, { "Light Cyan", "106" }, { "White", "107" } TEXT RESET FORMAT ATTRIBUTE { "All", "0" }, { "Bold", "21" }, { "Dim", "22" }, { "Underlined", "24" }, { "Blink", "25" }, { "Reverse", "27" }, { "Hidden", "28" }
有了这些信息,就很容易给字符串“I am a banana!”上色,前景色为“黄色”,背景色为“绿色”,就像这样
"\033[0;33;42mI am a Banana!\033[0m"
或者用c++库着色
auto const& colorized_text = color::rize( "I am a banana!", "Yellow", "Green" );
std::cout << colorized_text << std::endl;
更多关于FORMAT ATTRIBUTE的例子
你可以使用ANSI颜色代码。
使用这些函数。
enum c_color{BLACK=30,RED=31,GREEN=32,YELLOW=33,BLUE=34,MAGENTA=35,CYAN=36,WHITE=37};
enum c_decoration{NORMAL=0,BOLD=1,FAINT=2,ITALIC=3,UNDERLINE=4,RIVERCED=26,FRAMED=51};
void pr(const string str,c_color color,c_decoration decoration=c_decoration::NORMAL){
cout<<"\033["<<decoration<<";"<<color<<"m"<<str<<"\033[0m";
}
void prl(const string str,c_color color,c_decoration decoration=c_decoration::NORMAL){
cout<<"\033["<<decoration<<";"<<color<<"m"<<str<<"\033[0m"<<endl;
}
在OSX shell上,这为我工作(包括“红色文本”前面的2个空格):
$ printf "\e[033;31m red text\n"
$ echo "$(tput setaf 1) red text"
试试我的标题这里一个快速和简单的方法来着色文本:Aedi的颜色标题
Escape-Sequence-Color-Header
在Unix中使用c++为输出着色!!
文本属性选项:
ATTRIBUTES_OFF, BOLD, UNDERSCORE, BLINK, REVERSE_VIDEO, CONCEALED
颜色选择:
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE
格式:
通用格式,在$变量$中包含你想要的值
COLOR_$Foreground_Color$_$Background_Color$
COLOR_$Text_Attribute$_$Foreground_Color$_$Background_Color$
COLOR_NORMAL // To set color to default
e.g.
COLOR_BLUE_BLACK // Leave Text Attribute Blank if no Text Attribute appied
COLOR_UNDERSCORE_YELLOW_RED
COLOR_NORMAL
用法:
只是使用流的颜色,你想在输出文本和 在输出文本后再次使用以将颜色设置为正常。
cout << COLOR_BLUE_BLACK << "TEXT" << COLOR_NORMAL << endl;
cout << COLOR_BOLD_YELLOW_CYAN << "TEXT" << COLOR_NORMAL << endl;