import UIKit extension UIView { func gradientColor(bounds: CGRect, gradientLayer: CAGradientLayer) -> UIColor? { UIGraphicsBeginImageContext(gradientLayer.bounds.size) guard let context = UIGraphicsGetCurrentContext() else { return nil } gradientLayer.render(in: context) guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return nil } UIGraphicsEndImageContext() return UIColor(patternImage: image) } }