//
// © 2017-2020 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
//

import UIKit

extension UIImage {

	// Create image of a given size of a solid color
	static func image(color: UIColor, size: CGSize, circular: Bool = false) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(size, !circular, 0)
        color.set()
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
		let path = circular ? UIBezierPath(ovalIn: rect) : UIBezierPath(rect: rect)
        path.fill()
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
	
	// Create grayscale version of image
	func grayscaleImage() -> UIImage {
		let filter = CIFilter(name: "CIPhotoEffectMono")
		
		let ciInput = CIImage(image: self)
		filter?.setValue(ciInput, forKey: "inputImage")
		
		let ciOutput = filter?.outputImage
		let ciContext = CIContext()
		let cgImage = ciContext.createCGImage(ciOutput!, from: (ciOutput?.extent)!)
		
		return UIImage(cgImage: cgImage!)
	}
}
