Java BufferedImage to Transparent makeColorTransparent(Image image, final Color transparentColor)

Here you can find the source of makeColorTransparent(Image image, final Color transparentColor)

Description

Makes the given color in the given Image transparent.

License

Open Source License

Parameter

Parameter Description
image The Image to modify.
transparentColor The color to make transparent.

Return

A new created where the color is transparent.

Declaration

public static Image makeColorTransparent(Image image, final Color transparentColor) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Karlsruhe Institute of Technology, Germany
 *                    Technical University Darmstadt, Germany
 *                    Chalmers University of Technology, Sweden
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/* ww  w .  jav  a2  s.  c  o  m*/
 *    Technical University Darmstadt - initial API and implementation and/or initial documentation
 *******************************************************************************/

import java.awt.Color;

import java.awt.Image;
import java.awt.Toolkit;

import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;

import java.awt.image.RGBImageFilter;

public class Main {
    /**
     * <p>
     * Makes the given color in the given {@link Image} transparent.
     * </p>
     * <p>
     * Fore more details have a look at
     * <a href="http://stackoverflow.com/questions/665406/how-to-make-a-color-transparent-in-a-bufferedimage-and-save-as-png">http://stackoverflow.com/questions/665406/how-to-make-a-color-transparent-in-a-bufferedimage-and-save-as-png</a>.
     * </p>
     * @param image The {@link Image} to modify.
     * @param transparentColor The color to make transparent.
     * @return A new created {@link Image} where the color is transparent.
     */
    public static Image makeColorTransparent(Image image, final Color transparentColor) {
        ImageFilter filter = new RGBImageFilter() {
            @Override
            public final int filterRGB(int x, int y, int rgb) {
                if (rgb == transparentColor.getRGB()) {
                    return rgb & 0xFFFFFF; // Set fully transparent but keep color
                }
                return rgb;
            }
        };
        ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
        return Toolkit.getDefaultToolkit().createImage(ip);
    }
}

Related

  1. makeColorTransparent(BufferedImage im, final Color color)
  2. makeColorTransparent(BufferedImage image, Color color)
  3. makeColorTransparent(final BufferedImage im, final Color color)
  4. makeColorTransparent(final BufferedImage im, final Color colorToMakeTransparent, final int tolerance)
  5. makeColorTransparent(Image im, final Color color)
  6. makeTranslucentImage(BufferedImage image, float transparency)
  7. makeTransparent(BufferedImage image, int x, int y)
  8. makeTransparent(BufferedImage img, Color trColor)
  9. makeTransparentEdges(BufferedImage src)