colorshexjavafxrgb

How to get hex web String from JavaFX ColorPicker color?


I have Color chosen in JavaFX ColorPicker. Now I need to save it as hex string. I found this method, but for JavaFX it is not applicable. JavaFX has its own Color class without getRGB() method, that could be used as mediatory convertion.


Solution

  • Translate a color into a web color code:

    public class FxUtils
    {
        public static String toRGBCode( Color color )
        {
            return String.format( "#%02X%02X%02X",
                (int)( color.getRed() * 255 ),
                (int)( color.getGreen() * 255 ),
                (int)( color.getBlue() * 255 ) );
        }
    }