objective-cappkitnsgradient

What is "index" in NSGradient getColor:location:atIndex?


In the following NSGradient method:

- (void)getColor:(NSColor **)color location:(CGFloat *)location atIndex:(NSInteger)index

What is index? The documentation says index of the colour I want. But I thought the point of this method is that it tells YOU the colour to use for a particular location ? I don't know the index.

I want to define a gradient (colors and locations), and given a float return the corresponding colour.

Does anyone have an example where a gradient is defined, and this method is called ?


Solution

  • As the docs say,

    This method returns the color stop information that was used to create the receiver. It does not return the interpolated color values at any point along the gradient.

    The index is into the initial array of colors that you used to create the gradient. The location parameter isn't one that you pass in; it's an out parameter like color, and will give you the location (which you also specified when creating the gradient) associated with the color stop at the index you pass.

    It sounds like you're looking for interpolatedColorAtLocation: