diff --git a/src/app/commands/cmd_change_color.cpp b/src/app/commands/cmd_change_color.cpp index 3f431c93a..076bc836b 100644 --- a/src/app/commands/cmd_change_color.cpp +++ b/src/app/commands/cmd_change_color.cpp @@ -75,15 +75,20 @@ void ChangeColorCommand::onExecute(Context* context) // do nothing break; case IncrementIndex: { - int index = color.getIndex(); - if (index < get_current_palette()->size()-1) - color = app::Color::fromIndex(index+1); + const int palSize = get_current_palette()->size(); + if (palSize >= 1) { + // Seems safe to assume getIndex() will return a + // positive number, so use truncation modulo. + color = app::Color::fromIndex((color.getIndex() + 1) % palSize); + } break; } case DecrementIndex: { - int index = color.getIndex(); - if (index > 0) - color = app::Color::fromIndex(index-1); + const int palSize = get_current_palette()->size(); + if (palSize >= 1) { + // Floor modulo. + color = app::Color::fromIndex(((color.getIndex() - 1) % palSize + palSize) % palSize); + } break; } }