Convert PaletteUtil into a namespace
This commit is contained in:
parent
a629b07153
commit
bc2cc7d089
6 changed files with 64 additions and 76 deletions
|
@ -4,22 +4,10 @@
|
||||||
|
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QRgb>
|
#include <QRgb>
|
||||||
#include <QString>
|
|
||||||
|
|
||||||
class PaletteUtil
|
namespace PaletteUtil {
|
||||||
{
|
|
||||||
public:
|
|
||||||
PaletteUtil();
|
|
||||||
QList<QRgb> parse(QString filepath, bool *error);
|
QList<QRgb> parse(QString filepath, bool *error);
|
||||||
void writeJASC(QString filepath, QVector<QRgb> colors, int offset, int nColors);
|
void writeJASC(QString filepath, QVector<QRgb> colors, int offset, int nColors);
|
||||||
private:
|
}
|
||||||
QList<QRgb> parsePal(QString filepath, bool *error);
|
|
||||||
QList<QRgb> parseJASC(QString filepath, bool *error);
|
|
||||||
QList<QRgb> parseAdvanceMapPal(QString filepath, bool *error);
|
|
||||||
QList<QRgb> parseAdobeColorTable(QString filepath, bool *error);
|
|
||||||
QList<QRgb> parseTileLayerPro(QString filepath, bool *error);
|
|
||||||
QList<QRgb> parseAdvancePaletteEditor(QString filepath, bool *error);
|
|
||||||
int clampColorValue(int value);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // PALETTEUTIL_H
|
#endif // PALETTEUTIL_H
|
||||||
|
|
|
@ -2,11 +2,15 @@
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
PaletteUtil::PaletteUtil()
|
QList<QRgb> parsePal(QString filepath, bool *error);
|
||||||
{
|
QList<QRgb> parseJASC(QString filepath, bool *error);
|
||||||
|
QList<QRgb> parseAdvanceMapPal(QString filepath, bool *error);
|
||||||
}
|
QList<QRgb> parseAdobeColorTable(QString filepath, bool *error);
|
||||||
|
QList<QRgb> parseTileLayerPro(QString filepath, bool *error);
|
||||||
|
QList<QRgb> parseAdvancePaletteEditor(QString filepath, bool *error);
|
||||||
|
int clampColorValue(int value);
|
||||||
|
|
||||||
QList<QRgb> PaletteUtil::parse(QString filepath, bool *error) {
|
QList<QRgb> PaletteUtil::parse(QString filepath, bool *error) {
|
||||||
QFileInfo info(filepath);
|
QFileInfo info(filepath);
|
||||||
|
@ -34,7 +38,35 @@ QList<QRgb> PaletteUtil::parse(QString filepath, bool *error) {
|
||||||
return QList<QRgb>();
|
return QList<QRgb>();
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QRgb> PaletteUtil::parsePal(QString filepath, bool *error) {
|
void PaletteUtil::writeJASC(QString filepath, QVector<QRgb> palette, int offset, int nColors) {
|
||||||
|
if (!nColors) {
|
||||||
|
logWarn(QString("Cannot save a palette with no colors."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (offset > palette.size() || offset + nColors > palette.size()) {
|
||||||
|
logWarn("Palette offset out of range for color table.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString text = "JASC-PAL\r\n0100\r\n";
|
||||||
|
text += QString::number(nColors) + "\r\n";
|
||||||
|
|
||||||
|
for (int i = offset; i < offset + nColors; i++) {
|
||||||
|
QRgb color = palette.at(i);
|
||||||
|
text += QString::number(qRed(color)) + " "
|
||||||
|
+ QString::number(qGreen(color)) + " "
|
||||||
|
+ QString::number(qBlue(color)) + "\r\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
QFile file(filepath);
|
||||||
|
if (file.open(QIODevice::WriteOnly)) {
|
||||||
|
file.write(text.toUtf8());
|
||||||
|
} else {
|
||||||
|
logWarn(QString("Could not write to file '%1': ").arg(filepath) + file.errorString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<QRgb> parsePal(QString filepath, bool *error) {
|
||||||
QFile file(filepath);
|
QFile file(filepath);
|
||||||
if (!file.open(QIODevice::ReadOnly)) {
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
*error = true;
|
*error = true;
|
||||||
|
@ -53,7 +85,7 @@ QList<QRgb> PaletteUtil::parsePal(QString filepath, bool *error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QRgb> PaletteUtil::parseJASC(QString filepath, bool *error) {
|
QList<QRgb> parseJASC(QString filepath, bool *error) {
|
||||||
QFile file(filepath);
|
QFile file(filepath);
|
||||||
if (!file.open(QIODevice::ReadOnly)) {
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
*error = true;
|
*error = true;
|
||||||
|
@ -105,9 +137,9 @@ QList<QRgb> PaletteUtil::parseJASC(QString filepath, bool *error) {
|
||||||
return QList<QRgb>();
|
return QList<QRgb>();
|
||||||
}
|
}
|
||||||
|
|
||||||
palette.append(qRgb(this->clampColorValue(red),
|
palette.append(qRgb(clampColorValue(red),
|
||||||
this->clampColorValue(green),
|
clampColorValue(green),
|
||||||
this->clampColorValue(blue)));
|
clampColorValue(blue)));
|
||||||
} else {
|
} else {
|
||||||
*error = true;
|
*error = true;
|
||||||
logError(QString("JASC palette file '%1' had an unexpected format. Invalid color '%2'.").arg(filepath).arg(line));
|
logError(QString("JASC palette file '%1' had an unexpected format. Invalid color '%2'.").arg(filepath).arg(line));
|
||||||
|
@ -120,7 +152,7 @@ QList<QRgb> PaletteUtil::parseJASC(QString filepath, bool *error) {
|
||||||
return palette;
|
return palette;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QRgb> PaletteUtil::parseAdvanceMapPal(QString filepath, bool *error) {
|
QList<QRgb> parseAdvanceMapPal(QString filepath, bool *error) {
|
||||||
QFile file(filepath);
|
QFile file(filepath);
|
||||||
if (!file.open(QIODevice::ReadOnly)) {
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
*error = true;
|
*error = true;
|
||||||
|
@ -143,16 +175,16 @@ QList<QRgb> PaletteUtil::parseAdvanceMapPal(QString filepath, bool *error) {
|
||||||
unsigned char red = static_cast<unsigned char>(in.at(i));
|
unsigned char red = static_cast<unsigned char>(in.at(i));
|
||||||
unsigned char green = static_cast<unsigned char>(in.at(i + 1));
|
unsigned char green = static_cast<unsigned char>(in.at(i + 1));
|
||||||
unsigned char blue = static_cast<unsigned char>(in.at(i + 2));
|
unsigned char blue = static_cast<unsigned char>(in.at(i + 2));
|
||||||
palette.append(qRgb(this->clampColorValue(red),
|
palette.append(qRgb(clampColorValue(red),
|
||||||
this->clampColorValue(green),
|
clampColorValue(green),
|
||||||
this->clampColorValue(blue)));
|
clampColorValue(blue)));
|
||||||
i += 4;
|
i += 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
return palette;
|
return palette;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QRgb> PaletteUtil::parseAdobeColorTable(QString filepath, bool *error) {
|
QList<QRgb> parseAdobeColorTable(QString filepath, bool *error) {
|
||||||
QFile file(filepath);
|
QFile file(filepath);
|
||||||
if (!file.open(QIODevice::ReadOnly)) {
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
*error = true;
|
*error = true;
|
||||||
|
@ -175,16 +207,16 @@ QList<QRgb> PaletteUtil::parseAdobeColorTable(QString filepath, bool *error) {
|
||||||
unsigned char red = static_cast<unsigned char>(in.at(i));
|
unsigned char red = static_cast<unsigned char>(in.at(i));
|
||||||
unsigned char green = static_cast<unsigned char>(in.at(i + 1));
|
unsigned char green = static_cast<unsigned char>(in.at(i + 1));
|
||||||
unsigned char blue = static_cast<unsigned char>(in.at(i + 2));
|
unsigned char blue = static_cast<unsigned char>(in.at(i + 2));
|
||||||
palette.append(qRgb(this->clampColorValue(red),
|
palette.append(qRgb(clampColorValue(red),
|
||||||
this->clampColorValue(green),
|
clampColorValue(green),
|
||||||
this->clampColorValue(blue)));
|
clampColorValue(blue)));
|
||||||
i += 3;
|
i += 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
return palette;
|
return palette;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QRgb> PaletteUtil::parseTileLayerPro(QString filepath, bool *error) {
|
QList<QRgb> parseTileLayerPro(QString filepath, bool *error) {
|
||||||
QFile file(filepath);
|
QFile file(filepath);
|
||||||
if (!file.open(QIODevice::ReadOnly)) {
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
*error = true;
|
*error = true;
|
||||||
|
@ -213,16 +245,16 @@ QList<QRgb> PaletteUtil::parseTileLayerPro(QString filepath, bool *error) {
|
||||||
unsigned char red = static_cast<unsigned char>(in.at(i));
|
unsigned char red = static_cast<unsigned char>(in.at(i));
|
||||||
unsigned char green = static_cast<unsigned char>(in.at(i + 1));
|
unsigned char green = static_cast<unsigned char>(in.at(i + 1));
|
||||||
unsigned char blue = static_cast<unsigned char>(in.at(i + 2));
|
unsigned char blue = static_cast<unsigned char>(in.at(i + 2));
|
||||||
palette.append(qRgb(this->clampColorValue(red),
|
palette.append(qRgb(clampColorValue(red),
|
||||||
this->clampColorValue(green),
|
clampColorValue(green),
|
||||||
this->clampColorValue(blue)));
|
clampColorValue(blue)));
|
||||||
i += 3;
|
i += 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
return palette;
|
return palette;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QRgb> PaletteUtil::parseAdvancePaletteEditor(QString filepath, bool *error) {
|
QList<QRgb> parseAdvancePaletteEditor(QString filepath, bool *error) {
|
||||||
QFile file(filepath);
|
QFile file(filepath);
|
||||||
if (!file.open(QIODevice::ReadOnly)) {
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
*error = true;
|
*error = true;
|
||||||
|
@ -258,44 +290,16 @@ QList<QRgb> PaletteUtil::parseAdvancePaletteEditor(QString filepath, bool *error
|
||||||
int red = (raw & 0x1F) * 8;
|
int red = (raw & 0x1F) * 8;
|
||||||
int green = ((raw >> 5) & 0x1F) * 8;
|
int green = ((raw >> 5) & 0x1F) * 8;
|
||||||
int blue = ((raw >> 10) & 0x1F) * 8;
|
int blue = ((raw >> 10) & 0x1F) * 8;
|
||||||
palette.append(qRgb(this->clampColorValue(red),
|
palette.append(qRgb(clampColorValue(red),
|
||||||
this->clampColorValue(green),
|
clampColorValue(green),
|
||||||
this->clampColorValue(blue)));
|
clampColorValue(blue)));
|
||||||
}
|
}
|
||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
return palette;
|
return palette;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaletteUtil::writeJASC(QString filepath, QVector<QRgb> palette, int offset, int nColors) {
|
int clampColorValue(int value) {
|
||||||
if (!nColors) {
|
|
||||||
logWarn(QString("Cannot save a palette with no colors."));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (offset > palette.size() || offset + nColors > palette.size()) {
|
|
||||||
logWarn("Palette offset out of range for color table.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString text = "JASC-PAL\r\n0100\r\n";
|
|
||||||
text += QString::number(nColors) + "\r\n";
|
|
||||||
|
|
||||||
for (int i = offset; i < offset + nColors; i++) {
|
|
||||||
QRgb color = palette.at(i);
|
|
||||||
text += QString::number(qRed(color)) + " "
|
|
||||||
+ QString::number(qGreen(color)) + " "
|
|
||||||
+ QString::number(qBlue(color)) + "\r\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
QFile file(filepath);
|
|
||||||
if (file.open(QIODevice::WriteOnly)) {
|
|
||||||
file.write(text.toUtf8());
|
|
||||||
} else {
|
|
||||||
logWarn(QString("Could not write to file '%1': ").arg(filepath) + file.errorString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int PaletteUtil::clampColorValue(int value) {
|
|
||||||
if (value < 0) {
|
if (value < 0) {
|
||||||
value = 0;
|
value = 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,8 +63,7 @@ void RegionMap::saveTileImages() {
|
||||||
this->region_map_png_path = project->root + "/graphics/pokenav/region_map.png";
|
this->region_map_png_path = project->root + "/graphics/pokenav/region_map.png";
|
||||||
pngImage.save(pngPath());
|
pngImage.save(pngPath());
|
||||||
|
|
||||||
PaletteUtil parser;
|
PaletteUtil::writeJASC(project->root + "/graphics/pokenav/region_map.pal", pngImage.colorTable(), 0x70, 0x20);
|
||||||
parser.writeJASC(project->root + "/graphics/pokenav/region_map.pal", pngImage.colorTable(), 0x70, 0x20);
|
|
||||||
}
|
}
|
||||||
region_map_png_needs_saving = false;
|
region_map_png_needs_saving = false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1069,10 +1069,9 @@ void Project::saveTilesetTilesImage(Tileset *tileset) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Project::saveTilesetPalettes(Tileset *tileset) {
|
void Project::saveTilesetPalettes(Tileset *tileset) {
|
||||||
PaletteUtil paletteParser;
|
|
||||||
for (int i = 0; i < Project::getNumPalettesTotal(); i++) {
|
for (int i = 0; i < Project::getNumPalettesTotal(); i++) {
|
||||||
QString filepath = tileset->palettePaths.at(i);
|
QString filepath = tileset->palettePaths.at(i);
|
||||||
paletteParser.writeJASC(filepath, tileset->palettes.at(i).toVector(), 0, 16);
|
PaletteUtil::writeJASC(filepath, tileset->palettes.at(i).toVector(), 0, 16);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -279,9 +279,8 @@ void PaletteEditor::on_actionImport_Palette_triggered()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
PaletteUtil parser;
|
|
||||||
bool error = false;
|
bool error = false;
|
||||||
QList<QRgb> palette = parser.parse(filepath, &error);
|
QList<QRgb> palette = PaletteUtil::parse(filepath, &error);
|
||||||
if (error) {
|
if (error) {
|
||||||
QMessageBox msgBox(this);
|
QMessageBox msgBox(this);
|
||||||
msgBox.setText("Failed to import palette.");
|
msgBox.setText("Failed to import palette.");
|
||||||
|
|
|
@ -626,9 +626,8 @@ void TilesetEditor::importTilesetTiles(Tileset *tileset, bool primary) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
PaletteUtil parser;
|
|
||||||
bool error = false;
|
bool error = false;
|
||||||
QList<QRgb> palette = parser.parse(filepath, &error);
|
QList<QRgb> palette = PaletteUtil::parse(filepath, &error);
|
||||||
if (error) {
|
if (error) {
|
||||||
QMessageBox msgBox(this);
|
QMessageBox msgBox(this);
|
||||||
msgBox.setText("Failed to import palette.");
|
msgBox.setText("Failed to import palette.");
|
||||||
|
|
Loading…
Reference in a new issue