Search code examples
objective-cmacoscocoaquartz-graphics

Number Of Components in CGColorCreate() if colorspace is Indexed colorspace?


I am trying to use CGColorSpaceCreateIndexed() to Create ColorRef. I want to understand rule for passing component array when we use indexed color space.

CGColorRef CGColorCreate (
   CGColorSpaceRef colorspace,
   const CGFloat components[]
);

Apple doc says, The array should contain n+1 values that correspond to the n color components in the specified color space, followed by the alpha component.

unsigned char colorTable[] = {2,120,150,180,200,220,10, 30};
CGColorSpaceRef cs = CGColorSpaceCreateIndexed(CGColorSpaceCreateDeviceCMYK(), 1, colorTable);

now, if I create a color using this colorspace,

const CGFloat myComponentArray[] = ??;
CGColorRef colorref = CGColorCreate (cs, myComponentArray);

what should be myComponentArray? Should it depend upon the base colorspace passed in while creating indexed color space?


Solution

  • For an indexed color space, the color has one component, which is the index into the color table:

    CGFloat myComponentArray[] = { index, alpha };
    

    In your case, the color table has 2 entries, therefore the index must be 0 or 1.