cpp==============================
CColorList::CColorList()
{
colorStorage.clear();
loadColors();
}
CColorList::~CColorList()
{
colorStorage.clear();
}
void CColorList::loadColors(void)
{
addColor("default",255,255,255,255);
addColor("esp_ct",40,80,255,255);
addColor("esp_t",205,100,60,255);
addColor("cross",255,255,255,255);
addColor("con_back",0,0,0,90);
addColor("con_border",255,255,255,150);
addColor("con_text",255,255,255,255);
addColor("nicered",255,40,40,255);
addColor("rand1");
addColor("rand2");
addColor("rand3");
addColor("rand4");
}
void CColorList::addColor(char *szName)
{
for(int i = 0; i < (int)colorStorage.size(); i++)
if(!strcmp(colorStorage.szName,szName))
return;
static srand(GetTickCount());
int iRed = rand() % 0xFF;
int iGreen = rand() % 0xFF;
int iBlue = rand() % 0xFF;
int iAlpha = rand() % 0xFF;
colorEntry tmpColor =
{
szName,
iRed,
iGreen,
iBlue,
iAlpha,
iRed / 255.0f,
iGreen / 255.0f,
iBlue / 255.0f,
iAlpha / 255.0f
};
colorStorage.push_back(tmpColor);
}
void CColorList::addColor(char *szName, int iRed, int iGreen, int iBlue, int iAlpha)
{
for(int i = 0; i < (int)colorStorage.size(); i++)
if(!strcmp(colorStorage.szName,szName))
return;
colorEntry tmpColor =
{
szName,
iRed,
iGreen,
iBlue,
iAlpha,
iRed / 255.0f,
iGreen / 255.0f,
iBlue / 255.0f,
iAlpha / 255.0f
};
colorStorage.push_back(tmpColor);
}
void CColorList::randomizeColor(char *szName)
{
for(int i = 0; i < (int)colorStorage.size(); i++)
{
if(!strcmp(colorStorage.szName,szName))
{
static srand(GetTickCount());
colorStorage.iRed = rand() % 0xFF;
colorStorage.iGreen = rand() % 0xFF;
colorStorage.iBlue = rand() % 0xFF;
colorStorage.iAlpha = rand() % 0xFF;
colorStorage.fRed = colorStorage.iRed / 255.0f;
colorStorage.fGreen = colorStorage.iGreen / 255.0f;
colorStorage.fBlue = colorStorage.iBlue / 255.0f;
colorStorage.fAlpha = colorStorage.iAlpha / 255.0f;
}
else return;
}
}
void CColorList::toGreyscale(char *szName)
{
for(int i = 0; i < (int)colorStorage.size(); i++)
{
if(!strcmp(colorStorage.szName,szName))
{
int theAverage = (colorStorage.iRed + colorStorage.iGreen + colorStorage.iBlue) / 3;
colorStorage.iRed = theAverage;
colorStorage.iGreen = theAverage;
colorStorage.iBlue = theAverage;
colorStorage.fRed = theAverage / 255.0f;
colorStorage.fGreen = theAverage / 255.0f;
colorStorage.fBlue = theAverage / 255.0f;
}
}
}
void CColorList::saveColors(char *szFile)
{
for(int i = 0; i < (int)colorStorage.size(); i++)
{
char buf[256];
sprintf(buf,"%s %i %i %i %.2f %.2f %.2f\n",
colorStorage.szName,
colorStorage.iRed,
colorStorage.iGreen,
colorStorage.iBlue,
colorStorage.fRed,
colorStorage.fGreen,
colorStorage.fBlue);
FILE *colorFile = fopen(szFile,"a+");
if(colorFile != NULL)
{
fputs(buf,colorFile);
fclose(colorFile);
}
}
}
colorEntry *CColorList::getColorByName(char *szName)
{
for(int i = 0; i < (int)colorStorage.size(); i++)
{
if(!strcmp(colorStorage.szName,szName))
return &colorStorage;
}
return &colorStorage[0];
}
colorEntry *CColorList::getColorByIndex(int colorIndex)
{
if(colorIndex > (int)colorStorage.size() || colorIndex < 0) return &colorStorage[0];
return &colorStorage[colorIndex];
}
colorEntry *CColorList::getRandomColor(void)
{
static srand(GetTickCount());
int randIndex = rand() % (int)colorStorage.size();
return &colorStorage[randIndex];
}
========================================================
.h=================================================================
typedef struct
{
char *szName;
int iRed;
int iGreen;
int iBlue;
int iAlpha;
float fRed;
float fGreen;
float fBlue;
float fAlpha;
} colorEntry;
class CColorList
{
public:
CColorList::CColorList(void);
CColorList::~CColorList(void);
void loadColors(void);
void addColor(char *szName);
void addColor(char *szName, int colorRed, int colorGreen, int colorBlue, int colorAlpha);
void randomizeColor(char *szName);
void toGreyscale(char *szName);
void saveColors(char *szFile);
colorEntry *getColorByName(char *szName);
colorEntry *getColorByIndex(int colorIndex);
colorEntry *getRandomColor(void);
private:
std::vector<colorEntry> colorStorage;
};
extern CColorList colorManager;