turakvlad/replace-color

Multiple Color replacement

jeroldangarcia opened this issue · 3 comments

Is possible to replace 2 or more colors ?

It definitely is, but it takes some finagling and can potentially cause issues. A simple replacement could look like this:

const targetColors = ['#FF0000', '#00FF00'];
const replaceColors = ['#0000FF', '#FF0000'];
let data = '/path/to/initial/image.png';
for (let i = 0; i < replaceColors.length; i++) {
  data = await replaceColor({
    image: data,
    colors: {
      type: "hex",
      targetColor: targetColors[i],
      replaceColor: replaceColors[i]
    }
  });
}

The problem becomes when you're replacing one color early on that could potentially be replaced further down in the process, like if my first replaceColors was '#00FF00', it would get replaced by '#FF0000' in the next pass. You can come up with some priority system, but it gets a little more difficult with different shades of colors.