Java BufferedImage to Transparent makeColorTransparent(final BufferedImage im, final Color color)

Here you can find the source of makeColorTransparent(final BufferedImage im, final Color color)

Description

Make provided image transparent wherever color matches the provided color.

License

Apache License

Parameter

Parameter Description
im BufferedImage whose color will be made transparent.
color Color in provided image which will be made transparent.

Return

Image with transparency applied.

Declaration

public static Image makeColorTransparent(final BufferedImage im, final Color color) 

Method Source Code

//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);
    }
}

Related

  1. getTransparentImage(BufferedImage i)
  2. getTransperentImage(BufferedImage origImage, BufferedImage compImg)
  3. makeColorTransparent(BufferedImage im, final Color color)
  4. makeColorTransparent(BufferedImage im, final Color color)
  5. makeColorTransparent(BufferedImage image, Color color)
  6. makeColorTransparent(final BufferedImage im, final Color colorToMakeTransparent, final int tolerance)
  7. makeColorTransparent(Image im, final Color color)
  8. makeColorTransparent(Image image, final Color transparentColor)
  9. makeTranslucentImage(BufferedImage image, float transparency)