tags:

views:

366

answers:

2

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 ?

+1  A: 

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

[...]
schnaader
The image.bufferedImage no longer works with JavaFX 1.2.One might be able to do this: var bimg = iv.image.platformImage as java.awt.image.BufferedImage;
Refactor
A: 
Refactor