//
//  PKDrawing+Render.swift
//  ComposeUI
//
//  Created by Shadowfacts on 5/9/20.
//  Copyright © 2020 Shadowfacts. All rights reserved.
//

import UIKit
import PencilKit

extension PKDrawing {
    
    func imageInLightMode(from rect: CGRect, scale: CGFloat = UIScreen.main.scale) -> UIImage {
        let lightTraitCollection = UITraitCollection(userInterfaceStyle: .light)
        var drawingImage: UIImage!
        lightTraitCollection.performAsCurrent {
            drawingImage = self.image(from: rect, scale: scale)
        }
        
        let imageRect = CGRect(origin: .zero, size: rect.size)
        let format = UIGraphicsImageRendererFormat()
        format.opaque = false
        format.scale = scale
        let renderer = UIGraphicsImageRenderer(size: rect.size, format: format)
        return renderer.image { (context) in
            UIColor.white.setFill()
            context.fill(imageRect)
            drawingImage.draw(in: imageRect)
        }
    }
    
}
