@@ -13,6 +13,7 @@ public class ImageUtils
13
13
private static final float ERROR_SEVEN_SIXTEENTHS = 7f /16f ;
14
14
private static final int BLACK = Color .BLACK .getRGB ();
15
15
private static final int WHITE = Color .WHITE .getRGB ();
16
+ private static final int TRANSPARENT = new Color (0 , 0 , 0 , 0 ).getRGB ();
16
17
17
18
private ImageUtils () {}
18
19
@@ -51,7 +52,15 @@ public static void desaturateWithDithering(final BufferedImage image, final floa
51
52
52
53
// Convert to grayscale (or use other method to choose between color1 and color2)
53
54
final int gray = (int )(0.299 * red + 0.587 * green + 0.114 * blue );
54
- final int newPixel = gray <= luminanceByteThreshold && alpha >= alphaByteThreshold ? BLACK : WHITE ;
55
+ final int newPixel ;
56
+ if (alpha >= alphaByteThreshold )
57
+ {
58
+ newPixel = gray <= luminanceByteThreshold ? BLACK : WHITE ;
59
+ }
60
+ else
61
+ {
62
+ newPixel = TRANSPARENT ;
63
+ }
55
64
56
65
final int error = gray - ((newPixel == BLACK ) ? 0 : 255 );
57
66
@@ -92,13 +101,13 @@ public static void desaturateWithHardCut(final BufferedImage image, final float
92
101
{
93
102
final Color pixelColor = new Color (image .getRGB (x , y ), true );
94
103
final float luminance = HSLColor .fromRGB (pixelColor )[2 ];
95
- if (pixelColor .getAlpha () >= alphaByteThreshold && luminance <= luminanceByteThreshold )
104
+ if (pixelColor .getAlpha () >= alphaByteThreshold )
96
105
{
97
- image .setRGB (x , y , BLACK );
106
+ image .setRGB (x , y , luminance <= luminanceByteThreshold ? BLACK : WHITE );
98
107
}
99
108
else
100
109
{
101
- image .setRGB (x , y , WHITE );
110
+ image .setRGB (x , y , TRANSPARENT );
102
111
}
103
112
}
104
113
}
0 commit comments