如何在 JavaFX 中获取像素的 RGB 值

发布于 2024-07-14 22:14:06 字数 233 浏览 2 评论 0原文

我正在开始使用 JavaFX,基本上我想要实现的是一个颜色选择器。 起初,我想到了一个带有 LinearGradient 的矩形,它可以穿过所有原色/次要颜色。

看起来像我想要的,但问题是我无法获取此节点中给定坐标(x,y)处的 RGB 值。 我知道你可以通过任何形状的填充属性来做到这一点,如果它是颜色的话。

但是是否有办法获取 LinearGradient/Paint 内任何内容的 RGB 值?

I am getting started with JavaFX and basically what I am trying to implement is a Color Picker.
At first, I thought of having a rectangle with a LinearGradient that goes through all primary/secondary colors.

Looks like what I want, but the problem is that I can not get the RGB values at a given coordinate(x,y) in this Node.
I know you can do it through the fill property of any Shape IF it is a Color.

But Is there anyway to get the RGB values of anything inside a LinearGradient/Paint ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水微香 2024-07-21 22:14:06

这个 ColorPicker JavaFX 示例有帮助吗?

[...]

function colorAtLocation(x:Integer, y:Integer) : Color {
    var bimg = iv.image.bufferedImage;
    if (x < 0 or x >= bimg.getWidth() or y < 0 or y >= bimg.getHeight()) {
        return null;
    }
    var rgb = bimg.getRGB(x, y);
    var r = Bits.bitAnd(Bits.shiftRight(rgb, 16), 0xff);
    var g = Bits.bitAnd(Bits.shiftRight(rgb,  8), 0xff);
    var b = Bits.bitAnd(Bits.shiftRight(rgb,  0), 0xff);
    Color.rgb(r, g, b)
}

function updateSelectedColor(e:MouseEvent) {
    var rgb = colorAtLocation(e.x, e.y);
    if (rgb != null) {
        picker.selectedColor = rgb;
    }
}

[...]

Does this ColorPicker JavaFX example help?

[...]

function colorAtLocation(x:Integer, y:Integer) : Color {
    var bimg = iv.image.bufferedImage;
    if (x < 0 or x >= bimg.getWidth() or y < 0 or y >= bimg.getHeight()) {
        return null;
    }
    var rgb = bimg.getRGB(x, y);
    var r = Bits.bitAnd(Bits.shiftRight(rgb, 16), 0xff);
    var g = Bits.bitAnd(Bits.shiftRight(rgb,  8), 0xff);
    var b = Bits.bitAnd(Bits.shiftRight(rgb,  0), 0xff);
    Color.rgb(r, g, b)
}

function updateSelectedColor(e:MouseEvent) {
    var rgb = colorAtLocation(e.x, e.y);
    if (rgb != null) {
        picker.selectedColor = rgb;
    }
}

[...]
妄断弥空 2024-07-21 22:14:06

ColorPicker JavaFX 示例以 png 图像开始,该图像加载到 image,然后填充 ImageView

问题从包含 LinearGradient 的 JavaFX Rectangle 开始。

要将矩形内容放入缓冲图像中,可以使用 java.awt.Robot:

        var rectangle = new java.awt.Rectangle(x,y,width,height);
        var robot = new java.awt.Robot();
        var bufferedImage = robot.createScreenCapture(rectangle);   

其中 rectangle 将描述包含感兴趣位的 JavaFX 矩形的坐标。

robot.createScreenCapture 调用有一个问题,即要进行屏幕捕获,屏幕必须可见。 应该有更好的方法来填充缓冲图像,但我还没有遇到过。

The ColorPicker JavaFX example starts with a png image that is loaded to an image that then populates the ImageView.

The question starts with a JavaFX Rectangle containing LinearGradient.

To get the rectangle contents into a buffered image, one can use the java.awt.Robot:

        var rectangle = new java.awt.Rectangle(x,y,width,height);
        var robot = new java.awt.Robot();
        var bufferedImage = robot.createScreenCapture(rectangle);   

where rectangle would be describe the coordinates of the JavaFX Rectangle containing the bits of interest.

The robot.createScreenCapture call has the gotcha that to do the screen capture, the screen has to be visible. There should be a better of way to populate the buffered image but I've not yet encountered it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文