/*
 *  Copyright 2002-2009 Mikhail Kryshen <mikhail@kryshen.net>
 *
 *  This file is part of BMTron.
 *
 *  BMTron is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  BMTron is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with BMTron.  If not, see <http://www.gnu.org/licenses/>.
 */
 
package kryshen.bmtron;

import java.awt.Color;
import java.awt.image.RGBImageFilter;

/**
 * Turns gray images into color.
 */
public class PlayerColorFilter extends RGBImageFilter {
    private final Color color;
    
    public PlayerColorFilter (Color color) {
        this.color = color;
        canFilterIndexColorModel = true;
    }

    public int filterRGB(int x, int y, int rgb) {
	int a = (rgb >> 24) & 0xFF;
        int r = (rgb >> 16) & 0xFF;
        int g = (rgb >>  8) & 0xFF;
        int b = (rgb >>  0) & 0xFF;
        
        if (r != g | r != b) return rgb;
        
        int r2 = (color.getRed()   + r) / 2;
        int g2 = (color.getGreen() + g) / 2;
        int b2 = (color.getBlue()  + b) / 2;
       
        return ((a  & 0xFF) << 24) |
               ((r2 & 0xFF) << 16) |
               ((g2 & 0xFF) <<  8) |
               ((b2 & 0xFF) <<  0);
    }
}
