colorize JavaFX Image - Java JavaFX

Java examples for JavaFX:Color

Description

colorize JavaFX Image

Demo Code


//package com.java2s;

import javafx.scene.image.Image;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;

public class Main {
    public static Image colorizeImage(Image img, Color c, double a) {
        PixelReader reader = img.getPixelReader();
        WritableImage write = new WritableImage((int) img.getWidth(),
                (int) img.getHeight());
        PixelWriter writer = write.getPixelWriter();

        for (int readY = 0; readY < img.getHeight(); readY++) {
            for (int readX = 0; readX < img.getWidth(); readX++) {
                Color color = reader.getColor(readX, readY);
                if (color.getOpacity() == 0)
                    continue;
                color = color.interpolate(c, a);
                writer.setColor(readX, readY, color);
            }/*ww w .j av  a  2  s  .co m*/
        }

        return (Image) write;
    }
}

Related Tutorials