Java Image swapColor(Image image, Color imageColor, Color newColor)

Here you can find the source of swapColor(Image image, Color imageColor, Color newColor)

Description

swap Color

License

Open Source License

Parameter

Parameter Description
image a parameter
imageColor a parameter
newColor a parameter

Declaration

public static Image swapColor(Image image, Color imageColor, Color newColor) 

Method Source Code

//package com.java2s;
/*/*from  ww w.j  ava  2 s. co  m*/
 * Copyright appNativa Inc. All Rights Reserved.
 *
 * This file is part of the Real-time Application Rendering Engine (RARE).
 *
 * RARE 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.Component;
import java.awt.Composite;

import java.awt.Dimension;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.Toolkit;

import java.awt.image.BufferedImage;

import java.awt.image.FilteredImageSource;

import java.awt.image.RGBImageFilter;

import javax.swing.Icon;

public class Main {
    /**
     * {@inheritDoc}
     *
     * @param image {@inheritDoc}
     * @param imageColor {@inheritDoc}
     * @param newColor {@inheritDoc}
     *
     * @return {@inheritDoc}
     */
    public static Image swapColor(Image image, Color imageColor, Color newColor) {
        final int irgb = imageColor.getRGB();
        final int nrgb = newColor.getRGB();
        RGBImageFilter filter = new RGBImageFilter() {
            @Override
            public int filterRGB(int x, int y, int rgb) {
                if (rgb == irgb) {
                    return nrgb;
                }

                return rgb;
            }
        };
        FilteredImageSource filteredSrc = new FilteredImageSource(image.getSource(), filter);

        return Toolkit.getDefaultToolkit().createImage(filteredSrc);
    }

    public static BufferedImage createImage(Component comp) {
        Dimension size = comp.getSize();

        if ((size.width < 1) || (size.height < 1)) {
            size.width = 1;
            size.height = 1;
        }

        BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TRANSLUCENT);
        Graphics2D g2 = image.createGraphics();

        try {
            comp.paint(g2);
        } finally {
            g2.dispose();
        }

        return image;
    }

    public static BufferedImage createImage(Component comp, Icon icon) {
        return createImage(comp, icon, null);
    }

    public static BufferedImage createImage(Component comp, Icon icon, Composite composite) {
        if (icon == null) {
            return null;
        }

        BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                BufferedImage.TRANSLUCENT);
        Graphics2D g2 = image.createGraphics();

        if (composite != null) {
            g2.setComposite(composite);
        }

        try {
            icon.paintIcon(comp, g2, 0, 0);
        } finally {
            g2.dispose();
        }

        return image;
    }
}

Related

  1. setImageDimension(Image imagen)
  2. showImage(final String title, final Image image)
  3. showImage(Image image)
  4. showImage(Image image, String title)
  5. sizeToImage(JComponent component, Image image)
  6. texturePaintHorizontal(JComponent parent, Graphics g, Image image, Rectangle areaToPaint)
  7. usesAlpha(Image image)
  8. verifyImageSizeBigger(byte[] imageData, int maxDim)