image util: add conditional recolorImage method

This commit is contained in:
Twiglet1022
2019-06-09 00:17:50 +01:00
parent 72a7e05eac
commit 0de7cbca4c

View File

@@ -405,6 +405,36 @@ public class ImageUtil
return filledImage;
}
/**
* Recolors pixels of the given image with the given color based on a given recolor condition
* predicate.
*
* @param image The image which should have its non-transparent pixels recolored.
* @param color The color with which to recolor pixels.
* @param recolorCondition The condition on which to recolor pixels with the given color.
* @return The given image with all pixels fulfilling the recolor condition predicate
* set to the given color.
*/
public static BufferedImage recolorImage(final BufferedImage image, final Color color, final Predicate<Color> recolorCondition)
{
final BufferedImage recoloredImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < recoloredImage.getWidth(); x++)
{
for (int y = 0; y < recoloredImage.getHeight(); y++)
{
final Color pixelColor = new Color(image.getRGB(x, y), true);
if (!recolorCondition.test(pixelColor))
{
recoloredImage.setRGB(x, y, image.getRGB(x, y));
continue;
}
recoloredImage.setRGB(x, y, color.getRGB());
}
}
return recoloredImage;
}
/**
* Performs a rescale operation on the image's color components.
*