Few things. The error does not talk about colors, this refers to the const CGFloat locations[]
argument of const CGFloat locations[]
.
It must be a pointer, which can be achieved as follows ( Link to a pointer class )
locations = Pointer.new(:float, 2) locations[1] = 1.0
Next, your array does not need to complete nil
. In Ruby, this will create an array with three objects, which is not what you want, since it will most likely call the CGGradientCreateWithColors()
function to mess up
This looks like an example of http://www.raywenderlich.com/ , so the rest of it
def drawLinearGradient(context, rect, startColor, endColor) colorspace = CGColorSpaceCreateDeviceRGB() locations = Pointer.new(:float, 2) locations[1] = 1.0 colors = [startColor, endColor] gradient = CGGradientCreateWithColors(colorspace, colors, locations) startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect)) endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect)) CGContextSaveGState(context) CGContextAddRect(context, rect) CGContextClip(context) CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0) CGContextRestoreGState(context) end
Final note
The locations
argument is not required even in this case, since CGGradientCreateWithColors()
will automatically set the values ββ0.0 and 1.0 for the first and last color. Check out the CGGradient Reference
Places
Location for each color provided by the components. Each location must be a CGFloat value between 0 and 1 inclusive. If 0 and 1 are not in the array of locations, Quartz uses colors that are closest to 0 and 1 for these locations.
If the locations are NULL, the first color in the color is assigned to location 0, the last color colors are assigned to location 1, and the intermediate colors are assigned to places located between equal intervals.