Java Color Alpha applyColorFilter(Image image, Color color, float alpha)

Here you can find the source of applyColorFilter(Image image, Color color, float alpha)

Description

Apply simple color filter with specified alpha factor to the image

License

Open Source License

Declaration

public static void applyColorFilter(Image image, Color color, float alpha) 

Method Source Code


//package com.java2s;
/*/*from  w ww.  j a v  a 2s  . co m*/
 * Copyright 2010, 2011 Institut Pasteur.
 * 
 * This file is part of ICY.
 * 
 * ICY 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.
 * 
 * ICY 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 ICY. If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.AlphaComposite;
import java.awt.Color;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;

public class Main {
    /**
     * Apply simple color filter with specified alpha factor to the image
     */
    public static void applyColorFilter(Image image, Color color, float alpha) {
        if (image != null) {
            // should be Graphics2D compatible
            final Graphics2D g = (Graphics2D) image.getGraphics();
            final Rectangle rect = new Rectangle(image.getWidth(null), image.getHeight(null));

            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
            g.setColor(color);
            g.fill(rect);
            g.dispose();
        }
    }
}

Related

  1. buildColorRamp(Color startColor, Color endColor, int numColors, int alpha)
  2. changeAlpha(Color c, double alpha)
  3. changeAlpha(Color c, double newAlpha)
  4. changeAlpha(Color c, int alpha)