-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxpmgenerator.cpp
59 lines (50 loc) · 1.5 KB
/
xpmgenerator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "xpmgenerator.h"
#include <cstdio>
XPMGenerator::XPMGenerator()
:ColorGenerator(){
}
XPMGenerator::~XPMGenerator(){
}
void XPMGenerator::output(int x,int y,ColorRaw**bitmap){
if(!bitmap)return;
colorPalette.resize(0);
for(int j=0;j<y;j++)
for(int i=0;i<x;i++)
add(getCode(bitmap[i][j]));
printf("/* XPM */\n");
printf("static char *egc[] = {\n");
printf("/* width,height,nrcolors,charsperpixel */\n");
printf("\" %i %i %i 7 \"\n",x,y,colorPalette.size());
printf("/* colors #RRGGBB */\n");
for(int i=0;i<colorPalette.size();i++)
printf("\"%s c %s\",",colorPalette[i].c_str(),colorPalette[i].c_str());
printf("\n");
for(int j=0;j<y;j++){
printf(",\n\"");
for(int i=0;i<x;i++){
printf("%s",getCode(bitmap[i][j]).c_str());
}
printf("\"");
}
printf("};\n");
}
std::string XPMGenerator::getCode(ColorRaw c){
std::string hexcolor="#000000";
hexcolor[1]=hexDigit(c.r/16);
hexcolor[2]=hexDigit(c.r%16);
hexcolor[3]=hexDigit(c.g/16);
hexcolor[4]=hexDigit(c.g%16);
hexcolor[5]=hexDigit(c.b/16);
hexcolor[6]=hexDigit(c.b%16);
return hexcolor;
}
char XPMGenerator::hexDigit(int x){
if(x<0||x>15)return '!';//this is bad, this is error
if(x>=0&&x<=9)return x+'0';
return x-10+'A';
}
void XPMGenerator::add(std::string color){
for(int i=0;i<colorPalette.size();i++)
if(!color.compare(colorPalette[i]))return;
colorPalette.push_back(color);
}