From edb9f8945668f7de928fa42e9cf984fab29c3184 Mon Sep 17 00:00:00 2001 From: Jeremy Behreandt Date: Tue, 31 Aug 2021 01:10:18 -0500 Subject: [PATCH 1/2] Cyclic Color Change in Palettes Made Change Color: Increment/Decrement Foreground/Background methods cyclic instead of clamped. At last palette element, incrementing selected color returns to first element. At first palette element, decrementing color returns to last element. --- src/app/commands/cmd_change_color.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/app/commands/cmd_change_color.cpp b/src/app/commands/cmd_change_color.cpp index 3f431c93a..4c88c8051 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; } } From 845f275ae7e962d4b267c5d73f953878267b0196 Mon Sep 17 00:00:00 2001 From: David Capello Date: Wed, 1 Sep 2021 10:38:35 -0300 Subject: [PATCH 2/2] Enable cycling colors when palette size=1 so RGB/HSV colors are converted to index=0 anyway In this way after ChangeColor command to increase/decrease an index is used, we always got an index selected (even index=0 if palette size=1) --- src/app/commands/cmd_change_color.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/commands/cmd_change_color.cpp b/src/app/commands/cmd_change_color.cpp index 4c88c8051..076bc836b 100644 --- a/src/app/commands/cmd_change_color.cpp +++ b/src/app/commands/cmd_change_color.cpp @@ -76,7 +76,7 @@ void ChangeColorCommand::onExecute(Context* context) break; case IncrementIndex: { const int palSize = get_current_palette()->size(); - if (palSize > 1) { + 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); @@ -85,7 +85,7 @@ void ChangeColorCommand::onExecute(Context* context) } case DecrementIndex: { const int palSize = get_current_palette()->size(); - if (palSize > 1) { + if (palSize >= 1) { // Floor modulo. color = app::Color::fromIndex(((color.getIndex() - 1) % palSize + palSize) % palSize); }