Javafx - Get RGB Value from Node's Color Fill


Question

In my javafx app, I create a circle and then allow the user to color it in...

Circle circle = new Circle();
circle.setFill(colorPicker.getValue());

Then I need to later fetch the color that the circle is and get the RGB values into hex form (#FFFFFF)

circle.getFill(); //returns a Paint object

How do I get the fill in RGB hex form??

1
8
12/12/2013 12:40:55 AM

Accepted Answer

Try this:

Color c = (Color) circle.getFill();
String hex = String.format( "#%02X%02X%02X",
            (int)( c.getRed() * 255 ),
            (int)( c.getGreen() * 255 ),
            (int)( c.getBlue() * 255 ) );

Hope it helps.

12
12/12/2013 1:06:21 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon