package com.fundynamic.d2tm.game.rendering;

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

public enum Palette {
    RED(java.awt.Color.RED),
    GREEN(java.awt.Color.GREEN),
    BLUE(java.awt.Color.BLUE);

    private final float hue;

    private Palette(final java.awt.Color color) {
        this(java.awt.Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null)[0]);
    }

    private Palette(final float hue) {
        this.hue = hue;
    }

    public final float getHue() {
        return hue;
    }

    public SpriteSheet recolor(SpriteSheet spriteSheet) {
        final int width = spriteSheet.getWidth();
        final int height = spriteSheet.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 = spriteSheet.getColor(x, y);
                final float[] hsbColor = java.awt.Color.RGBtoHSB(pixel.getRed(), pixel.getGreen(), pixel.getBlue(), null);
                final Color rgbColor = new Color(java.awt.Color.HSBtoRGB(hue, hsbColor[1], hsbColor[2]));
                buffer.setRGBA(x, y, rgbColor.getRed(), rgbColor.getGreen(), rgbColor.getBlue(), pixel.getAlpha());
            }
        }
        return new SpriteSheet(buffer.getImage(), width / spriteSheet.getHorizontalCount(), height / spriteSheet.getVerticalCount());
    }
}


@@ -180,410 +180,30 @@
  RED
-(java.awt.Color.RED),%0A    GREEN(java.awt.Color.GREEN),%0A    BLUE(java.awt.Color.BLUE);%0A%0A    private final float hue;%0A%0A    private Palette(final java.awt.Color color) %7B%0A        this(java.awt.Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null)%5B0%5D);%0A    %7D%0A%0A    private Palette(final float hue) %7B%0A        this.hue = hue;%0A    %7D%0A%0A    public final float getHue() %7B%0A        return hue;%0A    %7D
+,%0A    GREEN,%0A    BLUE;
 %0A%0A  
@@ -625,56 +625,86 @@
 nal 
-float%5B%5D hsbColor = java.awt.Color.RGBtoHSB(pixel
+Color newColor = recolor(pixel);%0A                buffer.setRGBA(x, y, newColor
 .get
@@ -706,29 +706,32 @@
 r.getRed(), 
-pixel
+newColor
 .getGreen(),
@@ -727,29 +727,32 @@
 getGreen(), 
-pixel
+newColor
 .getBlue(), 
@@ -755,12 +755,24 @@
 (), 
-null
+pixel.getAlpha()
 );%0A 
@@ -786,143 +786,347 @@
     
+%7D%0A
     
-final Color rgbColor = new Color(java.awt.Color.HSBtoRGB(hue, hsbColor%5B1%5D, hsbColor%5B2%5D));%0A                buffer.setRGBA(x, y, rgbC
+    %7D%0A        return new SpriteSheet(buffer.getImage(), width / spriteSheet.getHorizontalCount(), height / spriteSheet.getVerticalCount());%0A    %7D%0A%0A    public Color recolor(Color color) %7B%0A        // Only recolor when the color is a bright red hue. This assumes that%0A        // sprites are always red structures or units.%0A        if (c
 olor
@@ -1134,22 +1134,20 @@
 getRed()
-, rgbC
+ %3C c
 olor.get
@@ -1157,14 +1157,12 @@
 en()
-, rgbC
+ + c
 olor
@@ -1171,35 +1171,42 @@
 etBlue()
-, pixel.getAlpha())
+) %7B%0A          return color
 ;%0A      
@@ -1211,32 +1211,68 @@
     
+%7D%0A%0A
     
-%7D%0A        %7D%0A
+    switch (this) %7B%0A          case GREEN:%0A   
         
 retu
@@ -1259,32 +1259,33 @@
 EEN:%0A           
+ 
 return new Sprit
@@ -1283,122 +1283,258 @@
 new 
-SpriteSheet(buffe
+Color(color.getGreen(), color.getRed(), colo
 r.get
-Imag
+Blu
 e(), 
-width / spriteSheet.getHorizontalCount(), height / spriteSheet.getVerticalCount());
+color.getAlpha());%0A          case BLUE:%0A            return new Color(color.getBlue(), color.getGreen(), color.getRed(), color.getAlpha());%0A          default:%0A            return color;%0A        %7D
 %0A   

Improve recolor algorithm