some refactoring of minimizeColorChanges