Added color parser.

This commit is contained in:
Miroslav Bendík 2012-08-23 14:43:11 +02:00
parent 0d8358d4e3
commit b7f69cc3df
2 changed files with 35 additions and 13 deletions

@ -16,10 +16,6 @@
using namespace std;
TileGenerator::TileGenerator():
m_bgColor("#ffffff"),
m_scaleColor("#000000"),
m_originColor("#ff0000"),
m_playerColor("#ff0000"),
m_drawOrigin(false),
m_drawPlayers(false),
m_drawScale(false),
@ -31,6 +27,10 @@ TileGenerator::TileGenerator():
m_zMin(0),
m_zMax(0)
{
m_bgColor = {255, 255, 255};
m_scaleColor = {0, 0, 0};
m_originColor = {255, 0, 0};
m_playerColor = {255, 0, 0};
}
TileGenerator::~TileGenerator()
@ -43,22 +43,40 @@ TileGenerator::~TileGenerator()
void TileGenerator::setBgColor(const std::string &bgColor)
{
m_bgColor = bgColor;
m_bgColor = parseColor(bgColor);
}
void TileGenerator::setScaleColor(const std::string &scaleColor)
{
m_scaleColor = scaleColor;
m_scaleColor = parseColor(scaleColor);
}
void TileGenerator::setOriginColor(const std::string &originColor)
{
m_originColor = originColor;
m_originColor = parseColor(originColor);
}
void TileGenerator::setPlayerColor(const std::string &playerColor)
{
m_playerColor = playerColor;
m_playerColor = parseColor(playerColor);
}
Color TileGenerator::parseColor(const std::string &color)
{
Color parsed;
if (color.length() != 7) {
throw ColorError();
}
if (color[0] != '#') {
throw ColorError();
}
long col = strtol(color.c_str() + 1, NULL, 16);
parsed.b = col % 256;
col = col / 256;
parsed.g = col % 256;
col = col / 256;
parsed.r = col % 256;
return parsed;
}
void TileGenerator::setDrawOrigin(bool drawOrigin)
@ -194,7 +212,7 @@ void TileGenerator::createImage()
m_imgHeight = (m_zMax - m_zMin) * 16;
m_image = gdImageCreate(m_imgWidth, m_imgHeight);
// Background
gdImageColorAllocate(m_image, 255, 255, 255);
gdImageColorAllocate(m_image, m_bgColor.r, m_bgColor.g, m_bgColor.b);
}
void TileGenerator::writeImage(const std::string &output)

@ -31,6 +31,9 @@ struct BlockPos {
class DbError {
};
class ColorError {
};
class TileGenerator
{
private:
@ -43,6 +46,7 @@ public:
void setScaleColor(const std::string &scaleColor);
void setOriginColor(const std::string &originColor);
void setPlayerColor(const std::string &playerColor);
Color parseColor(const std::string &color);
void setDrawOrigin(bool drawOrigin);
void setDrawPlayers(bool drawPlayers);
void setDrawScale(bool drawScale);
@ -59,10 +63,10 @@ private:
void writeImage(const std::string &output);
private:
std::string m_bgColor;
std::string m_scaleColor;
std::string m_originColor;
std::string m_playerColor;
Color m_bgColor;
Color m_scaleColor;
Color m_originColor;
Color m_playerColor;
bool m_drawOrigin;
bool m_drawPlayers;
bool m_drawScale;