15,440
社区成员
发帖
与我相关
我的任务
分享
/****************************************************************************
文件名:colorprint.h
用处:对一个字符串着色
作者:杨志轩
时间:2010.2.11 晚上 20.57
用法请参见注释,祝大家新年快乐!
****************************************************************************/
#ifndef COLORPRINT_H
#define COLORPRINT_H
#include <string.h>
/*****************************************************************************
枚举类型color用来定义能够支持的各种颜色,一共有8种
****************************************************************************/
enum color{red,blue,green,black,yellow,white,darkgreen,purple};
/*****************************************************************************
*setcolor函数用来给一个字符串设定颜色
*第一个参数color用于定义颜色,是一个枚举类型color,可以选择的颜色一共有8种
*第二个参数des用来存放保存着色过的字符串
*第三个参数src源字符串的位置
*返回值是着色过的字符串的地址
*用法举例:printf("%s",setcolor(red,des,"Happy new year!"));
*****************************************************************************/
char* setcolor(enum color col,char* des,char* src)
{
char* tep=des;
strcpy(tep,"\033[0;");
tep+=strlen("\033[0;");
if(col==red){
strcpy(tep,"31m");
tep+=3;
}
else if(col==blue){
strcpy(tep,"34m");
tep+=3;
}
else if(col==green){
strcpy(tep,"32m");
tep+=3;
}
else if(col==black){
strcpy(tep,"30m");
tep+=3;
}
else if(col==yellow){
strcpy(tep,"33m");
tep+=3;
}
else if(col==white){
strcpy(tep,"37m");
tep+=3;
}
else if(col==darkgreen){
strcpy(tep,"36m");
tep+=3;
}
else if(col==purple){
strcpy(tep,"35m");
tep+=3;
}
else{
strcpy(tep,"0m");
tep+=2;
}
strcpy(tep,src);
tep+=strlen(src);
strcpy(tep,"\033[0m");
return des;
}
#endif