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

发布于 2024-09-04 22:26:04 字数 155 浏览 3 评论 0原文

我查看了 ColorSpace 类,发现了常量 TYPE_HLS(可能只是不同顺序的 HSL)。

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

坚持沉默 2024-09-11 22:26:04

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

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

我已经创建了一个要点备份,以防博客崩溃:https://gist.github.com/Yona-Appletree/0c4b58763f070ae8cdff7db583c82563

如果你不这样做的话,其中的方法很容易提取不想使用整个班级。

许可证

该代码似乎属于公共领域,如博客的“关于”页面所示 (https://tips4java.wordpress.com/about/):

We assume no responsibility for the code. You are free to use and/or modify and/or distribute any or all code posted on the Java Tips Weblog without restriction. A credit in the code comments would be nice, but not in any way mandatory.

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.

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

I have created a gist backup, should the blog ever go down: https://gist.github.com/Yona-Appletree/0c4b58763f070ae8cdff7db583c82563

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

License

The code appears to be in the public domain, as noted on the "About" page of the blog (https://tips4java.wordpress.com/about/):

We assume no responsibility for the code. You are free to use and/or modify and/or distribute any or all code posted on the Java Tips Weblog without restriction. A credit in the code comments would be nice, but not in any way mandatory.
匿名的好友 2024-09-11 22:26:04

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

我认为这里没有必要使用ColorSpaces。尝试如下操作:

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

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

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

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

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

Color myRGBColor = Color.getHSBColor(hue, saturation, brightness);
青巷忧颜 2024-09-11 22:26:04

这是一个简单的实现,它将返回基于从 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));
}

由 Yona-Appletree 编辑:

我找到了我认为正确的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;
}

Here is a simple implementation that will return a Color based on hue, saturation, and lightness values from 0.0 to 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:

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

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;
}
如梦初醒的夏天 2024-09-11 22:26:04

我找到了HSB的内置方法(与HSL不一样,但类似)

[Color.getHSBColor(float h, float s, float b)](http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#getHSBColor(float,%20float,%20float))

I found the built-in method for HSB (which is not the same as HSL, but is similar)

[Color.getHSBColor(float h, float s, float b)](http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#getHSBColor(float,%20float,%20float))
何以畏孤独 2024-09-11 22:26:04

也许会有所帮助。当想要使用其他颜色空间中的颜色时,JDK 似乎没有太大帮助。

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

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

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

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

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

 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.

智商已欠费 2024-09-11 22:26:04

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文