Issue
I have a grayscale image, this image is set to a rectangle with an ImagePattern. like this:
ImagePattern imagePattern = new ImagePattern(new Image("File:resources/images/image.png"));
Rectangle rectangle = new Rectangle();
rectangle.setFill(imagePattern);
The only problem is I want the user to choose the colour of the image, so I want to change the hue of the image.
I found following question on StackOverflow, the first answer https://stackoverflow.com/a/18124868/15277155 shows how a coloured image is changed to a red image.
The only problem I have is that answer is done with an ImageView instead of an Imagepattern. Is there any way This can be done with an ImagePattern. Or that I can place the ImageView inside a rectangle?
Based on @jewelsea's comment this is the code I have.
ImagePattern imagePattern = new ImagePattern(new
Image("File:resources/images/image.png"));
Rectangle rectangle = new Rectangle();
rectangle.setFill(imagePattern);
ColorAdjust colorAdjust = new ColorAdjust();
// define target color
Color targetColor = Color.GREEN;
double hue = map( (targetColor.getHue() + 180) % 360, 0, 360, -1, 1);
colorAdjust.setHue(hue);
// use saturation as it is enter code here
double saturation = targetColor.getSaturation();
colorAdjust.setSaturation(saturation);
double brightness = map( targetColor.getBrightness(), 0, 1, -1, 0);
colorAdjust.setBrightness(brightness);
// apply color adjustment
rectangle.setEffect(colorAdjust);
rectangle.setFill(imagePattern);
I tested this on a yellow image (PNG with transparent background, but no opacity) and it worked.
Then I tried it on an image with only colors ranging from white to black (and grey) (also a PNG with transparent background, but also with opacity in the colors) and it didn't change those colors.
Solution
The ColorAdjust effect will work with any node, not just an ImageView.
So you can apply the ColorAdjust effect to your rectangle, which is filled with the ImagePattern, and the effect will be applied to the ImagePattern.
Answered By - jewelsea