Java BufferedImage Color Set changeColor(BufferedImage image, String hexval)

Here you can find the source of changeColor(BufferedImage image, String hexval)

Description

Changes the foreground colors to the color equivalent to the given hex value

License

Open Source License

Parameter

Parameter Description
image The original image
hexval The hexadecimal value of a color without a leading #

Return

The converted image

Declaration

public static BufferedImage changeColor(BufferedImage image, String hexval) 

Method Source Code

//package com.java2s;
/**//from   www .jav a2 s  . co m
 * monimix - monochrome image mix: it combines monochrome images to one
 * Copyright (C) 2012 Periklis Ntanasis <pntanasis@gmail.com>
 *
 * This program 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.
 *
 * This program 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
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Changes the foreground colors to the color equivalent to the given hex
     * value
     *
     * @param image The original image
     * @param hexval The hexadecimal value of a color without a leading #
     * @return The converted image
     */
    public static BufferedImage changeColor(BufferedImage image, String hexval) {
        int red = Integer.valueOf(hexval.substring(0, 2), 16);
        int green = Integer.valueOf(hexval.substring(2, 4), 16);
        int blue = Integer.valueOf(hexval.substring(4, 6), 16);
        int RGBval = red;
        RGBval = (RGBval << 8) + green;
        RGBval = (RGBval << 8) + blue;
        RGBval = RGBval + Color.BLACK.getRGB();
        for (int w = 0; w < image.getWidth(); w++) {
            for (int h = 0; h < image.getHeight(); h++) {
                if (image.getRGB(w, h) != Color.WHITE.getRGB() && image.getRGB(w, h) != 0) {
                    image.setRGB(w, h, RGBval);
                }
            }
        }
        return image;
    }
}

Related

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