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.
This commit is contained in:
Jeremy Behreandt 2021-08-31 01:10:18 -05:00
parent dd2d226264
commit edb9f89456

View File

@ -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;
}
}