<commit_before>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());
    }
}
<commit_msg>Improve recolor algorithm<commit_after>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,
    GREEN,
    BLUE;

    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 Color newColor = recolor(pixel);
                buffer.setRGBA(x, y, newColor.getRed(), newColor.getGreen(), newColor.getBlue(), pixel.getAlpha());
            }
        }
        return new SpriteSheet(buffer.getImage(), width / spriteSheet.getHorizontalCount(), height / spriteSheet.getVerticalCount());
    }

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

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

{'fraction_non_alphanumeric': 0.12612612612612611, 'fraction_numerical': 0.0028957528957528956, 'mean_word_length': 2.5653669724770642, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1692166', 'n_tokens_mistral': 967, 'n_tokens_neox': 918, 'n_words': 221}