Here you can find the source of makeColorTransparent(final BufferedImage im, final Color color)
Parameter | Description |
---|---|
im | BufferedImage whose color will be made transparent. |
color | Color in provided image which will be made transparent. |
public static Image makeColorTransparent(final BufferedImage im, final Color color)
//package com.java2s; /*/*from ww w . j a v a2s .com*/ * Copyright 2009-2016 DigitalGlobe, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and limitations under the License. * */ import java.awt.*; import java.awt.image.*; public class Main { /** * Make provided image transparent wherever color matches the provided color. * * @param im BufferedImage whose color will be made transparent. * @param color Color in provided image which will be made transparent. * @return Image with transparency applied. */ public static Image makeColorTransparent(final BufferedImage im, final Color color) { final ImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque public int markerRGB = color.getRGB() | 0xFF000000; @Override public final int filterRGB(final int x, final int y, final int rgb) { if ((rgb | 0xFF000000) == markerRGB) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb; } // nothing to do return rgb; } }; final ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); } }