Java BufferedImage to Transparent makeColorTransparent(final BufferedImage im, final Color colorToMakeTransparent, final int tolerance)

Here you can find the source of makeColorTransparent(final BufferedImage im, final Color colorToMakeTransparent, final int tolerance)

Description

Credits go to http://stackoverflow.com/a/665483 and http://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/

License

Open Source License

Parameter

Parameter Description
im a parameter
colorToMakeTransparent a parameter
tolerance a parameter

Declaration

public static Image makeColorTransparent(final BufferedImage im, final Color colorToMakeTransparent,
        final int tolerance) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;

public class Main {
    /**/*w ww .java 2  s  . c o m*/
     *
     * Credits go to http://stackoverflow.com/a/665483 and
     * http://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/
     *
     * @param im
     * @param colorToMakeTransparent
     * @param tolerance
     *
     * @return
     */
    public static Image makeColorTransparent(final BufferedImage im, final Color colorToMakeTransparent,
            final int tolerance) {

        final ImageFilter transparencyfilter = new RGBImageFilter() {

            @Override
            public int filterRGB(int x, int y, int rgb) {
                final Color filterColor = new Color(rgb);

                if (colorsAreSimilar(filterColor, colorToMakeTransparent, tolerance)) {
                    // Mark the alpha bits as zero - transparent
                    return 0x00FFFFFF & rgb;
                } else {
                    // Nothing to do
                    return rgb;
                }

            }

        };

        final ImageProducer ip = new FilteredImageSource(im.getSource(), transparencyfilter);
        return Toolkit.getDefaultToolkit().createImage(ip);
    }

    /**
     * Credits: http://www.java-gaming.org/index.php?topic=32741.0
     *
     * Checks whether the two colors are similar for a given tolerance (in the
     * sense of a distance between the RGB values).
     *
     * @param c1
     * @param c2
     * @param tolerance
     * @return Whether or not the two colors are similar for the given tolerance.
     */
    public static boolean colorsAreSimilar(final Color c1, final Color c2, final int tolerance) {
        int r1 = c1.getRed();
        int g1 = c1.getGreen();
        int b1 = c1.getBlue();
        int r2 = c2.getRed();
        int g2 = c2.getGreen();
        int b2 = c2.getBlue();

        return ((r2 - tolerance <= r1) && (r1 <= r2 + tolerance) && (g2 - tolerance <= g1) && (g1 <= g2 + tolerance)
                && (b2 - tolerance <= b1) && (b1 <= b2 + tolerance));
    }
}

Related

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