How to make a Gradient Map (2 colors) in an Image?

Asked

Viewed 113 times

1

I need a solution to turn the color of an image into black and white. There are 3 ways to do this, using Grayscale that technically plays white to gray, making the saturation stay -100 and through Gradient Map (2 colors) that filters the image and adjusts the color according to the gradient.

Of the 3 methods the best I found for what I need was the gradient map, because it makes the white and dark darker, having a better photographic effect, besides the advantage of being able to define other colors to create the gradient map. But so far I have found nothing to make the application of this effect in java.

Below is an image referring to what I mean.

Exemplo

At the moment I was able to create an algorithm that only makes the Gradient Map from White to Black and apparently it is not working with images that have alpha Channel (the image is all black). The goal of the algorithm itself is to turn the weaker color of the image into the color of the Gradient1 and scale to the strongest color of the image with the color of Gradient2.

import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;


public class GradientMap {

    private Color RGB;

    public BufferedImage setGradientMap(BufferedImage image, Color gradient1, Color gradient2) {

        int width = image.getWidth();
        int height = image.getHeight();
        int minRGB = 255;
        int maxRGB = 0;

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                RGB = new Color(image.getRGB(x, y));
                int media = (RGB.getRed() + RGB.getBlue() + RGB.getGreen()) / 3;
                if (media < minRGB) minRGB = media;
                if (media > maxRGB) maxRGB = media;
            }
        }

        Color[] ScaleMap = new Color[maxRGB - minRGB + 1];
        double soma = 255/(maxRGB - minRGB);

        for (int i = 0; i < ScaleMap.length; i++) {
            double color = i * soma;
            ScaleMap[i] = new Color((int) color, (int) color, (int) color);
        }

        System.out.println(minRGB + " " + maxRGB);

        Color[] GradientMap = new Color[256];

        int count = 0;

        for (int i = 0; i < 256; i++) {
            if (i < minRGB || i > maxRGB)
                GradientMap[i] = null;
            else {
                GradientMap[i] = ScaleMap[count];
                count += 1;
            }
        }

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                RGB = new Color(image.getRGB(x, y));
                int media = (RGB.getRed() + RGB.getBlue() + RGB.getGreen()) / 3;
                image.setRGB(x, y, GradientMap[media].getRGB());
            }
        }



        /**       #SATURATION

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                RGB = new Color(image.getRGB(x, y));
                int media = (RGB.getRed() + RGB.getBlue() + RGB.getGreen()) / 3;
                image.setRGB(x, y, new Color(media,media,media).getRGB());
            }
        }

        **/


        return image;
    }
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.