Java BufferedImage Color Set changeColor(BufferedImage image, Color replacement)

Here you can find the source of changeColor(BufferedImage image, Color replacement)

Description

Changes color of image's border to specified color.

License

Open Source License

Parameter

Parameter Description
image image to deal with
replacement new color; null if new color is transparent

Declaration

public static BufferedImage changeColor(BufferedImage image,
        Color replacement) 

Method Source Code

//package com.java2s;
/*/*from   www  .  j a va2  s  .  c om*/
 * This file is part of qSim.
 *
 * qSim 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.
 *
 * qSim 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 qSim.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class Main {
    private static final int ALPHA = 0;
    private static final int RED = 1;
    private static final int GREEN = 2;
    private static final int BLUE = 3;
    private static final int HUE = 0;
    private static final int SATURATION = 1;
    private static final int TRANSPARENT = 0;
    private static final Color COLOR_TO_REPLACE = new Color(178, 186, 157);
    private static final Color TRANSPARENT_COLOR = new Color(178, 186, 157,
            0);

    /**
     * Changes color of image's border to specified color. All occurrence of
     * color RGB 178, 186, 157 will be replaced with new one.
     *
     * @param image image to deal with
     * @param replacement new color; null if new color is transparent
     * @return
     */
    public static BufferedImage changeColor(BufferedImage image,
            Color replacement) {
        if (replacement == null) {
            return changeColor(image, COLOR_TO_REPLACE, TRANSPARENT_COLOR);
        } else {
            return changeColor(image, COLOR_TO_REPLACE, replacement);
        }
    }

    /**
     * Changes color of image's border to specified color.
     *
     * @param image image to deal with
     * @param replacement new color
     * @param color to be replaced
     * @return
     */
    public static BufferedImage changeColor(BufferedImage image,
            Color original, Color replacement) {
        BufferedImage destImage = new BufferedImage(image.getWidth(),
                image.getHeight(), BufferedImage.TYPE_INT_ARGB);

        Graphics2D g = destImage.createGraphics();
        g.drawImage(image, null, 0, 0);
        g.dispose();

        int replacementRGB = replacement.getRGB();
        int originalRGB = original.getRGB();

        for (int i = 0; i < destImage.getWidth(); i++) {
            for (int j = 0; j < destImage.getHeight(); j++) {

                int destRGB = destImage.getRGB(i, j);

                if (matches(originalRGB, destRGB)) {
                    destImage.setRGB(i, j, replacementRGB);
                }
            }
        }

        return destImage;
    }

    private static boolean matches(int originalRGB, int destRGB) {
        float[] hsbMask = getHSBArray(originalRGB);
        float[] hsbDest = getHSBArray(destRGB);

        if (hsbMask[HUE] == hsbDest[HUE]
                && hsbMask[SATURATION] == hsbDest[SATURATION]
                && getRGBArray(destRGB)[ALPHA] != TRANSPARENT) {

            return true;
        }
        return false;
    }

    private static float[] getHSBArray(int rgb) {
        int[] rgbArr = getRGBArray(rgb);
        return Color.RGBtoHSB(rgbArr[RED], rgbArr[GREEN], rgbArr[BLUE],
                null);
    }

    private static int[] getRGBArray(int rgb) {
        return new int[] { (rgb >> 24) & 0xff, (rgb >> 16) & 0xff,
                (rgb >> 8) & 0xff, rgb & 0xff };
    }
}

Related

  1. changeBrightness(BufferedImage image, int offset)
  2. changeColor(BufferedImage image, Color color, Color replacement_color)
  3. changeColor(BufferedImage image, String hexval)
  4. changeRGBSaturation(final BufferedImage image, final double s)
  5. changeToTypeIntRGB(BufferedImage image)
  6. changeTranslucentImage(BufferedImage img, float transperancy)