Java: convert java.awt.Color to javafx.scene.paint.Color
Asked Answered
H

1

11

How can I conver one to another? I thought a way via rgb string, but this case alpha layer is ignored. So the question - how to convert one to another with alpha?

Herring answered 26/5, 2015 at 18:41 Comment(0)
A
13

Get each component from the awt Color object and use the javafx.scene.paint.Color.rgb(...) static method. Note that the awt Color has a getAlpha() method that returns the alpha as an int in the range 0-255, whereas javafx.scene.paint.Color.rgb(...) expects the 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);
Anabiosis answered 26/5, 2015 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.