如何在 Java 中使用 HSL 色彩空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2997656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:24:35  来源:igfitidea点击:

How can I use the HSL colorspace in Java?

javacolor-spacehsl

提问by Eric

I've had a look at the ColorSpace class, and found the constant TYPE_HLS(which presumablyis just HSL in a different order).

我查看了 ColorSpace 类,并找到了常量TYPE_HLS大概只是 HSL 的顺序不同)。

Can I use this constant to create a Colorfrom hue, saturation, and luminosity? If not, are there any Java classes for this, or do I need to write my own?

我可以使用这个常数Color从色调、饱和度和亮度创建一个吗?如果没有,是否有任何 Java 类,或者我需要编写自己的类吗?

采纳答案by Eric

回答by Andrei Fierbinteanu

Maybe thiswill help. The JDK doesn't seem to be very helpful when wanting to use colors in another color space.

也许会有所帮助。当想要在另一个颜色空间中使用颜色时,JDK 似乎不是很有帮助。

Edit: In ColorSpace.getName(idx)there's this little snippet:

编辑:在ColorSpace.getName(idx)有这个小片段:

 case ColorSpace.TYPE_HLS:
                    compName = new String[] {"Hue", "Lightness", 
                                             "Saturation"};

so it was what you thought, but looking at the type hierarchy of ColorSpace it doesn't seem to be used or implemented in any way anywhere. ColorSpace is extended by only two other classes BogusColorSpace and ICC_ColorSpace, so I'm guessing they're expecting developers to create their own implementations for different color spaces.

所以这就是你的想法,但是从 ColorSpace 的类型层次结构来看,它似乎没有以任何方式在任何地方使用或实现。ColorSpace 仅由另外两个类 BogusColorSpace 和 ICC_ColorSpace 扩展,所以我猜他们希望开发人员为不同的颜色空间创建自己的实现。

回答by Justin Garrick

EDIT: I realize HSB != HSL, the answer below is for HSB.

编辑:我意识到 HSB != HSL,下面的答案是针对 HSB 的。

I don't think there is any need to use ColorSpaces here. Try something like the following:

我认为这里没有必要使用 ColorSpaces。尝试类似以下内容:

float hue = 0.9f; //hue
float saturation = 1.0f; //saturation
float brightness = 0.8f; //brightness

Color myRGBColor = Color.getHSBColor(hue, saturation, brightness);

回答by Waldemar Wosiński

If your input is swing/awt widgets, then with Java 7 JColorChooseryou can get Color by HSV and HSL spaces. http://java.dzone.com/articles/new-color-chooser-jdk-7

如果您的输入是 Swing/awt 小部件,那么使用Java 7 JColorChooser,您可以通过 HSV 和 HSL 空间获取颜色。http://java.dzone.com/articles/new-color-chooser-jdk-7

回答by Yona Appletree

Most of the given answers here seem to assume that HSL == HSB, which is false. The HSB colorspace is useful (and used) in many cases, but there is one notable exception: CSS. The non-RGB css color functions, hsl() and hsla() are HSL, not HSB. As such, it is very useful to be able to convert to and from HSL in java.

这里给出的大多数答案似乎都假设 HSL == HSB,这是错误的。HSB 色彩空间在许多情况下都很有用(并被使用),但有一个值得注意的例外:CSS。非 RGB css 颜色函数 hsl() 和 hsla() 是 HSL,而不是 HSB。因此,能够在 Java 中与 HSL 相互转换是非常有用的。

There is a good writeup about the problem here: http://tips4java.wordpress.com/2009/07/05/hsl-color/TL;DR: the code is here: http://www.camick.com/java/source/HSLColor.java

这里有一篇关于这个问题的好文章:http: //tips4java.wordpress.com/2009/07/05/hsl-color/TL;DR:代码在这里:http: //www.camick.com/java /source/HSLColor.java

The methods therein are pretty easy to extract if you don't want to use the whole class.

如果您不想使用整个类,那么其中的方法很容易提取。

It doesn't appear that the author of the class included a license, though the context of the blog post seems to imply public domain. Use your own judgement.

尽管博客文章的上下文似乎暗示了公共领域,但该课程的作者似乎并未包含许可证。使用你自己的判断。

回答by xtempore

Here is a simple implementation that will return a Color based on hue, saturation, and lightness values from 0.0 to 1.0...

这是一个简单的实现,它将根据从 0.0 到 1.0 的色调、饱和度和亮度值返回一个颜色...

static public Color hslColor(float h, float s, float l) {
    float q, p, r, g, b;

    if (s == 0) {
        r = g = b = l; // achromatic
    } else {
        q = l < 0.5 ? (l * (1 + s)) : (l + s - l * s);
        p = 2 * l - q;
        r = hue2rgb(p, q, h + 1.0f / 3);
        g = hue2rgb(p, q, h);
        b = hue2rgb(p, q, h - 1.0f / 3);
    }
    return new Color(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255));
}

EDIT by Yona-Appletree:

Yona-Appletree 编辑:

I found what I think is the correct hue2rgb function and tested it as working:

我发现我认为是正确的hue2rgb 函数并测试它是否有效:

private static float hue2rgb(float p, float q, float h) {
    if (h < 0) {
        h += 1;
    }

    if (h > 1) {
        h -= 1;
    }

    if (6 * h < 1) {
        return p + ((q - p) * 6 * h);
    }

    if (2 * h < 1) {
        return q;
    }

    if (3 * h < 2) {
        return p + ((q - p) * 6 * ((2.0f / 3.0f) - h));
    }

    return p;
}