Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there function to convert UIColor to Hue Saturation Brightness?

Tags:

i can set uicolor with RGB values:

[UIColor colorWithRed:0.53 green:0.37 blue:0.11 alpha:1.00]; 

i can set uicolor with hsb values:

[UIColor colorWithHue:0.10 saturation:0.16 brightness:0.13 alpha:1.00]; 

i also could convert it back to RGB:

CGFloat* colors = CGColorGetComponents(Color1.CGColor); 

But how i can get HSB from uicolor?

like image 642
Horhe Garcia Avatar asked Apr 09 '12 10:04

Horhe Garcia


People also ask

Can be hue brightness and saturation?

"Hue" differs slightly from "color" because a color can have saturation or brightness as well as a hue. Saturation is the intensity of a hue from gray tone (no saturation) to pure, vivid color (high saturation).

What color mode uses hue saturation and brightness?

HSB is an alternative color space to RGB. In this model, colors are described by their hue, saturation, and brightness: Hue is expressed in degrees, from red(0), through all the colors around the color wheel, and back to red (360).

Is hue the same as brightness?

Hue is therefore the actual color. Brightness refers to how much white (or black) is mixed in the color while Saturation indicates the amount of grey in a color.

What is hue saturation brightness and luminance?

– Hue represents the actual color that is shown. – Saturation represents how deep the color is, going from grey (completely desaturated) to a very deep shade of the color (oversaturated). – Luminance is how much white or black is mixed into the color.


2 Answers

Use the UIColor method: getHue:saturation:brightness:alpha:

From the Apple docs:
"Returns the components that make up the color in the HSB color space."

- (BOOL)getHue:(CGFloat *)hue saturation:(CGFloat *)saturation brightness:(CGFloat *)brightness alpha:(CGFloat *)alpha 

Example:

UIColor *testColor = [UIColor colorWithRed:0.53 green:0.37 blue:0.11 alpha:1.00];  CGFloat hue; CGFloat saturation; CGFloat brightness; CGFloat alpha; BOOL success = [testColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]; NSLog(@"success: %i hue: %0.2f, saturation: %0.2f, brightness: %0.2f, alpha: %0.2f", success, hue, saturation, brightness, alpha); 

NSLog output:

success: 1 hue: 0.10, saturation: 0.79, brightness: 0.53, alpha: 1.00

Here is a corrected version of the method provided by @WhiteTiger:

// Test values CGFloat red = 0.53; CGFloat green = 0.37; CGFloat blue = 0.11;  CGFloat hue = 0; CGFloat saturation = 0; CGFloat brightness = 0;  CGFloat minRGB = MIN(red, MIN(green,blue)); CGFloat maxRGB = MAX(red, MAX(green,blue));  if (minRGB==maxRGB) {     hue = 0;     saturation = 0;     brightness = minRGB; } else {     CGFloat d = (red==minRGB) ? green-blue : ((blue==minRGB) ? red-green : blue-red);     CGFloat h = (red==minRGB) ? 3 : ((blue==minRGB) ? 1 : 5);     hue = (h - d/(maxRGB - minRGB)) / 6.0;     saturation = (maxRGB - minRGB)/maxRGB;     brightness = maxRGB; } NSLog(@"hue: %0.2f, saturation: %0.2f, value: %0.2f", hue, saturation, brightness); 

NSLog output:

hue: 0.10, saturation: 0.79, value: 0.53

like image 162
zaph Avatar answered Sep 29 '22 05:09

zaph


Here is a nice way of using Swift features (extensions, computed properties and tuples) to do the same thing in just a few lines of code.

extension UIColor {   var hsba: (h: CGFloat, s: CGFloat, b: CGFloat, a: CGFloat) {     var hsba: (h: CGFloat, s: CGFloat, b: CGFloat, a: CGFloat) = (0, 0, 0, 0)     self.getHue(&(hsba.h), saturation: &(hsba.s), brightness: &(hsba.b), alpha: &(hsba.a))     return hsba   } } 

Swift 3.2 / 4 minor update

Swift 3.2 / 4 enforced a new warning triggered with the previous code because you were modifying the hsba variable several times within the same call to getHue

Simultaneous accesses to parameter 'hsba', but modification requires exclusive access; consider copying to a local variable.

extension UIColor {     var hsba: (h: CGFloat, s: CGFloat, b: CGFloat, a: CGFloat) {         var h: CGFloat = 0, s: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0         self.getHue(&h, saturation: &s, brightness: &b, alpha: &a)         return (h: h, s: s, b: b, a: a)     } } 
like image 39
apouche Avatar answered Sep 29 '22 07:09

apouche