Material Function - Avoid Modifying the Same Pixels

I’m making a game with Paper2D. As part of this, I want to have customizable colors for the player character. My material uses the material function below to replace one color with another, where the BaseColor is the color to be replaced, the NewColor is the color to replace it with, and Pixels is the current texture (the Source Texture multiplied by the Vertex color, as in the default sprite material).

The function works correctly, but when I have multiple parts of the texture to be replaced (such as when the player sets their outfit main color and trim color separately), there is the issue that the same pixels could be run multiple times. For example, if my spriteset has the outfit main color as red and the trim color as blue, and the player sets the main color to blue and the trim to red, the entire outfit comes out red, because the main outfit was changed again when attempting to change the trim color.

How can I block editing the same pixels when calling this function repeatedly?