Objective-C RGB to HSB

Let’s say I’ve got the colour FF0000, which is red. Finding a darker colour is easy, I just type maybe CC instead of the FF, but let’s say I’ve got the colour AE83FC, which is a complicated colour, how the heck would I find a lighter or darker version of it automatically?

I figured the easy way to do this is to convert my RGB to HSB [Hue, Saturation, Brightness]

  • How do I pick the color in hexadecimal form or RGB form instead of using the colors given to me in Swift
  • How to get the RGB Code (INT) from an UIColor in Swift
  • How do I create a UIColor from RGBA?
  • Need help with YUV display in OpenGl
  • UIColor CMYK and Lab Values?
  • Extract RGB Values From UIColor
  • How would I do that in Objective-C?

    Let’s say I’ve got a RGB which is: 1.0, 0.0, 0.0. That’s red.

    CGFloat r = 1.0;
    CGFloat g = 0.0;
    CGfloat b = 0.0;

    How would I convert that to HSB and then transform the colors and make it go back to RGB to I can use CGContextRGBSetFillColor?

    Are there any HSB functions?

    Please help. 🙂

    2 Solutions Collect From Internet About “Objective-C RGB to HSB”

    First, please be aware that three numbers don’t describe a color, three numbers together with a colorspace do. RGB isn’t a colorspace, it’s what’s called a color model. There are lots of colorspaces with the RGB model. So ((1,0,0), sRGB) is a different color than ((1,0,0), Adobe RGB). Are you aware of how string encodings work, where a bunch of bytes by itself is not a string? It’s a lot like that. It’s also similar in that you’re kind of asking for trouble whenever you want to look at the component values, because it’s an opportunity for messing up the colorspace handling.

    Sorry, cannot help myself. Anyway, I’ll answer the question as if your original color was ((1,0,0), Generic RGB).

    NSColor *color = [NSColor colorWithCalibratedRed:1 green:0 blue:0 alpha:1];
    NSLog(@"hue! %g saturation! %g brightness! %g, [color hueComponent], [color saturationComponent], [color brightnessComponent]);

    and on the other hand,

    NSColor *color = [NSColor colorWithCalibratedHue:h saturation:s brightness:b alpha:1];
    NSLog(@"red! %g green! %g blue! %g, [color redComponent], [color greenComponent], [color blueComponent]);

    These fooComponent methods do not work with all colors, only those in two specific colorspaces, see docs. You already know you’re good if you created the colors yourself with the methods above. If you have a color of unknown provenance, you can (attempt to) convert it to a colorspace in which you can use those component methods with -[NSColor colorUsingColorspaceName:].

    There is no need to go all the way to HSB for this. If you want to find a darker version of a color, just multiply each R, G and B component with a number between 0 and 1, and you will get the same color, but darker.

    Example: half intensity of AE83FC:

      {0xAE, 0x83, 0xFC} * 0.5 =
      {174, 131, 252} * 0.5 =
      {87, 65, 126} =
      {0x57, 0x41, 0x7E} => 57417E

    In the same way, you can obtain brighter versions by multiplying with anything >1. The value of each component can’t be larger than 255, so when that happens, you need to limit it to 255. That means that the color wont be exactly a brighter version of the same color, but probably close enough for your purposes.