package com.fundynamic.d2tm.game.rendering;

import org.newdawn.slick.Color;
import org.newdawn.slick.Image;
import org.newdawn.slick.ImageBuffer;

public class Recolorer {

    public enum FactionColor {
        RED, GREEN, BLUE
    }

    public Image recolor(Image image, FactionColor factionColor) {
        final int width = image.getWidth();
        final int height = image.getHeight();

        ImageBuffer buffer = new ImageBuffer(width, height);
        for (int x = 0; x < buffer.getWidth(); x++) {
            for (int y = 0; y < buffer.getHeight(); y++) {
                final Color pixel = image.getColor(x, y);
                final Color newColor = recolor(pixel, factionColor);
                buffer.setRGBA(x, y, newColor.getRed(), newColor.getGreen(), newColor.getBlue(), pixel.getAlpha());
            }
        }
        return buffer.getImage();
    }

    public Color recolor(Color src, FactionColor factionColor) {
        // Only recolor when the color is a bright red hue. This assumes that
        // sprites are always red structures or units.
        if (src.getRed() < src.getGreen() + src.getBlue()) {
          return src;
        }

        switch (factionColor) {
          case GREEN:
            return new Color(src.getGreen(), src.getRed(), src.getBlue(), src.getAlpha());
          case BLUE:
            return new Color(src.getBlue(), src.getGreen(), src.getRed(), src.getAlpha());
          default:
            return src;
        }
    }
}


@@ -145,32 +145,508 @@
 r;%0A%0A
-public class Recolorer %7B
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0Apublic class Recolorer %7B%0A%0A    private List%3CColor%3E redColors;%0A%0A    public Recolorer() %7B%0A        this.redColors = new ArrayList%3C%3E();%0A        redColors.add(new Color(214, 0, 0));%0A        redColors.add(new Color(182, 0, 0));%0A        redColors.add(new Color(153, 0, 0));%0A        redColors.add(new Color(125, 0, 0));%0A        redColors.add(new Color(89, 0, 0));%0A        redColors.add(new Color(60, 0, 0));%0A        redColors.add(new Color(32, 0, 0));%0A    %7D
 %0A%0A  
@@ -1423,189 +1423,35 @@
     
-// Only recolor when the color is a bright red hue. This assumes that%0A        // sprites are always red structures or units.%0A        if (src.getRed() %3C src.getGreen() + src.getBlue(
+if (!redColors.contains(src
 )) %7B

replace only the 7 (red) colors of units