1// Aseprite
2// Copyright (C) 2020 Igara Studio S.A.
3// Copyright (C) 2001-2017 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "app/app.h"
13#include "app/commands/command.h"
14#include "app/modules/editors.h"
15#include "app/ui/color_bar.h"
16#include "app/ui/context_bar.h"
17#include "app/ui/editor/editor.h"
18#include "ui/base.h"
19
20namespace app {
21
22class SwitchColorsCommand : public Command {
23public:
24 SwitchColorsCommand();
25
26protected:
27 bool onEnabled(Context* context) override;
28 void onExecute(Context* context) override;
29};
30
31SwitchColorsCommand::SwitchColorsCommand()
32 : Command(CommandId::SwitchColors(), CmdUIOnlyFlag)
33{
34}
35
36bool SwitchColorsCommand::onEnabled(Context* context)
37{
38 return (current_editor ? true: false);
39}
40
41void SwitchColorsCommand::onExecute(Context* context)
42{
43 ASSERT(current_editor);
44 if (!current_editor)
45 return;
46
47 tools::Tool* tool = current_editor->getCurrentEditorTool();
48 if (tool) {
49 const auto& toolPref(Preferences::instance().tool(tool));
50 if (toolPref.ink() == tools::InkType::SHADING) {
51 App::instance()->contextBar()->reverseShadeColors();
52 }
53 }
54
55 DisableColorBarEditMode disable;
56 Site site = context->activeSite();
57 if (site.tilemapMode() == TilemapMode::Tiles) {
58 auto& pref = Preferences::instance();
59 doc::tile_t fg = pref.colorBar.fgTile();
60 doc::tile_t bg = pref.colorBar.bgTile();
61 pref.colorBar.bgTile(fg);
62 pref.colorBar.fgTile(bg);
63 }
64 else {
65 ColorBar* colorbar = ColorBar::instance();
66 app::Color fg = colorbar->getFgColor();
67 app::Color bg = colorbar->getBgColor();
68
69 // Change the background and then the foreground color so the color
70 // spectrum and color wheel shows the foreground color as the
71 // selected one.
72 colorbar->setBgColor(fg);
73 colorbar->setFgColor(bg);
74 }
75}
76
77Command* CommandFactory::createSwitchColorsCommand()
78{
79 return new SwitchColorsCommand;
80}
81
82} // namespace app
83