Skip to content

Commit

Permalink
Added support for setting colors by string in Image.setColorTable
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn committed Nov 5, 2020
1 parent 5221ebc commit 9124a7c
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/tiled/scriptimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "scriptmanager.h"

#include <QCoreApplication>
#include <QJSEngine>

namespace Tiled {
Expand Down Expand Up @@ -71,8 +72,24 @@ void ScriptImage::setColorTable(QJSValue colors)
const int length = colors.property(QStringLiteral("length")).toInt();
colorTable.resize(length);

for (int i = 0; i < length; ++i)
colorTable[i] = colors.property(i).toUInt();
for (int i = 0; i < length; ++i) {
const QJSValue color = colors.property(i);
if (color.isNumber()) {
colorTable[i] = color.toUInt();
} else if (color.isString()) {
const QString colorName = color.toString();
if (QColor::isValidColor(colorName)) {
colorTable[i] = QColor(colorName).rgb();
} else {
ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors",
"Invalid color name: '%2'").arg(colorName));
return;
}
} else {
ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "Invalid color value"));
return;
}
}

mImage.setColorTable(std::move(colorTable));
}
Expand Down

0 comments on commit 9124a7c

Please sign in to comment.