diff --git a/include/core/paletteutil.h b/include/core/paletteutil.h index abc1700a..ce221026 100644 --- a/include/core/paletteutil.h +++ b/include/core/paletteutil.h @@ -4,22 +4,10 @@ #include #include -#include -class PaletteUtil -{ -public: - PaletteUtil(); +namespace PaletteUtil { QList parse(QString filepath, bool *error); void writeJASC(QString filepath, QVector colors, int offset, int nColors); -private: - QList parsePal(QString filepath, bool *error); - QList parseJASC(QString filepath, bool *error); - QList parseAdvanceMapPal(QString filepath, bool *error); - QList parseAdobeColorTable(QString filepath, bool *error); - QList parseTileLayerPro(QString filepath, bool *error); - QList parseAdvancePaletteEditor(QString filepath, bool *error); - int clampColorValue(int value); -}; +} #endif // PALETTEUTIL_H diff --git a/src/core/paletteutil.cpp b/src/core/paletteutil.cpp index 3a6f7ff7..4bc7eab3 100644 --- a/src/core/paletteutil.cpp +++ b/src/core/paletteutil.cpp @@ -2,11 +2,15 @@ #include "log.h" #include #include +#include -PaletteUtil::PaletteUtil() -{ - -} +QList parsePal(QString filepath, bool *error); +QList parseJASC(QString filepath, bool *error); +QList parseAdvanceMapPal(QString filepath, bool *error); +QList parseAdobeColorTable(QString filepath, bool *error); +QList parseTileLayerPro(QString filepath, bool *error); +QList parseAdvancePaletteEditor(QString filepath, bool *error); +int clampColorValue(int value); QList PaletteUtil::parse(QString filepath, bool *error) { QFileInfo info(filepath); @@ -34,7 +38,35 @@ QList PaletteUtil::parse(QString filepath, bool *error) { return QList(); } -QList PaletteUtil::parsePal(QString filepath, bool *error) { +void PaletteUtil::writeJASC(QString filepath, QVector 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 parsePal(QString filepath, bool *error) { QFile file(filepath); if (!file.open(QIODevice::ReadOnly)) { *error = true; @@ -53,7 +85,7 @@ QList PaletteUtil::parsePal(QString filepath, bool *error) { } } -QList PaletteUtil::parseJASC(QString filepath, bool *error) { +QList parseJASC(QString filepath, bool *error) { QFile file(filepath); if (!file.open(QIODevice::ReadOnly)) { *error = true; @@ -105,9 +137,9 @@ QList PaletteUtil::parseJASC(QString filepath, bool *error) { return QList(); } - palette.append(qRgb(this->clampColorValue(red), - this->clampColorValue(green), - this->clampColorValue(blue))); + palette.append(qRgb(clampColorValue(red), + clampColorValue(green), + clampColorValue(blue))); } else { *error = true; logError(QString("JASC palette file '%1' had an unexpected format. Invalid color '%2'.").arg(filepath).arg(line)); @@ -120,7 +152,7 @@ QList PaletteUtil::parseJASC(QString filepath, bool *error) { return palette; } -QList PaletteUtil::parseAdvanceMapPal(QString filepath, bool *error) { +QList parseAdvanceMapPal(QString filepath, bool *error) { QFile file(filepath); if (!file.open(QIODevice::ReadOnly)) { *error = true; @@ -143,16 +175,16 @@ QList PaletteUtil::parseAdvanceMapPal(QString filepath, bool *error) { unsigned char red = static_cast(in.at(i)); unsigned char green = static_cast(in.at(i + 1)); unsigned char blue = static_cast(in.at(i + 2)); - palette.append(qRgb(this->clampColorValue(red), - this->clampColorValue(green), - this->clampColorValue(blue))); + palette.append(qRgb(clampColorValue(red), + clampColorValue(green), + clampColorValue(blue))); i += 4; } return palette; } -QList PaletteUtil::parseAdobeColorTable(QString filepath, bool *error) { +QList parseAdobeColorTable(QString filepath, bool *error) { QFile file(filepath); if (!file.open(QIODevice::ReadOnly)) { *error = true; @@ -175,16 +207,16 @@ QList PaletteUtil::parseAdobeColorTable(QString filepath, bool *error) { unsigned char red = static_cast(in.at(i)); unsigned char green = static_cast(in.at(i + 1)); unsigned char blue = static_cast(in.at(i + 2)); - palette.append(qRgb(this->clampColorValue(red), - this->clampColorValue(green), - this->clampColorValue(blue))); + palette.append(qRgb(clampColorValue(red), + clampColorValue(green), + clampColorValue(blue))); i += 3; } return palette; } -QList PaletteUtil::parseTileLayerPro(QString filepath, bool *error) { +QList parseTileLayerPro(QString filepath, bool *error) { QFile file(filepath); if (!file.open(QIODevice::ReadOnly)) { *error = true; @@ -213,16 +245,16 @@ QList PaletteUtil::parseTileLayerPro(QString filepath, bool *error) { unsigned char red = static_cast(in.at(i)); unsigned char green = static_cast(in.at(i + 1)); unsigned char blue = static_cast(in.at(i + 2)); - palette.append(qRgb(this->clampColorValue(red), - this->clampColorValue(green), - this->clampColorValue(blue))); + palette.append(qRgb(clampColorValue(red), + clampColorValue(green), + clampColorValue(blue))); i += 3; } return palette; } -QList PaletteUtil::parseAdvancePaletteEditor(QString filepath, bool *error) { +QList parseAdvancePaletteEditor(QString filepath, bool *error) { QFile file(filepath); if (!file.open(QIODevice::ReadOnly)) { *error = true; @@ -258,44 +290,16 @@ QList PaletteUtil::parseAdvancePaletteEditor(QString filepath, bool *error int red = (raw & 0x1F) * 8; int green = ((raw >> 5) & 0x1F) * 8; int blue = ((raw >> 10) & 0x1F) * 8; - palette.append(qRgb(this->clampColorValue(red), - this->clampColorValue(green), - this->clampColorValue(blue))); + palette.append(qRgb(clampColorValue(red), + clampColorValue(green), + clampColorValue(blue))); } file.close(); return palette; } -void PaletteUtil::writeJASC(QString filepath, QVector 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()); - } -} - -int PaletteUtil::clampColorValue(int value) { +int clampColorValue(int value) { if (value < 0) { value = 0; } diff --git a/src/core/regionmap.cpp b/src/core/regionmap.cpp index c5a1b294..7d3c8a7e 100644 --- a/src/core/regionmap.cpp +++ b/src/core/regionmap.cpp @@ -63,8 +63,7 @@ void RegionMap::saveTileImages() { this->region_map_png_path = project->root + "/graphics/pokenav/region_map.png"; pngImage.save(pngPath()); - PaletteUtil parser; - parser.writeJASC(project->root + "/graphics/pokenav/region_map.pal", pngImage.colorTable(), 0x70, 0x20); + PaletteUtil::writeJASC(project->root + "/graphics/pokenav/region_map.pal", pngImage.colorTable(), 0x70, 0x20); } region_map_png_needs_saving = false; } diff --git a/src/project.cpp b/src/project.cpp index 0a7d2dea..5f455f07 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1069,10 +1069,9 @@ void Project::saveTilesetTilesImage(Tileset *tileset) { } void Project::saveTilesetPalettes(Tileset *tileset) { - PaletteUtil paletteParser; for (int i = 0; i < Project::getNumPalettesTotal(); 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); } } diff --git a/src/ui/paletteeditor.cpp b/src/ui/paletteeditor.cpp index f376dc3b..d03615fa 100644 --- a/src/ui/paletteeditor.cpp +++ b/src/ui/paletteeditor.cpp @@ -279,9 +279,8 @@ void PaletteEditor::on_actionImport_Palette_triggered() return; } - PaletteUtil parser; bool error = false; - QList palette = parser.parse(filepath, &error); + QList palette = PaletteUtil::parse(filepath, &error); if (error) { QMessageBox msgBox(this); msgBox.setText("Failed to import palette."); diff --git a/src/ui/tileseteditor.cpp b/src/ui/tileseteditor.cpp index 4fd61a83..d324a5c9 100644 --- a/src/ui/tileseteditor.cpp +++ b/src/ui/tileseteditor.cpp @@ -626,9 +626,8 @@ void TilesetEditor::importTilesetTiles(Tileset *tileset, bool primary) { return; } - PaletteUtil parser; bool error = false; - QList palette = parser.parse(filepath, &error); + QList palette = PaletteUtil::parse(filepath, &error); if (error) { QMessageBox msgBox(this); msgBox.setText("Failed to import palette.");