Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP ID & Card Vault7// File : SPVault/Engines/VirtualCard/DemoCardFactory.swift8// Module : SPVault9// Repository : https://git.spboucher.ai/sp-id-card-vault10// Project Website : Not specified11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041#if DEBUG || targetEnvironment(simulator)42import UIKit4344/// Fictitious cards drawn in code, for tests, screenshots and the simulator's45/// camera stand-in. No real issuer, person or number: every name is invented,46/// every card is stamped SPECIMEN and bank numbers are published test numbers.47/// This file is compiled out of Release device builds.48enum DemoCardFactory {49 struct Demo {50 var card: Card51 var front: CGImage52 var back: CGImage?53 }5455 static let cardSize = CGSize(width: 1712, height: 1080) // ID-1 at 20 px/mm56 static let passportSize = CGSize(width: 1750, height: 1232) // ID-3 at 14 px/mm5758 private static func date(_ year: Int, _ month: Int, _ day: Int) -> Date {59 Calendar(identifier: .gregorian).date(from: DateComponents(year: year, month: month, day: day, hour: 12))!60 }6162 private static func font(_ size: CGFloat, _ weight: UIFont.Weight = .regular, mono: Bool = false, rounded: Bool = false) -> UIFont {63 if mono { return .monospacedSystemFont(ofSize: size, weight: weight) }64 let base = UIFont.systemFont(ofSize: size, weight: weight)65 if rounded, let descriptor = base.fontDescriptor.withDesign(.rounded) { return UIFont(descriptor: descriptor, size: size) }66 return base67 }6869 private static func draw(_ text: String, at point: CGPoint, font: UIFont, color: UIColor, kern: CGFloat = 0) {70 (text as NSString).draw(at: point, withAttributes: [.font: font, .foregroundColor: color, .kern: kern])71 }7273 private static func render(_ size: CGSize, _ body: (CGContext, CGRect) -> Void) -> CGImage {74 let format = UIGraphicsImageRendererFormat()75 format.scale = 176 format.opaque = true77 return UIGraphicsImageRenderer(size: size, format: format).image { context in78 body(context.cgContext, CGRect(origin: .zero, size: size))79 }.cgImage!80 }8182 private static func gradient(_ context: CGContext, _ rect: CGRect, _ colors: [UIColor], from: CGPoint = .zero, to: CGPoint? = nil) {83 let cg = CGGradient(colorsSpace: CGColorSpace(name: CGColorSpace.sRGB), colors: colors.map(\.cgColor) as CFArray, locations: nil)!84 context.saveGState()85 context.clip(to: rect)86 context.drawLinearGradient(cg, start: CGPoint(x: rect.minX + from.x * rect.width, y: rect.minY + from.y * rect.height),87 end: to.map { CGPoint(x: rect.minX + $0.x * rect.width, y: rect.minY + $0.y * rect.height) }88 ?? CGPoint(x: rect.maxX, y: rect.maxY), options: [.drawsBeforeStartLocation, .drawsAfterEndLocation])89 context.restoreGState()90 }9192 private static func guilloche(_ context: CGContext, _ rect: CGRect, color: UIColor, lines: Int = 26) {93 context.saveGState()94 context.setStrokeColor(color.cgColor)95 context.setLineWidth(1.6)96 for i in 0..<lines {97 let path = CGMutablePath()98 let phase = CGFloat(i) * 0.4299 for x in stride(from: rect.minX, through: rect.maxX, by: 8) {100 let t = (x - rect.minX) / rect.width101 let y = rect.midY + sin(t * 9 + phase) * rect.height * 0.32 * cos(t * 2.2 + phase * 0.3) + CGFloat(i - lines / 2) * 9102 x == rect.minX ? path.move(to: CGPoint(x: x, y: y)) : path.addLine(to: CGPoint(x: x, y: y))103 }104 context.addPath(path)105 context.strokePath()106 }107 context.restoreGState()108 }109110 private static func chip(_ context: CGContext, _ rect: CGRect) {111 let path = UIBezierPath(roundedRect: rect, cornerRadius: rect.height * 0.18)112 context.saveGState()113 path.addClip()114 gradient(context, rect, [UIColor(red: 0.93, green: 0.82, blue: 0.52, alpha: 1), UIColor(red: 0.72, green: 0.58, blue: 0.28, alpha: 1)])115 context.setStrokeColor(UIColor(white: 0.25, alpha: 0.55).cgColor)116 context.setLineWidth(3)117 for f in [0.33, 0.66] {118 context.move(to: CGPoint(x: rect.minX, y: rect.minY + rect.height * f))119 context.addLine(to: CGPoint(x: rect.maxX, y: rect.minY + rect.height * f))120 }121 context.move(to: CGPoint(x: rect.midX, y: rect.minY)); context.addLine(to: CGPoint(x: rect.midX, y: rect.maxY))122 context.strokePath()123 context.restoreGState()124 }125126 private static func portrait(_ context: CGContext, _ rect: CGRect, tint: UIColor) {127 context.saveGState()128 UIBezierPath(roundedRect: rect, cornerRadius: 18).addClip()129 gradient(context, rect, [tint.withAlphaComponent(0.35), tint.withAlphaComponent(0.12)], from: CGPoint(x: 0.5, y: 0), to: CGPoint(x: 0.5, y: 1))130 context.setFillColor(tint.withAlphaComponent(0.55).cgColor)131 let head = CGRect(x: rect.midX - rect.width * 0.2, y: rect.minY + rect.height * 0.18, width: rect.width * 0.4, height: rect.width * 0.4)132 context.fillEllipse(in: head)133 context.fillEllipse(in: CGRect(x: rect.midX - rect.width * 0.42, y: head.maxY + rect.height * 0.05, width: rect.width * 0.84, height: rect.height * 0.7))134 context.restoreGState()135 }136137 private static func specimen(_ context: CGContext, _ rect: CGRect, color: UIColor) {138 context.saveGState()139 context.translateBy(x: rect.midX, y: rect.midY)140 context.rotate(by: -0.42)141 let f = font(rect.height * 0.16, .heavy)142 let text = "SPECIMEN" as NSString143 let size = text.size(withAttributes: [.font: f, .kern: 22])144 text.draw(at: CGPoint(x: -size.width / 2, y: -size.height / 2), withAttributes: [.font: f, .foregroundColor: color, .kern: 22])145 context.restoreGState()146 }147148 // MARK: Bank card149150 static func bankFront(issuer: String, number: String, holder: String, expiry: String, colors: [UIColor], network: String, accent: UIColor = .white) -> CGImage {151 render(cardSize) { context, rect in152 gradient(context, rect, colors)153 guilloche(context, rect.insetBy(dx: -40, dy: 120), color: UIColor.white.withAlphaComponent(0.05))154 context.setFillColor(UIColor.white.withAlphaComponent(0.05).cgColor)155 context.fillEllipse(in: CGRect(x: rect.width * 0.55, y: -rect.height * 0.5, width: rect.width * 0.9, height: rect.width * 0.9))156 draw(issuer.uppercased(), at: CGPoint(x: 110, y: 96), font: font(74, .semibold), color: accent, kern: 6)157 draw("WORLD ELITE", at: CGPoint(x: 112, y: 190), font: font(34, .medium), color: accent.withAlphaComponent(0.6), kern: 10)158 chip(context, CGRect(x: 118, y: 380, width: 210, height: 160))159 draw(number, at: CGPoint(x: 110, y: 640), font: font(104, .medium, mono: true), color: accent, kern: 2)160 draw("VALID THRU", at: CGPoint(x: 760, y: 800), font: font(28, .semibold), color: accent.withAlphaComponent(0.6), kern: 4)161 draw(expiry, at: CGPoint(x: 760, y: 840), font: font(62, .medium, mono: true), color: accent)162 draw(holder.uppercased(), at: CGPoint(x: 112, y: 920), font: font(58, .medium), color: accent, kern: 4)163 draw(network, at: CGPoint(x: rect.width - 420, y: 880), font: font(92, .heavy, rounded: true), color: accent.withAlphaComponent(0.92))164 specimen(context, rect, color: accent.withAlphaComponent(0.07))165 }166 }167168 /// Back of a bank card: stripe, signature panel and a three-digit security169 /// code at `cvvRect` (in unit coordinates, origin top-left).170 static let bankBackCVVRect = CGRect(x: 0.715, y: 0.395, width: 0.13, height: 0.105)171172 static func bankBack(issuer: String, cvv: String = "737", colors: [UIColor]) -> CGImage {173 render(cardSize) { context, rect in174 gradient(context, rect, colors.reversed())175 context.setFillColor(UIColor(white: 0.06, alpha: 1).cgColor)176 context.fill(CGRect(x: 0, y: 110, width: rect.width, height: 190))177 let panel = CGRect(x: 110, y: 410, width: 1090, height: 140)178 context.setFillColor(UIColor(white: 0.93, alpha: 1).cgColor)179 context.fill(panel)180 context.setStrokeColor(UIColor(white: 0.7, alpha: 1).cgColor)181 context.setLineWidth(2)182 for y in stride(from: panel.minY + 18, to: panel.maxY, by: 18) {183 context.move(to: CGPoint(x: panel.minX, y: y)); context.addLine(to: CGPoint(x: panel.maxX, y: y))184 }185 context.strokePath()186 draw("Alex Specimen", at: CGPoint(x: 160, y: 430), font: UIFont(name: "SnellRoundhand-Bold", size: 84) ?? font(80, .light), color: UIColor(white: 0.15, alpha: 1))187 let code = CGRect(x: 1224, y: 426, width: 222, height: 114)188 context.setFillColor(UIColor.white.cgColor)189 context.fill(code)190 draw(cvv, at: CGPoint(x: code.minX + 32, y: code.minY + 16), font: font(74, .semibold, mono: true), color: .black, kern: 6)191 draw("CVV", at: CGPoint(x: code.minX + 60, y: code.maxY + 10), font: font(30, .semibold), color: UIColor.white.withAlphaComponent(0.75), kern: 4)192 draw("Authorized signature — not valid unless signed.", at: CGPoint(x: 110, y: 574), font: font(30), color: UIColor.white.withAlphaComponent(0.7))193 let lines = ["This card is the property of \(issuer). Use is governed by the cardholder agreement.",194 "If found, please return to any \(issuer) branch. Customer service 1 800 555 0199.",195 "SPECIMEN — fictitious card generated for testing. Not a payment instrument."]196 for (i, line) in lines.enumerated() {197 draw(line, at: CGPoint(x: 110, y: 720 + CGFloat(i) * 56), font: font(32), color: UIColor.white.withAlphaComponent(0.72))198 }199 draw(issuer.uppercased(), at: CGPoint(x: 110, y: 950), font: font(46, .semibold), color: UIColor.white.withAlphaComponent(0.85), kern: 5)200 }201 }202203 // MARK: Identity documents204205 static func identityFront(title: String, authority: String, fields: [(String, String)], number: String, tint: UIColor, paper: [UIColor]) -> CGImage {206 render(cardSize) { context, rect in207 gradient(context, rect, paper)208 guilloche(context, rect, color: tint.withAlphaComponent(0.13), lines: 34)209 context.setFillColor(tint.cgColor)210 context.fill(CGRect(x: 0, y: 0, width: rect.width, height: 150))211 draw(authority.uppercased(), at: CGPoint(x: 70, y: 28), font: font(46, .bold), color: .white, kern: 5)212 draw(title, at: CGPoint(x: 72, y: 88), font: font(36, .medium), color: UIColor.white.withAlphaComponent(0.85), kern: 2)213 portrait(context, CGRect(x: 70, y: 210, width: 470, height: 610), tint: tint)214 var y: CGFloat = 214215 for (label, value) in fields {216 draw(label.uppercased(), at: CGPoint(x: 600, y: y), font: font(26, .semibold), color: tint.withAlphaComponent(0.9), kern: 3)217 draw(value, at: CGPoint(x: 600, y: y + 32), font: font(52, .semibold), color: UIColor(white: 0.1, alpha: 1))218 y += 122219 }220 draw(number, at: CGPoint(x: 70, y: 880), font: font(76, .semibold, mono: true), color: UIColor(white: 0.1, alpha: 1), kern: 3)221 draw("Alex Specimen", at: CGPoint(x: 1130, y: 890), font: UIFont(name: "SnellRoundhand-Bold", size: 78) ?? font(70, .light), color: UIColor(white: 0.12, alpha: 1))222 specimen(context, rect, color: tint.withAlphaComponent(0.12))223 }224 }225226 static func identityBack(tint: UIColor, paper: [UIColor], lines: [String]) -> CGImage {227 render(cardSize) { context, rect in228 gradient(context, rect, paper.reversed())229 guilloche(context, rect, color: tint.withAlphaComponent(0.1), lines: 30)230 context.setFillColor(UIColor(white: 0.08, alpha: 1).cgColor)231 var x: CGFloat = 80232 var seed: UInt64 = 0x5EED233 while x < 900 { // decorative, non-decodable bar pattern234 seed = seed &* 6364136223846793005 &+ 1442695040888963407235 let w = CGFloat(3 + (seed >> 60))236 context.fill(CGRect(x: x, y: 110, width: w, height: 190))237 x += w + CGFloat(4 + ((seed >> 56) & 7))238 }239 for (i, line) in lines.enumerated() {240 draw(line, at: CGPoint(x: 80, y: 380 + CGFloat(i) * 66), font: font(40, .medium), color: UIColor(white: 0.15, alpha: 1))241 }242 draw("SPECIMEN — fictitious document generated for testing.", at: CGPoint(x: 80, y: 960), font: font(34, .semibold), color: tint)243 }244 }245246 static func passportPage() -> CGImage {247 render(passportSize) { context, rect in248 let tint = UIColor(red: 0.10, green: 0.22, blue: 0.45, alpha: 1)249 gradient(context, rect, [UIColor(red: 0.93, green: 0.94, blue: 0.97, alpha: 1), UIColor(red: 0.84, green: 0.88, blue: 0.95, alpha: 1)])250 guilloche(context, rect, color: tint.withAlphaComponent(0.10), lines: 40)251 draw("PASSPORT · PASSEPORT", at: CGPoint(x: 80, y: 54), font: font(50, .bold), color: tint, kern: 6)252 draw("REPUBLIC OF NORTHWIND", at: CGPoint(x: 80, y: 122), font: font(36, .semibold), color: tint.withAlphaComponent(0.75), kern: 5)253 portrait(context, CGRect(x: 80, y: 220, width: 480, height: 620), tint: tint)254 let rows = [("Surname / Nom", "SPECIMEN"), ("Given names / Prénoms", "ALEX JORDAN"), ("Nationality / Nationalité", "NORTHWIND"),255 ("Date of birth / Date de naissance", "14 FEB 1988"), ("Date of expiry / Date d'expiration", "09 AUG 2031")]256 var y: CGFloat = 224257 for (label, value) in rows {258 draw(label, at: CGPoint(x: 620, y: y), font: font(26, .medium), color: tint.withAlphaComponent(0.8))259 draw(value, at: CGPoint(x: 620, y: y + 32), font: font(50, .semibold), color: UIColor(white: 0.1, alpha: 1))260 y += 118261 }262 draw("Passport No.", at: CGPoint(x: 1250, y: 224), font: font(26, .medium), color: tint.withAlphaComponent(0.8))263 draw("NW4821973", at: CGPoint(x: 1250, y: 256), font: font(50, .semibold, mono: true), color: UIColor(white: 0.1, alpha: 1))264 context.setFillColor(UIColor.white.withAlphaComponent(0.55).cgColor)265 context.fill(CGRect(x: 0, y: 960, width: rect.width, height: rect.height - 960))266 draw("P<NWDSPECIMEN<<ALEX<JORDAN<<<<<<<<<<<<<<<<<<<", at: CGPoint(x: 80, y: 990), font: font(52, .medium, mono: true), color: UIColor(white: 0.1, alpha: 1))267 draw("NW4821973<4NWD8802145M3108097<<<<<<<<<<<<<<06", at: CGPoint(x: 80, y: 1070), font: font(52, .medium, mono: true), color: UIColor(white: 0.1, alpha: 1))268 specimen(context, rect, color: tint.withAlphaComponent(0.10))269 }270 }271272 static func brandFront(brand: String, tagline: String, number: String, colors: [UIColor], accent: UIColor) -> CGImage {273 render(cardSize) { context, rect in274 gradient(context, rect, colors, from: CGPoint(x: 0, y: 1), to: CGPoint(x: 1, y: 0))275 context.setStrokeColor(accent.withAlphaComponent(0.18).cgColor)276 context.setLineWidth(3)277 for i in 0..<9 {278 context.strokeEllipse(in: CGRect(x: rect.width * 0.5 - CGFloat(i) * 60, y: rect.height * 0.1 - CGFloat(i) * 60,279 width: 900 + CGFloat(i) * 120, height: 900 + CGFloat(i) * 120))280 }281 draw(brand, at: CGPoint(x: 110, y: 110), font: font(120, .heavy, rounded: true), color: accent)282 draw(tagline.uppercased(), at: CGPoint(x: 116, y: 260), font: font(38, .semibold), color: accent.withAlphaComponent(0.7), kern: 9)283 draw("MEMBER", at: CGPoint(x: 112, y: 780), font: font(30, .semibold), color: accent.withAlphaComponent(0.65), kern: 6)284 draw("ALEX SPECIMEN", at: CGPoint(x: 110, y: 820), font: font(60, .semibold), color: accent, kern: 3)285 draw(number, at: CGPoint(x: 110, y: 910), font: font(60, .medium, mono: true), color: accent.withAlphaComponent(0.9), kern: 3)286 specimen(context, rect, color: accent.withAlphaComponent(0.06))287 }288 }289290 // MARK: Demo set291292 static func all(now: Date = Date()) -> [Demo] {293 let calendar = Calendar(identifier: .gregorian)294 func inDays(_ days: Int) -> Date { calendar.date(byAdding: .day, value: days, to: now)! }295 var demos: [Demo] = []296297 let obsidian = [UIColor(red: 0.07, green: 0.08, blue: 0.11, alpha: 1), UIColor(red: 0.16, green: 0.19, blue: 0.27, alpha: 1)]298 var credit = Card(name: "Aurora Infinite", kind: .creditCard)299 credit.issuer = "Aurora Bank"; credit.holderName = "Alex Specimen"; credit.number = "4242424242424242"300 credit.expiry = inDays(540); credit.isFavorite = true301 credit.notes = "Travel insurance included. Concierge 1 800 555 0199."302 demos.append(Demo(card: credit, front: bankFront(issuer: "Aurora Bank", number: "4242 4242 4242 4242", holder: "Alex Specimen", expiry: "05/28", colors: obsidian, network: "VISA"),303 back: bankBack(issuer: "Aurora Bank", colors: obsidian)))304305 var licence = Card(name: "Driver's Licence", kind: .driverLicense)306 licence.issuer = "Northwind Motor Vehicles"; licence.holderName = "Alex Specimen"; licence.number = "S1234-140288-07"307 licence.expiry = inDays(42); licence.isFavorite = true308 licence.fields = [CardField(label: "Class", value: "5"), CardField(label: "Date of birth", value: "1988-02-14", isSensitive: true)]309 let licenceTint = UIColor(red: 0.05, green: 0.36, blue: 0.62, alpha: 1)310 let licencePaper = [UIColor(red: 0.90, green: 0.95, blue: 0.99, alpha: 1), UIColor(red: 0.78, green: 0.88, blue: 0.96, alpha: 1)]311 demos.append(Demo(card: licence, front: identityFront(title: "Driver's Licence · Permis de conduire", authority: "Province of Northwind",312 fields: [("Name", "SPECIMEN, Alex"), ("Date of birth", "1988-02-14"), ("Class", "5"), ("Expires", "2027-01-13"), ("Address", "12 Example St, Sampleton")],313 number: "S1234-140288-07", tint: licenceTint, paper: licencePaper),314 back: identityBack(tint: licenceTint, paper: licencePaper, lines: ["Class 5 — passenger vehicles", "Conditions: A (corrective lenses)", "Organ donor: yes", "Issued 2023-01-13"])))315316 var passport = Card(name: "Passport", kind: .passport)317 passport.issuer = "Republic of Northwind"; passport.holderName = "Alex Jordan Specimen"; passport.number = "NW4821973"318 passport.expiry = date(2031, 8, 9)319 demos.append(Demo(card: passport, front: passportPage(), back: nil))320321 var health = Card(name: "Health Insurance Card", kind: .healthCard)322 health.issuer = "Northwind Health"; health.holderName = "Alex Specimen"; health.number = "SPEA 8802 1417"323 health.expiry = inDays(-20)324 let healthTint = UIColor(red: 0.80, green: 0.45, blue: 0.10, alpha: 1)325 let healthPaper = [UIColor(red: 1.0, green: 0.93, blue: 0.72, alpha: 1), UIColor(red: 0.98, green: 0.80, blue: 0.45, alpha: 1)]326 demos.append(Demo(card: health, front: identityFront(title: "Health Insurance · Assurance maladie", authority: "Northwind Health",327 fields: [("Name", "SPECIMEN, Alex"), ("Born", "1988-02-14"), ("Sex", "X"), ("Expires", "2026-11")],328 number: "SPEA 8802 1417", tint: healthTint, paper: healthPaper),329 back: identityBack(tint: healthTint, paper: healthPaper, lines: ["Present this card at every visit.", "Information: 1 800 555 0142"])))330331 let titanium = [UIColor(red: 0.62, green: 0.65, blue: 0.70, alpha: 1), UIColor(red: 0.84, green: 0.86, blue: 0.89, alpha: 1)]332 var debit = Card(name: "Everyday Debit", kind: .debitCard)333 debit.issuer = "Meridian Credit Union"; debit.holderName = "Alex Specimen"; debit.number = "5555555555554444"334 debit.expiry = inDays(75)335 demos.append(Demo(card: debit, front: bankFront(issuer: "Meridian", number: "5555 5555 5555 4444", holder: "Alex Specimen", expiry: "02/27", colors: titanium, network: "debit", accent: UIColor(white: 0.12, alpha: 1)),336 back: bankBack(issuer: "Meridian", cvv: "418", colors: titanium)))337338 var insurance = Card(name: "Auto Insurance", kind: .autoInsurance)339 insurance.issuer = "Harbor Mutual"; insurance.holderName = "Alex Specimen"; insurance.number = "HM-AUTO-0048213"340 insurance.expiry = inDays(160)341 insurance.fields = [CardField(label: "Vehicle", value: "2024 Example EV"), CardField(label: "Claims", value: "1 800 555 0177")]342 demos.append(Demo(card: insurance, front: brandFront(brand: "Harbor Mutual", tagline: "Auto insurance certificate", number: "HM-AUTO-0048213",343 colors: [UIColor(red: 0.04, green: 0.30, blue: 0.28, alpha: 1), UIColor(red: 0.10, green: 0.52, blue: 0.46, alpha: 1)], accent: .white), back: nil))344345 var university = Card(name: "Faculty Card", kind: .universityCard)346 university.issuer = "Northwind University"; university.holderName = "Alex Specimen"; university.number = "FAC-204718"347 university.expiry = inDays(300)348 demos.append(Demo(card: university, front: brandFront(brand: "Northwind University", tagline: "Faculty · School of Management", number: "FAC-204718",349 colors: [UIColor(red: 0.26, green: 0.14, blue: 0.48, alpha: 1), UIColor(red: 0.46, green: 0.32, blue: 0.78, alpha: 1)], accent: .white), back: nil))350351 var loyalty = Card(name: "Skyline Rewards", kind: .loyaltyCard)352 loyalty.issuer = "Skyline Air"; loyalty.holderName = "Alex Specimen"; loyalty.number = "SKY 778 204 913"353 demos.append(Demo(card: loyalty, front: brandFront(brand: "Skyline", tagline: "Rewards · Gold", number: "SKY 778 204 913",354 colors: [UIColor(red: 0.78, green: 0.56, blue: 0.20, alpha: 1), UIColor(red: 0.96, green: 0.84, blue: 0.52, alpha: 1)], accent: UIColor(white: 0.10, alpha: 1)), back: nil))355356 return demos.map { demo in357 var demo = demo358 demo.card.dominantColor = CardImageCodec.dominantColor(of: demo.front)359 return demo360 }361 }362363 /// A card photographed on a table: the flat artwork placed in perspective on a364 /// textured background, to exercise detection and perspective correction.365 static func photographed(_ card: CGImage, canvas: CGSize = CGSize(width: 3024, height: 4032), skew: CGFloat = 0.06) -> (image: CGImage, corners: [CGPoint]) {366 let input = CIImage(cgImage: card)367 let w = canvas.width, h = canvas.height368 let cardWidth = w * 0.78, cardHeight = cardWidth * CGFloat(card.height) / CGFloat(card.width)369 let cx = w / 2, cy = h / 2370 // Core Image coordinates (origin bottom-left).371 let tl = CGPoint(x: cx - cardWidth / 2 + w * skew, y: cy + cardHeight / 2 + h * skew * 0.3)372 let tr = CGPoint(x: cx + cardWidth / 2 - w * skew * 0.4, y: cy + cardHeight / 2 - h * skew * 0.2)373 let br = CGPoint(x: cx + cardWidth / 2, y: cy - cardHeight / 2 - h * skew * 0.1)374 let bl = CGPoint(x: cx - cardWidth / 2, y: cy - cardHeight / 2 + h * skew * 0.25)375 let warped = input.applyingFilter("CIPerspectiveTransform", parameters: [376 "inputTopLeft": CIVector(cgPoint: tl), "inputTopRight": CIVector(cgPoint: tr),377 "inputBottomRight": CIVector(cgPoint: br), "inputBottomLeft": CIVector(cgPoint: bl)])378 let table = CIImage(color: CIColor(red: 0.30, green: 0.24, blue: 0.19)).cropped(to: CGRect(origin: .zero, size: canvas))379 let composed = warped.composited(over: table)380 let cg = CIContext().createCGImage(composed, from: CGRect(origin: .zero, size: canvas))!381 return (cg, [tl, tr, br, bl])382 }383}384#endif385