 import UIKit
 
 internal class RainbowCircleView: UIView {
+    var rainbowImage: CGImage!
+    
     var rainbowRadius: CGFloat = 100.0 {
         didSet{
             setNeedsDisplay()

        context.addPath(shapeCopyPath)
        
        context.clip(using: .winding)
        drawRainbow(onContext: context)
        context.resetClip()
    }
    

        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        rainbowImage = drawColorCircleImage(withDiameter: Int(min(self.bounds.size.width, self.bounds.size.height)))
    }
    
    func drawColorCircleImage(withDiameter diameter: Int) -> CGImage {
        let bufferLength: Int = Int(diameter * diameter * 4)
        
        let bitmapData: CFMutableData = CFDataCreateMutable(nil, 0)
        CFDataSetLength(bitmapData, CFIndex(bufferLength))
        let bitmap = CFDataGetMutableBytePtr(bitmapData)
        
        for y in 0 ... diameter {
            for x in 0 ... diameter {
                let angle = CGVector(point: CGPoint(x: x, y: diameter-y) - origo).angle
                let rgbComponents = UIColor.init(hue: getHue(at: angle), saturation: 1, brightness: 1, alpha: 1).cgColor.components!
                
                let offset = Int(4 * (x + y * diameter))
                bitmap?[offset] = UInt8(rgbComponents[0]*255)
                bitmap?[offset + 1] = UInt8(rgbComponents[1]*255)
                bitmap?[offset + 2] = UInt8(rgbComponents[2]*255)
                bitmap?[offset + 3] = UInt8(255)
            }
        }
        
        let colorSpace: CGColorSpace? = CGColorSpaceCreateDeviceRGB()
        let dataProvider: CGDataProvider? = CGDataProvider(data: bitmapData)
        let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo().rawValue | CGImageAlphaInfo.last.rawValue)
        let imageRef: CGImage? = CGImage(width: diameter, height: diameter, bitsPerComponent: 8, bitsPerPixel: 32, bytesPerRow: diameter * 4, space: colorSpace!, bitmapInfo: bitmapInfo, provider: dataProvider!, decode: nil, shouldInterpolate: false, intent: CGColorRenderingIntent.defaultIntent)
        return imageRef!
    }
    
    override func draw(_ rect: CGRect) {
