Efficiency: Release builds by default, only the visible island screen is built, character at 30 fps, adaptive mouse polling (60/30/10 Hz), hex colours parsed once
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,28 +3,44 @@ import CoreGraphics
|
||||
|
||||
// Hex color helpers shared by the Mac app and the iPhone app.
|
||||
|
||||
// Views ask for the same few dozen hex colours on every frame: parse each string once.
|
||||
private enum HexCache {
|
||||
nonisolated(unsafe) static var colors: [String: Color] = [:]
|
||||
nonisolated(unsafe) static var cgColors: [String: CGColor] = [:]
|
||||
static let lock = NSLock()
|
||||
}
|
||||
|
||||
// MARK: - Color from hex string
|
||||
|
||||
extension Color {
|
||||
init(hex: String) {
|
||||
HexCache.lock.lock()
|
||||
if let c = HexCache.colors[hex] { HexCache.lock.unlock(); self = c; return }
|
||||
HexCache.lock.unlock()
|
||||
let h = hex.trimmingCharacters(in: CharacterSet(charactersIn: "#"))
|
||||
let val = UInt64(h, radix: 16) ?? 0
|
||||
let r = Double((val >> 16) & 0xFF) / 255
|
||||
let g = Double((val >> 8) & 0xFF) / 255
|
||||
let b = Double( val & 0xFF) / 255
|
||||
self.init(red: r, green: g, blue: b)
|
||||
HexCache.lock.lock(); HexCache.colors[hex] = self; HexCache.lock.unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - CGColor from hex string
|
||||
|
||||
func cgColorFromHex(_ hex: String) -> CGColor? {
|
||||
HexCache.lock.lock()
|
||||
if let c = HexCache.cgColors[hex] { HexCache.lock.unlock(); return c }
|
||||
HexCache.lock.unlock()
|
||||
let h = hex.trimmingCharacters(in: CharacterSet(charactersIn: "#"))
|
||||
guard let val = UInt64(h, radix: 16) else { return nil }
|
||||
let r = CGFloat((val >> 16) & 0xFF) / 255
|
||||
let g = CGFloat((val >> 8) & 0xFF) / 255
|
||||
let b = CGFloat( val & 0xFF) / 255
|
||||
return CGColor(red: r, green: g, blue: b, alpha: 1)
|
||||
let c = CGColor(red: r, green: g, blue: b, alpha: 1)
|
||||
HexCache.lock.lock(); HexCache.cgColors[hex] = c; HexCache.lock.unlock()
|
||||
return c
|
||||
}
|
||||
|
||||
extension CGColor {
|
||||
|
||||
Reference in New Issue
Block a user