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

View File

@ -16,10 +16,6 @@
using namespace std; using namespace std;
TileGenerator::TileGenerator(): TileGenerator::TileGenerator():
m_bgColor("#ffffff"),
m_scaleColor("#000000"),
m_originColor("#ff0000"),
m_playerColor("#ff0000"),
m_drawOrigin(false), m_drawOrigin(false),
m_drawPlayers(false), m_drawPlayers(false),
m_drawScale(false), m_drawScale(false),
@ -31,6 +27,10 @@ TileGenerator::TileGenerator():
m_zMin(0), m_zMin(0),
m_zMax(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() TileGenerator::~TileGenerator()
@ -43,22 +43,40 @@ TileGenerator::~TileGenerator()
void TileGenerator::setBgColor(const std::string &bgColor) void TileGenerator::setBgColor(const std::string &bgColor)
{ {
m_bgColor = bgColor; m_bgColor = parseColor(bgColor);
} }
void TileGenerator::setScaleColor(const std::string &scaleColor) void TileGenerator::setScaleColor(const std::string &scaleColor)
{ {
m_scaleColor = scaleColor; m_scaleColor = parseColor(scaleColor);
} }
void TileGenerator::setOriginColor(const std::string &originColor) void TileGenerator::setOriginColor(const std::string &originColor)
{ {
m_originColor = originColor; m_originColor = parseColor(originColor);
} }
void TileGenerator::setPlayerColor(const std::string &playerColor) 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) void TileGenerator::setDrawOrigin(bool drawOrigin)
@ -194,7 +212,7 @@ void TileGenerator::createImage()
m_imgHeight = (m_zMax - m_zMin) * 16; m_imgHeight = (m_zMax - m_zMin) * 16;
m_image = gdImageCreate(m_imgWidth, m_imgHeight); m_image = gdImageCreate(m_imgWidth, m_imgHeight);
// Background // 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) void TileGenerator::writeImage(const std::string &output)

View File

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