Add support to use ColorQuantization from scripts

This commit is contained in:
David Capello 2019-08-12 15:56:18 -03:00
parent 35aaa18ee3
commit f317f9594d
3 changed files with 69 additions and 36 deletions

View File

@ -207,7 +207,6 @@ if(ENABLE_UI)
commands/cmd_clear_cel.cpp
commands/cmd_clear_recent_files.cpp
commands/cmd_close_file.cpp
commands/cmd_color_quantization.cpp
commands/cmd_contiguous_fill.cpp
commands/cmd_copy.cpp
commands/cmd_copy_cel.cpp
@ -494,6 +493,7 @@ add_library(app-lib
commands/cmd_canvas_size.cpp
commands/cmd_cel_opacity.cpp
commands/cmd_change_pixel_format.cpp
commands/cmd_color_quantization.cpp
commands/cmd_crop.cpp
commands/cmd_export_sprite_sheet.cpp
commands/cmd_flatten_layers.cpp

View File

@ -11,7 +11,7 @@
#include "app/app.h"
#include "app/cmd/set_palette.h"
#include "app/commands/command.h"
#include "app/commands/new_params.h"
#include "app/console.h"
#include "app/context.h"
#include "app/context_access.h"
@ -29,9 +29,18 @@
#include "palette_from_sprite.xml.h"
#include <algorithm>
namespace app {
class ColorQuantizationCommand : public Command {
struct ColorQuantizationParams : public NewParams {
Param<bool> ui { this, true, "ui" };
Param<bool> withAlpha { this, true, "withAlpha" };
Param<int> maxColors { this, 256, "maxColors" };
Param<bool> useRange { this, false, "useRange" };
};
class ColorQuantizationCommand : public CommandWithNewParams<ColorQuantizationParams> {
public:
ColorQuantizationCommand();
@ -41,37 +50,44 @@ protected:
};
ColorQuantizationCommand::ColorQuantizationCommand()
: Command(CommandId::ColorQuantization(), CmdRecordableFlag)
: CommandWithNewParams<ColorQuantizationParams>(
CommandId::ColorQuantization(),
CmdRecordableFlag)
{
}
bool ColorQuantizationCommand::onEnabled(Context* context)
bool ColorQuantizationCommand::onEnabled(Context* ctx)
{
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable);
return ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable);
}
void ColorQuantizationCommand::onExecute(Context* context)
void ColorQuantizationCommand::onExecute(Context* ctx)
{
try {
#ifdef ENABLE_UI
const bool ui = (params().ui() && ctx->isUIAvailable());
#endif
bool withAlpha = params().withAlpha();
int maxColors = params().maxColors();
bool createPal;
Site site = ctx->activeSite();
PalettePicks entries = site.selectedColors();
#ifdef ENABLE_UI
if (ui) {
app::gen::PaletteFromSprite window;
PalettePicks entries;
Sprite* sprite;
frame_t frame;
Palette* curPalette;
{
ContextReader reader(context);
Site site = context->activeSite();
sprite = site.sprite();
frame = site.frame();
curPalette = sprite->palette(frame);
ContextReader reader(ctx);
const Palette* curPalette = site.sprite()->palette(site.frame());
if (!params().withAlpha.isSet())
withAlpha = App::instance()->preferences().quantization.withAlpha();
window.newPalette()->setSelected(true);
window.alphaChannel()->setSelected(
App::instance()->preferences().quantization.withAlpha());
window.ncolors()->setText("256");
window.alphaChannel()->setSelected(withAlpha);
window.ncolors()->setTextf("%d", maxColors);
ColorBar::instance()->getPaletteView()->getSelectedEntries(entries);
if (entries.picks() > 1) {
window.currentRange()->setTextf(
"%s, %d color(s)",
@ -91,26 +107,39 @@ void ColorQuantizationCommand::onExecute(Context* context)
if (window.closer() != window.ok())
return;
bool withAlpha = window.alphaChannel()->isSelected();
maxColors = window.ncolors()->textInt();
withAlpha = window.alphaChannel()->isSelected();
App::instance()->preferences().quantization.withAlpha(withAlpha);
bool createPal = false;
if (window.newPalette()->isSelected()) {
int n = window.ncolors()->textInt();
n = MAX(1, n);
entries = PalettePicks(n);
entries.all();
createPal = true;
}
else if (window.currentPalette()->isSelected()) {
entries.all();
else {
createPal = false;
if (window.currentPalette()->isSelected())
entries.all();
}
if (entries.picks() == 0)
return;
}
else
#endif // ENABLE_UI
{
createPal = (!params().useRange());
}
if (createPal) {
entries = PalettePicks(std::max(1, maxColors));
entries.all();
}
if (entries.picks() == 0)
return;
try {
ContextReader reader(ctx);
Sprite* sprite = site.sprite();
frame_t frame = site.frame();
const Palette* curPalette = site.sprite()->palette(frame);
Palette tmpPalette(frame, entries.picks());
ContextReader reader(context);
SpriteJob job(reader, "Color Quantization");
const bool newBlend = Preferences::instance().experimental.newBlend();
job.startJobWithCallback(
@ -144,8 +173,12 @@ void ColorQuantizationCommand::onExecute(Context* context)
if (*curPalette != *newPalette)
job.tx()(new cmd::SetPalette(sprite, frame, newPalette.get()));
set_current_palette(newPalette.get(), false);
ui::Manager::getDefault()->invalidate();
#ifdef ENABLE_UI
if (ui) {
set_current_palette(newPalette.get(), false);
ui::Manager::getDefault()->invalidate();
}
#endif // ENABLE_UI
}
catch (const base::Exception& e) {
Console::showException(e);

View File

@ -12,6 +12,7 @@ FOR_EACH_COMMAND(CanvasSize)
FOR_EACH_COMMAND(CelOpacity)
FOR_EACH_COMMAND(ChangePixelFormat)
FOR_EACH_COMMAND(ColorCurve)
FOR_EACH_COMMAND(ColorQuantization)
FOR_EACH_COMMAND(ConvolutionMatrix)
FOR_EACH_COMMAND(CopyColors)
FOR_EACH_COMMAND(CropSprite)
@ -52,7 +53,6 @@ FOR_EACH_COMMAND(ClearCel)
FOR_EACH_COMMAND(ClearRecentFiles)
FOR_EACH_COMMAND(CloseAllFiles)
FOR_EACH_COMMAND(CloseFile)
FOR_EACH_COMMAND(ColorQuantization)
FOR_EACH_COMMAND(ContiguousFill)
FOR_EACH_COMMAND(Copy)
FOR_EACH_COMMAND(CopyCel)