简体   繁体   English

JColorChooser将Hue,Saturation和Value表示为整数。 如何从Color对象获取这些值?

[英]The JColorChooser represents Hue, Saturation, and Value as integers. How do I get these values from a Color object?

I am using a Java Swing JColorChooser in the HSV color space. 我在HSV颜色空间中使用Java Swing JColorChooser。 This widget uses spinners to adjust the color. 此小部件使用微调器调整颜色。 Hue is 0-360, Saturation is 0-100, and Value is 0-100. 色相是0-360,饱和度是0-100,值是0-100。 I am only able to get float values back though for the component values. 我只能获取组件值的浮点值。 I want to display the component values in a label after the user selects a color, but I can't figure out how to get the same values as are in the JColorChooser. 我想在用户选择一种颜色后在标签中显示组件值,但是我不知道如何获得与JColorChooser中相同的值。 My code: 我的代码:

private String getColorString(Color color)
{
    float[] comp = color.getColorComponents(chooser.getColorModel().getColorSpace(),
                                            null);

    return comp[0] + ", " + comp[1] + ", " + comp[2];
}

When my color chooser shows an HSV of 180,50,50 my component values are 0.24938,0.49749,0.49793 当我的颜色选择器显示HSV为180,50,50时,我的成分值为0.24938,0.49749,0.49793

I realize that I am requesting a float array from the color, but there are no methods such as getHue(). 我意识到我要从颜色请求一个float数组,但是没有诸如getHue()这样的方法。

调用Color.RGBtoHSB()使用的RGB分量Color从选择器获得,如所示在这里

To get HSB (same as HSV) from jColorChooser you can use Color.RGBtoHSB() in the following way. 要从jColorChooser获取HSB(与HSV相同),可以按以下方式使用Color.RGBtoHSB()。

Color c = jColorChooser1.getColor();
float[] comp = new float[3];
Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), comp);
comp[0]*= 360;
comp[1]*= 100;
comp[2]*= 100;
return  comp[0]+", "+comp[1]+", "+comp[2];

or in your method you can implement it like this 或者在您的方法中,您可以像这样实现它

private String getColorString(Color color)
{
    float[] comp = new float[3];
    Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), comp);
    comp[0]*= 360;
    comp[1]*= 100;
    comp[2]*= 100;
    return  comp[0]+", "+comp[1]+", "+comp[2];
}

I know that there is a small difference in the value we give and in the value which is returned but you cannot go accurate more than this! 我知道我们提供的值与返回的值之间存在很小的差异,但您不能再准确地做到这一点!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM