6

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?

treeba
  • 469
  • 5
  • 10

4 Answers4

6

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

Matteo Alessani
  • 10,264
  • 4
  • 40
  • 57
4

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.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • +1, though HSL isn't quite the same as HSB/HSV. The H and S should be the same, though. – Wevah Jun 10 '10 at 19:09
  • @Wevah, wikipedia says it's not very standardized, and that any of the names can go with any of the models. I'm not enough of an expert to know the subtleties though. – Carl Norum Jun 10 '10 at 19:15
  • Well, they're *sort* of standard. Almost. Mostly it's just unfortunate that the built-in stuff doesn't handle HSL(ightness). :/ And if he really does want HSV/HSB, then your answer is perfect! – Wevah Jun 10 '10 at 19:24
  • There is indeed a UIColor! (I missed the `iphone` tag, too.) – Wevah Jun 10 '10 at 19:28
  • Thanks Carl, I tried using UIColor which got me a little closer but I get the error 'UIColor may not respond to -getHue:saturation:brightness:alpha' – treeba Jun 10 '10 at 19:43
  • The HUE is the only similar value in HSB and HSL. The S (Saturation) are not calculated the same. – Ryan Gibbons Feb 14 '11 at 05:06
  • -getHue:saturation:brightness:alpha is from available starting with iOS5 – pchap10k Jul 11 '12 at 08:14
3

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);
Nathan S.
  • 5,244
  • 3
  • 45
  • 55
Berk
  • 367
  • 2
  • 7
  • Thanks! Runs much more quickly than using `NSColor`. – VinceFior Jul 04 '15 at 22:34
  • Update: This function actually returns different hue values than I think it should. When I use it to select different hue ranges from a color wheel, it jumps around. In contrast, the following solution rotates around the color wheel as I'd expect: http://stackoverflow.com/a/6930407/1976584 – VinceFior Jul 05 '15 at 03:55
0

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.

progrmr
  • 75,956
  • 16
  • 112
  • 147