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 throws white to gray, causing the saturation to stand -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 needed was the gradient map, because it leaves the white whiter and darker dark, 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 not found anything to do with this effect in Java.
Below is an image of what I mean.
AtthemomentIwasabletocreateanalgorithmthatonlydoestheGradientMapfromWhitetoBlackandapparentlyitisnotworkingwithimagesthathavealphachannel(theimageisallblack).ThegoalofthealgorithmistotransformthefaintestcoloroftheimageintothecolorofGradient1
andscaletothestrongestcoloroftheimagewiththecolorofGradient2
.
importjava.awt.Color;importjava.awt.Image;importjava.awt.image.BufferedImage;publicclassGradientMap{privateColorRGB;publicBufferedImagesetGradientMap(BufferedImageimage,Colorgradient1,Colorgradient2){intwidth=image.getWidth();intheight=image.getHeight();intminRGB=255;intmaxRGB=0;for(intx=0;x<width;x++){for(inty=0;y<height;y++){RGB=newColor(image.getRGB(x,y));intmedia=(RGB.getRed()+RGB.getBlue()+RGB.getGreen())/3;if(media<minRGB)minRGB=media;if(media>maxRGB)maxRGB=media;}}Color[]ScaleMap=newColor[maxRGB-minRGB+1];doublesoma=255/(maxRGB-minRGB);for(inti=0;i<ScaleMap.length;i++){doublecolor=i*soma;ScaleMap[i]=newColor((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;
}
}