2

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

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

但是无论如何要获得 LinearGradient/Paint 内任何东西的 RGB 值吗?

4

2 回答 2

1

这个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;
    }
}

[...]
于 2009-03-13T08:09:18.493 回答
0

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.

于 2009-03-30T19:54:22.630 回答