Java BufferedImage Operation tintImage(BufferedImage src, Color color, float tintOpacity)

Here you can find the source of tintImage(BufferedImage src, Color color, float tintOpacity)

Description

tint Image

License

Open Source License

Declaration

public static BufferedImage tintImage(BufferedImage src, Color color, float tintOpacity) 

Method Source Code

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

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.awt.image.Raster;

public class Main {
    public static BufferedImage tintImage(BufferedImage src, Color color, float tintOpacity) {
        BufferedImage img = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TRANSLUCENT);
        Graphics2D g2d = img.createGraphics();

        //Draw the base image
        g2d.drawImage(src, null, 0, 0);/*from  www . ja v  a2s  .com*/
        //Set the color to a transparent version of the input color
        g2d.setColor(
                new Color(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, tintOpacity));

        //Iterate over every pixel, if it isn't transparent paint over it
        Raster data = src.getData();
        for (int x = data.getMinX(); x < data.getWidth(); x++) {
            for (int y = data.getMinY(); y < data.getHeight(); y++) {

                int[] pixel = data.getPixel(x, y, new int[4]);

                if (pixel[0] == 0 && pixel[1] == 0 && pixel[2] == 0) {
                    continue;
                }

                if (pixel[3] > 0) { //If pixel isn't full alpha. Could also be pixel[3]==255
                    g2d.fillRect(x, y, 1, 1);
                }
            }
        }
        g2d.dispose();
        return img;
    }
}

Related

  1. texture2D(BufferedImage normalMap, int x, int y)
  2. thresholdImage(BufferedImage image, int threshold)
  3. tile(BufferedImage source, Rectangle r, GraphicsConfiguration conf)
  4. tileStretchPaint(Graphics g, Component component, BufferedImage image, Insets insets)
  5. tilt(BufferedImage image, double angle)
  6. unweaveFrom(BufferedImage bufferedImage)
  7. updateImageSpecifications( BufferedImage bufferedImage)
  8. validateSelectionRectangle(BufferedImage image, Rectangle selection)
  9. verticalGradient(BufferedImage image, Graphics2D g2d, Color... colors)