以矩形为容器更改图像模式的色调比例

Change hue scale of an imagepattern with a rectangle as container

我有一个灰度图像,这个图像被设置为一个带有 ImagePattern 的矩形。像这样:

 ImagePattern imagePattern = new ImagePattern(new Image("File:resources/images/image.png"));
 Rectangle rectangle = new Rectangle();
 rectangle.setFill(imagePattern);

唯一的问题是我想让用户选择图像的颜色,所以我想改变图像的色调。

我在 Whosebug 上发现了以下问题,第一个答案 显示如何将彩色图像更改为红色图像。

我遇到的唯一问题是答案是使用 ImageView 而不是 Imagepattern 完成的。 有什么办法可以用 ImagePattern 来完成。 或者我可以将 ImageView 放在一个矩形内?

根据@jewelsea 的评论,这是我的代码。

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);

我在黄色图像(具有透明背景但不透明的 PNG)上对此进行了测试并且有效。

然后我在只有从白色到黑色(和灰色)的颜色的图像上尝试了它(也是具有透明背景的 PNG,但颜色也不透明)并且它没有改变这些颜色。

ColorAdjust 效果适用于任何节点,而不仅仅是 ImageView。

因此,您可以将 ColorAdjust 效果应用到填充有 ImagePattern 的矩形,效果将应用到 ImagePattern。