Skip to content

Commit

Permalink
Add params to ReplaceColor command (now this filter can be used from …
Browse files Browse the repository at this point in the history
…a script without UI)
  • Loading branch information
dacap committed Jul 16, 2019
1 parent 66442ee commit 9143523
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
51 changes: 40 additions & 11 deletions src/app/commands/filters/cmd_replace_color.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
Expand All @@ -17,6 +18,8 @@
#include "app/commands/commands.h"
#include "app/commands/filters/filter_manager_impl.h"
#include "app/commands/filters/filter_window.h"
#include "app/commands/filters/filter_worker.h"
#include "app/commands/new_params.h"
#include "app/context.h"
#include "app/find_widget.h"
#include "app/ini_file.h"
Expand All @@ -37,6 +40,14 @@ namespace app {

static const char* ConfigSection = "ReplaceColor";

struct ReplaceColorParams : public NewParams {
Param<bool> ui { this, true, "ui" };
Param<filters::Target> target { this, 0, "target" };
Param<app::Color> from { this, app::Color(), "from" };
Param<app::Color> to { this, app::Color(), "to" };
Param<int> tolerance { this, 0, "tolerance" };
};

// Wrapper for ReplaceColorFilter to handle colors in an easy way
class ReplaceColorFilterWrapper : public ReplaceColorFilter {
public:
Expand Down Expand Up @@ -127,7 +138,7 @@ class ReplaceColorWindow : public FilterWindow {
ui::Slider* m_toleranceSlider;
};

class ReplaceColorCommand : public Command {
class ReplaceColorCommand : public CommandWithNewParams<ReplaceColorParams> {
public:
ReplaceColorCommand();

Expand All @@ -137,7 +148,7 @@ class ReplaceColorCommand : public Command {
};

ReplaceColorCommand::ReplaceColorCommand()
: Command(CommandId::ReplaceColor(), CmdRecordableFlag)
: CommandWithNewParams<ReplaceColorParams>(CommandId::ReplaceColor(), CmdRecordableFlag)
{
}

Expand All @@ -149,13 +160,10 @@ bool ReplaceColorCommand::onEnabled(Context* context)

void ReplaceColorCommand::onExecute(Context* context)
{
const bool ui = (params().ui() && context->isUIAvailable());
Site site = context->activeSite();

ReplaceColorFilterWrapper filter(site.layer());
filter.setFrom(get_config_color(ConfigSection, "Color1", ColorBar::instance()->getFgColor()));
filter.setTo(get_config_color(ConfigSection, "Color2", ColorBar::instance()->getBgColor()));
filter.setTolerance(get_config_int(ConfigSection, "Tolerance", 0));

FilterManagerImpl filterMgr(context, &filter);
filterMgr.setTarget(
site.sprite()->pixelFormat() == IMAGE_INDEXED ?
Expand All @@ -166,11 +174,32 @@ void ReplaceColorCommand::onExecute(Context* context)
TARGET_GRAY_CHANNEL |
TARGET_ALPHA_CHANNEL);

ReplaceColorWindow window(filter, filterMgr);
if (window.doModal()) {
set_config_color(ConfigSection, "From", filter.getFrom());
set_config_color(ConfigSection, "To", filter.getTo());
set_config_int(ConfigSection, "Tolerance", filter.getTolerance());
if (ui) {
filter.setFrom(get_config_color(ConfigSection, "Color1", ColorBar::instance()->getFgColor()));
filter.setTo(get_config_color(ConfigSection, "Color2", ColorBar::instance()->getBgColor()));
filter.setTolerance(get_config_int(ConfigSection, "Tolerance", 0));
}
else {
filter.setFrom(ColorBar::instance()->getFgColor());
filter.setTo(ColorBar::instance()->getBgColor());
filter.setTolerance(params().tolerance());
}

if (params().from.isSet()) filter.setFrom(params().from());
if (params().to.isSet()) filter.setTo(params().to());
if (params().tolerance.isSet()) filter.setTolerance(params().tolerance());
if (params().target.isSet()) filterMgr.setTarget(params().target());

if (ui) {
ReplaceColorWindow window(filter, filterMgr);
if (window.doModal()) {
set_config_color(ConfigSection, "From", filter.getFrom());
set_config_color(ConfigSection, "To", filter.getTo());
set_config_int(ConfigSection, "Tolerance", filter.getTolerance());
}
}
else {
start_filter_worker(&filterMgr);
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/app/commands/new_params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

#include "app/commands/new_params.h"

#include "app/color.h"
#include "app/doc_exporter.h"
#include "app/sprite_sheet_type.h"
#include "base/convert_to.h"
#include "base/string.h"
#include "doc/color_mode.h"

#ifdef ENABLE_SCRIPTING
#include "app/script/engine.h"
#include "app/script/luacpp.h"
#endif

Expand Down Expand Up @@ -83,6 +85,12 @@ void Param<doc::ColorMode>::fromString(const std::string& value)
setValue(doc::ColorMode::RGB);
}

template<>
void Param<app::Color>::fromString(const std::string& value)
{
setValue(app::Color::fromString(value));
}

#ifdef ENABLE_SCRIPTING

template<>
Expand Down Expand Up @@ -133,6 +141,12 @@ void Param<doc::ColorMode>::fromLua(lua_State* L, int index)
setValue((doc::ColorMode)lua_tointeger(L, index));
}

template<>
void Param<app::Color>::fromLua(lua_State* L, int index)
{
setValue(script::convert_args_into_color(L, index));
}

void CommandWithNewParamsBase::loadParamsFromLuaTable(lua_State* L, int index)
{
onResetValues();
Expand Down
12 changes: 12 additions & 0 deletions src/app/script/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "doc/anidir.h"
#include "doc/blend_mode.h"
#include "doc/color_mode.h"
#include "filters/target.h"

#include <fstream>
#include <sstream>
Expand Down Expand Up @@ -319,6 +320,17 @@ Engine::Engine()
setfield_integer(L, "NONE", doc::BrushPattern::PAINT_BRUSH);
lua_pop(L, 1);

lua_newtable(L);
lua_pushvalue(L, -1);
lua_setglobal(L, "FilterTarget");
setfield_integer(L, "RED", TARGET_RED_CHANNEL);
setfield_integer(L, "GREEN", TARGET_GREEN_CHANNEL);
setfield_integer(L, "BLUE", TARGET_BLUE_CHANNEL);
setfield_integer(L, "ALPHA", TARGET_ALPHA_CHANNEL);
setfield_integer(L, "GRAY", TARGET_GRAY_CHANNEL);
setfield_integer(L, "INDEX", TARGET_INDEX_CHANNEL);
lua_pop(L, 1);

// Register classes/prototypes
register_brush_class(L);
register_cel_class(L);
Expand Down

0 comments on commit 9143523

Please sign in to comment.