How to access UIColor color components?

i.e. I want to know the meaning of blue. How can I get this from UIColor?

+4
source share
3 answers

The UIColor class does not provide information about color components. Instead, you should get the components from your CGColor. Note that the number of components depends on the color space used in CGColorRef.

This code prints the components for blue:

UIColor* color = [UIColor blueColor]; int n = CGColorGetNumberOfComponents(color.CGColor); const CGFloat *coms = CGColorGetComponents(color.CGColor); for (int i = 0; i < n; ++i) NSLog(@"%f", coms[i]); 
+6
source

I was just looking for this problem this morning. I do not know why UIColor is so incomplete compared to NSColor. Anyway, I found this useful category for UIColor: Access to UIColor components

0
source

Just made a category for it.

 NSLog(@"%f", [UIColor blueColor].blue); // 1.000000 

There is something like:

 typedef enum { R, G, B, A } UIColorComponentIndices; @implementation UIColor (EPPZKit) -(CGFloat)red { return CGColorGetComponents(self.CGColor)[R]; } -(CGFloat)green { return CGColorGetComponents(self.CGColor)[G]; } -(CGFloat)blue { return CGColorGetComponents(self.CGColor)[B]; } -(CGFloat)alpha { return CGColorGetComponents(self.CGColor)[A]; } @end 

Part of eppz!kit with more UIColor goodies .

0
source

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


All Articles