What returns CGColorGetComponents ()?

CGFloat* colors = CGColorGetComponents(hsbaColor.CGColor); 

Does this return a float or an array of floats? The asterisk seems to be short for array creation. It is right?

When I call this function in the CGColor property of a HSBC UIColor object, does it convert the values ​​to RGB?

+7
iphone cocoa-touch core-graphics uicolor cgcolor
Apr 27 '09 at 2:48
source share
5 answers

Yes, it returns an array of CGFloats . In particular, it returns "an array of intensity values ​​for color components (including alpha) associated with the specified color."

The color components returned depend on which color space is passed to CGColorRef .

More information can be found in the CGColor Documentation .

+4
Apr 27 '09 at 2:58
source share
 CGFloat* colors = CGColorGetComponents(hsbaColor.CGColor); 

Does this return a float or an array of floats? The asterisk seems to be short for array creation. It is right?

Sorting.

CGFloat *colors declares a variable containing a pointer to at least one CGFloat. CGColorGetComponents returns a pointer to several CGFloats, one after another - an array of C. You take this pointer and assign it (insert the pointer) into the colors variable.

A variable declaration does not create an array. In fact, and CGColorGetComponents . No matter which created object, CGColor created the array and saved it inside the object; CGColorGetComponents allows CGColorGetComponents to specify a pointer to this repository.

Declaring a CGFloat *colors variable only creates a place β€” a variable β€” to hold a pointer to one or more CGFloats. The thing in the variable is a pointer, and the thing in this pointer is an array.

If this is still unclear, see Everything You Need to Know About Pointers in C.

+5
Apr 27 '09 at 9:55
source share

From Apple :

It returns the values ​​of the color components (including alpha) associated with the color of quartz. An array of intensity values ​​for color components (including alpha) associated with the specified color. The size of the array is more than the number of color space components for the color.

+1
Apr 27 '09 at 2:57
source share

To get the RGB components, I use:

 // Get UIColor RGB normalized components (0..1) CGFloat red, green, blue, alpha; [color getRed:&red green:&green blue:&blue alpha:&alpha]; // Convert RGB components to 8-bit values (0..255) int r = (int)(red * 255.0); int g = (int)(green * 255.0); int b = (int)(blue * 255.0); 
0
Feb 01 '17 at 4:09 on
source share

Here is an example of how you can correctly convert CGColorRef myColorRef to NSColor myNSColor :

 NSColorSpace *cp = [[NSColorSpace alloc] initWithCGColorSpace:CGColorGetColorSpace(myColorRef)]; const CGFloat *components = CGColorGetComponents(myColorRef); size_t componentCount = CGColorGetNumberOfComponents(myColorRef); NSColor* myNSColor = [NSColor colorWithColorSpace:cp components:components count:componentCount]; 
0
May 11 '17 at 5:11
source share



All Articles