如何从Color变量中获取RGB值

时间:2014-05-05 12:07:19

标签: java colors rgb

JColorChooser.showDialog(tEkran, "Select a Color", selectedColorBG);

选择一种颜色后,我需要将其转移到3变量。像这样:

colorR = selectedColorBG.getR

colorG = selectedColorBG.getG

colorB = selectedColorBG.getB

有没有办法做到这一点?

3 个答案:

答案 0 :(得分:4)

JColorChooser有一个getColor方法,返回Color,其中分别包含方法getRedgetGreengetBlue

答案 1 :(得分:1)

您必须处理返回值:

Color selectedColor = JColorChooser.showDialog(tEkran, "Select the color", initialColor);
int red = selectedColor.getRed();
int green = selectedColor.getGreen();
int blue = selectedColor.getBlue();

答案 2 :(得分:0)

查看java.awt.Color documentation会回答您的问题:

int getAlpha()
    Returns the alpha component in the range 0-255.
int getBlue()
    Returns the blue component in the range 0-255 in the default sRGB space.
int getGreen()
    Returns the green component in the range 0-255 in the default sRGB space.
int getRed()
    Returns the red component in the range 0-255 in the default sRGB space.