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

  • This fragile solution does the trick perfectly:

    // 8 symbols.
    String hex1 = Integer.toHexString(myColorPicker.getValue().hashCode()); 
    
    // With # prefix.
    String hex2 = "#" + Integer.toHexString(myColorPicker.getValue().hashCode()); 
    
    // 6 symbols in capital letters.
    String hex3 = Integer.toHexString(myColorPicker.getValue().hashCode()).substring(0, 6).toUpperCase();