views:

2031

answers:

3
CGFloat* colors = CGColorGetComponents(hsbaColor.CGColor);

Does this return a float, or an array of floats? It looks like the asterisk is shorthand for creating an array. Is that correct?

When I call this function on the CGColor property of an HSB UIColor object does it convert the values to RGB?

A: 

From Apple:

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

Lucas McCoy
+1  A: 

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

The color components returned depend on what color space the passed CGColorRef uses.

More information can be found in the CGColor documentation.

htw
@htw Thanks. Does this mean it's always RGBA? I haven't seen an HSBA color space anywhere in the api's.
Joe Ricioppo
Not always—the CGColor could have been created in the CMYK or white color space, for example. However, according to Apple's color programming documentation (http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/DrawColor/index.html), the HSBA color space is designated as the RGBA color space. So, yes, in this case, it would return RGBA components, since the color was created with HSBA components.
htw
@htw Thanks! I've been looking through the docs. This is definitely what I needed to find. Very helpful!
Joe Ricioppo
+2  A: 
CGFloat* colors = CGColorGetComponents(hsbaColor.CGColor);

Does this return a float, or an array of floats? It looks like the asterisk is shorthand for creating an array. Is that correct?

Sort of.

CGFloat *colors declares a variable holding a pointer to at least one CGFloat. CGColorGetComponents returns a pointer to several CGFloats, one after the other—a C array. You take that pointer and assign it to (put it in) the colors variable.

Declaring the variable does not create the array. In fact, neither does CGColorGetComponents. Whatever created the CGColor object created the array and stored it inside the object; CGColorGetComponents lets you have the pointer to that storage.

Declaring the CGFloat *colors variable creates only a place to store a pointer to one or more CGFloats. That place is the colors variable itself. The thing in the variable is the pointer, and the thing at that pointer is the array.

If this is still unclear, see Everything you need to know about pointers in C.

Peter Hosey
@Peter Thanks! I should have made this two questions. This answer is very helpful. ObjC and C are my first programming languages, and I'm must picking it up from books and the documentation as I go. I hadn't seen this use of pointers before, so was confused by the documentation stating it returned an array. Your explanation was very helpful as well as the link.
Joe Ricioppo