Java: convert java.awt.Color to javafx.scene.paint.Color

How can I convert one to another? I thought the path was through the rgb line, but this case the alpha layer is ignored. So the question is how to convert one to another with alpha?

+7
java javafx awt
source share
1 answer

Get each component from the awt Color object and use the static method javafx.scene.paint.Color.rgb(...) . Note that awt Color has a getAlpha() method that returns alpha as an int in the range 0-255 , while javafx.scene.paint.Color.rgb(...) expects an alpha value as a double in the range 0.0-1.0 :

 java.awt.Color awtColor = ... ; int r = awtColor.getRed(); int g = awtColor.getGreen(); int b = awtColor.getBlue(); int a = awtColor.getAlpha(); double opacity = a / 255.0 ; javafx.scene.paint.Color fxColor = javafx.scene.paint.Color.rgb(r, g, b, opacity); 
+10
source share

All Articles