4 static func image(_ design: TileDesign, pixelSize: CGFloat, foregroundColor: CGColor? = nil, backgroundColor: CGColor? = nil) -> CGImage {
6 let fg = foregroundColor ?? .init(red: 0, green: 0, blue: 0, alpha: 255)
7 let bg = backgroundColor ?? .init(red: 255, green: 255, blue: 255, alpha: 255)
9 // Convert the array to image data
11 let pixels: [UInt8] = design.pixels().map({ x in
16 let components = color.components ?? [0,0,0,1]
17 return components.map({ c in
24 let pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: pixels.count)
25 pointer.initialize(from: pixels, count: pixels.count)
28 let bitsPerComponent = 8
29 let componentCount = 4
30 let bitsPerPixel = bitsPerComponent * componentCount
31 let colorSpace = CGColorSpaceCreateDeviceRGB()
33 let bitmapInfo = CGBitmapInfo([.byteOrderDefault, CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)])
34 let provider = CGDataProvider(dataInfo: nil, data: pointer, size: Int(width * height * 4)) { _, _, _ in
41 bitsPerComponent: bitsPerComponent,
42 bitsPerPixel: bitsPerPixel,
43 bytesPerRow: Int(width) * componentCount,
45 bitmapInfo: bitmapInfo,
47 decode: nil, shouldInterpolate: false, intent: CGColorRenderingIntent.defaultIntent)!
49 return image.resize(size: CGSize(width: width * pixelSize, height: height * pixelSize)) ?? image