使用 Objective C 从 RGB 转换为 HSL

发布于 2024-09-05 08:05:55 字数 101 浏览 11 评论 0原文

我对 Objective C 很陌生,但已经编程了一段时间了。我开始创建一个函数,可以从 RGB 转换为 HSL,然后再转换回来,但我感觉它太长并且方向错误。有谁知道执行此转换的简单方法?

I'm quite new to objective c but have been programming for a while. I started creating a function that would convert from RGB to HSL and back again but I get a feeling it is way too long and headed in the wrong direction. Does anyone know of a simple way to perform this conversion?

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

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

发布评论

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

评论(4

蔚蓝源自深海 2024-09-12 08:06:06

您可以将 UIColor-HSVAdditions.h/.m 类别添加到您的应用程序中,以向 UIColor 添加一组操作来处理色调、饱和度和值。请参阅 http://bravobug.com/news/?p=448

还有 ArsTechnica 文章。

You can add the UIColor-HSVAdditions.h/.m category to your app to add a set of operations to UIColor for working with hue, saturation and value. See http://bravobug.com/news/?p=448 and
this
ArsTechnica article also.

木有鱼丸 2024-09-12 08:06:05

这是我正在使用的:

 static void RVNColorRGBtoHSL(CGFloat red, CGFloat green, CGFloat blue, CGFloat *hue, CGFloat *saturation, CGFloat *lightness)
{
    CGFloat r = red / 255.0f;
    CGFloat g = green / 255.0f;
    CGFloat b = blue / 255.0f;

    CGFloat max = MAX(r, g);
    max = MAX(max, b);
    CGFloat min = MIN(r, g);
    min = MIN(min, b);

    CGFloat h;
    CGFloat s;
    CGFloat l = (max + min) / 2.0f;

    if (max == min) {
        h = 0.0f;
        s = 0.0f;
    }

    else {
        CGFloat d = max - min;
        s = l > 0.5f ? d / (2.0f - max - min) : d / (max + min);

        if (max == r) {
            h = (g - b) / d + (g < b ? 6.0f : 0.0f);
        }

        else if (max == g) {
            h = (b - r) / d + 2.0f;
        }

        else if (max == b) {
            h = (r - g) / d + 4.0f;
        }

        h /= 6.0f;
    }

    if (hue) {
        *hue = roundf(h * 255.0f);
    }

    if (saturation) {
        *saturation = roundf(s * 255.0f);
    }

    if (lightness) {
        *lightness = roundf(l * 255.0f);
    }
}

以下是如何调用它:

CGFloat h, s, l;
RVNColorRGBtoHSL(r, g, b,
                 &h, &s, &l);

Here's what I'm using:

 static void RVNColorRGBtoHSL(CGFloat red, CGFloat green, CGFloat blue, CGFloat *hue, CGFloat *saturation, CGFloat *lightness)
{
    CGFloat r = red / 255.0f;
    CGFloat g = green / 255.0f;
    CGFloat b = blue / 255.0f;

    CGFloat max = MAX(r, g);
    max = MAX(max, b);
    CGFloat min = MIN(r, g);
    min = MIN(min, b);

    CGFloat h;
    CGFloat s;
    CGFloat l = (max + min) / 2.0f;

    if (max == min) {
        h = 0.0f;
        s = 0.0f;
    }

    else {
        CGFloat d = max - min;
        s = l > 0.5f ? d / (2.0f - max - min) : d / (max + min);

        if (max == r) {
            h = (g - b) / d + (g < b ? 6.0f : 0.0f);
        }

        else if (max == g) {
            h = (b - r) / d + 2.0f;
        }

        else if (max == b) {
            h = (r - g) / d + 4.0f;
        }

        h /= 6.0f;
    }

    if (hue) {
        *hue = roundf(h * 255.0f);
    }

    if (saturation) {
        *saturation = roundf(s * 255.0f);
    }

    if (lightness) {
        *lightness = roundf(l * 255.0f);
    }
}

And here's how to call it:

CGFloat h, s, l;
RVNColorRGBtoHSL(r, g, b,
                 &h, &s, &l);
温柔女人霸气范 2024-09-12 08:06:02

我想你可以使用 NSColor 。

CGFloat r, g, b, a, h, s, b, a2;
NSColor *c = [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
[c getHue:&h saturation:&s brightness:&b alpha:&a2];

再想一想,我不知道 NSColor 在 iPhone 框架中是否可用 - 没有 UIColor 吗?不管怎样,我会留下这个答案,以防有人在寻找 OS X 解决方案时出现在这里。

You can use NSColor, I think.

CGFloat r, g, b, a, h, s, b, a2;
NSColor *c = [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
[c getHue:&h saturation:&s brightness:&b alpha:&a2];

On second thought, I don't know if NSColor is available in the iPhone frameworks or not - isn't there a UIColor? Anyway I'll leave this answer in case someone searching for an OS X solution ends up here.

尾戒 2024-09-12 08:06:00

iPhone SDK 中缺少 NSColor。您可以使用此实用程序在 RGB 空间和 HSL 空间之间进行相互转换:

https://github.com/alessani/ColorConverter< /a>

NSColor is missing in iPhone SDK. You can use this utility to convert from RGB to HSL space and back:

https://github.com/alessani/ColorConverter

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