How to use "getHue: saturation: bright: alpha:"?

Here is this method in UIColor in iOS 5:

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

But I do not understand how I should use this in code. Of course, I do not need to specify each of these components if I want to get this from UIColor?

+2
source share
2 answers
CGFloat hue;
CGFloat saturation;
CGFloat brightness;
CGFloat alpha;

[aColor getHue:&hue 
    saturation:&saturation 
    brightness:&brightness 
         alpha:&alpha];    
//now the variables hold the values

getHue:saturation:brightness:alpha: returns a boolean value indicating whether the UIColor could be converted at all.

Example:

BOOL b = [[UIColor colorWithRed:.23 green:.42 blue:.9 alpha:1.0] getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
NSLog(@"%f %f %f %f %d", hue, saturation, brightness, alpha, b);

will record 0.619403 0.744444 0.900000 1.000000 1how it really

while

BOOL b = [[UIColor colorWithPatternImage:[UIImage imageNamed:@"pattern.png"]] getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
NSLog(@"%f %f %f %f %d", hue, saturation, brightness, alpha, b);

magazines 0.000000 0.000000 -1.998918 0.000000 0. The last 0 is Bool, so this is unacceptable, and in fact brightness can only range from 0.0 to 1.0, but here it contains some random nonsense.

Conclusion

The code should be something like

CGFloat hue;
CGFloat saturation;
CGFloat brightness;
CGFloat alpha;

if([aColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]){
    //do what ever you want to do if values are valid
} else {
    //what needs to be done, if converting failed? 
    //Some default values? raising an exception? return?
}
+16

, CGFloat s, . C- . . :

:

CGFloat hue, saturation, brightness, alpha;
[myColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
// hue, saturation, brightness, and alpha are now set
+8

Source: https://habr.com/ru/post/1762751/


All Articles