This is thrown away together, but it is an approach that I will probably take. This creates an angle gradient by drawing it directly into a bitmap using a simple trigger, and then cropping it in a circle. I create a memory grid using the grayscale color space, calculate the angle from the given point to the center, and then the color based on the periodic function, between 0 and 255. Of course, you could expand this to RGBA color.
Of course, you have to cache this and play with math to get the colors you need. It currently runs from black to white, which doesnβt look as good as we would like.

- (void)drawRect:(CGRect)rect { CGImageAlphaInfo alphaInfo = kCGImageAlphaNone; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); size_t components = CGColorSpaceGetNumberOfComponents( colorSpace ); size_t width = 100; size_t height = 100; size_t bitsPerComponent = 8; size_t bytesPerComponent = bitsPerComponent / 8; size_t bytesPerRow = width * bytesPerComponent * components; size_t dataLength = bytesPerRow * height; uint8_t data[dataLength]; CGContextRef imageCtx = CGBitmapContextCreate( &data, width, height, bitsPerComponent, bytesPerRow, colorSpace, alphaInfo ); NSUInteger offset = 0; for (NSUInteger y = 0; y < height; ++y) { for (NSUInteger x = 0; x < bytesPerRow; x += components) { CGFloat opposite = y - height/2.; CGFloat adjacent = x - width/2.; if (adjacent == 0) adjacent = 0.001; CGFloat angle = atan(opposite/adjacent); data[offset] = abs((cos(angle * 2) * 255)); offset += components * bytesPerComponent; } } CGImageRef image = CGBitmapContextCreateImage(imageCtx); CGContextRelease(imageCtx); CGColorSpaceRelease(colorSpace); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGRect buttonRect = CGRectMake(100, 100, width, width); CGContextAddEllipseInRect(ctx, buttonRect); CGContextClip(ctx); CGContextDrawImage(ctx, buttonRect, image); CGImageRelease(image); }
Rob Napier Aug 02 2018-11-11T00: 00Z
source share