繁体   English   中英

如何更改JavaFX中a.png图像的颜色?

[英]How to change the color of a .png image in JavaFX?

我有一个这样的PNG图像:

在此处输入图像描述

如何在 JavaFX 中更改颜色?

您可以使用Lighting效果,这是一个示例:

Lighting lighting = new Lighting(new Light.Distant(45, 90, Color.RED));
ColorAdjust bright = new ColorAdjust(0, 1, 1, 1);
lighting.setContentInput(bright);
lighting.setSurfaceScale(0.0);

imageView.setEffect(lighting);

Output:

样本输出

我真的很喜欢 @MS 的解决方案,它使用了 Lighting。 但是,如果您想生成可重用的图像来渲染多个 ImageView 节点,以下是一种可能的解决方案。

请注意,以下解决方案是通过保持透明像素来改变整个图像的颜色。 可能如果您有白色背景图像,您可以相应地调整代码。

public static Image blendColor(final Image sourceImage, final Color blendColor) {
    final double r = blendColor.getRed();
    final double g = blendColor.getGreen();
    final double b = blendColor.getBlue();
    final int w = (int) sourceImage.getWidth();
    final int h = (int) sourceImage.getHeight();
    final WritableImage outputImage = new WritableImage(w, h);
    final PixelWriter writer = outputImage.getPixelWriter();
    final PixelReader reader = sourceImage.getPixelReader();
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            // Keeping the opacity of every pixel as it is.
            writer.setColor(x, y, new Color(r, g, b, reader.getColor(x, y).getOpacity()));
        }
    }
    return outputImage;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM