| 1 | // Aseprite |
|---|---|
| 2 | // Copyright (C) 2001-2017 David Capello |
| 3 | // |
| 4 | // This program is distributed under the terms of |
| 5 | // the End-User License Agreement for Aseprite. |
| 6 | |
| 7 | #ifdef HAVE_CONFIG_H |
| 8 | #include "config.h" |
| 9 | #endif |
| 10 | |
| 11 | #include <string> |
| 12 | |
| 13 | #include "app/app.h" |
| 14 | #include "app/commands/command.h" |
| 15 | #include "app/commands/params.h" |
| 16 | #include "app/i18n/strings.h" |
| 17 | #include "app/modules/palettes.h" |
| 18 | #include "app/ui/color_bar.h" |
| 19 | #include "doc/palette.h" |
| 20 | #include "fmt/format.h" |
| 21 | |
| 22 | namespace app { |
| 23 | |
| 24 | class ChangeColorCommand : public Command |
| 25 | { |
| 26 | enum Change { |
| 27 | None, |
| 28 | IncrementIndex, |
| 29 | DecrementIndex, |
| 30 | }; |
| 31 | |
| 32 | /** |
| 33 | * True means "change background color", false the foreground color. |
| 34 | */ |
| 35 | bool m_background; |
| 36 | |
| 37 | Change m_change; |
| 38 | |
| 39 | public: |
| 40 | ChangeColorCommand(); |
| 41 | |
| 42 | protected: |
| 43 | bool onNeedsParams() const override { return true; } |
| 44 | void onLoadParams(const Params& params) override; |
| 45 | void onExecute(Context* context) override; |
| 46 | std::string onGetFriendlyName() const override; |
| 47 | }; |
| 48 | |
| 49 | ChangeColorCommand::ChangeColorCommand() |
| 50 | : Command(CommandId::ChangeColor(), CmdUIOnlyFlag) |
| 51 | { |
| 52 | m_background = false; |
| 53 | m_change = None; |
| 54 | } |
| 55 | |
| 56 | void ChangeColorCommand::onLoadParams(const Params& params) |
| 57 | { |
| 58 | std::string target = params.get("target"); |
| 59 | if (target == "foreground") m_background = false; |
| 60 | else if (target == "background") m_background = true; |
| 61 | |
| 62 | std::string change = params.get("change"); |
| 63 | if (change == "increment-index") m_change = IncrementIndex; |
| 64 | else if (change == "decrement-index") m_change = DecrementIndex; |
| 65 | } |
| 66 | |
| 67 | void ChangeColorCommand::onExecute(Context* context) |
| 68 | { |
| 69 | ColorBar* colorbar = ColorBar::instance(); |
| 70 | app::Color color = m_background ? colorbar->getBgColor(): |
| 71 | colorbar->getFgColor(); |
| 72 | |
| 73 | switch (m_change) { |
| 74 | case None: |
| 75 | // do nothing |
| 76 | break; |
| 77 | case IncrementIndex: { |
| 78 | const int palSize = get_current_palette()->size(); |
| 79 | if (palSize >= 1) { |
| 80 | // Seems safe to assume getIndex() will return a |
| 81 | // positive number, so use truncation modulo. |
| 82 | color = app::Color::fromIndex((color.getIndex() + 1) % palSize); |
| 83 | } |
| 84 | break; |
| 85 | } |
| 86 | case DecrementIndex: { |
| 87 | const int palSize = get_current_palette()->size(); |
| 88 | if (palSize >= 1) { |
| 89 | // Floor modulo. |
| 90 | color = app::Color::fromIndex(((color.getIndex() - 1) % palSize + palSize) % palSize); |
| 91 | } |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (m_background) |
| 97 | colorbar->setBgColor(color); |
| 98 | else |
| 99 | colorbar->setFgColor(color); |
| 100 | } |
| 101 | |
| 102 | std::string ChangeColorCommand::onGetFriendlyName() const |
| 103 | { |
| 104 | std::string action; |
| 105 | |
| 106 | switch (m_change) { |
| 107 | case None: |
| 108 | break; |
| 109 | case IncrementIndex: |
| 110 | if (m_background) |
| 111 | action = Strings::commands_ChangeColor_IncrementBgIndex(); |
| 112 | else |
| 113 | action = Strings::commands_ChangeColor_IncrementFgIndex(); |
| 114 | break; |
| 115 | case DecrementIndex: |
| 116 | if (m_background) |
| 117 | action = Strings::commands_ChangeColor_DecrementBgIndex(); |
| 118 | else |
| 119 | action = Strings::commands_ChangeColor_DecrementFgIndex(); |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | return fmt::format(getBaseFriendlyName(), action); |
| 124 | } |
| 125 | |
| 126 | Command* CommandFactory::createChangeColorCommand() |
| 127 | { |
| 128 | return new ChangeColorCommand; |
| 129 | } |
| 130 | |
| 131 | } // namespace app |
| 132 |