Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does CGColorGetComponents() return?

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?

like image 991
Joe Ricioppo Avatar asked Apr 27 '09 02:04

Joe Ricioppo


1 Answers

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 the pointer 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—the variable—to store a pointer to one or more CGFloats. 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.

like image 146
Peter Hosey Avatar answered Oct 10 '22 05:10

Peter Hosey