import SwiftUI /// Material You palette built in OKLCH, ported 1:1 from the Android app's Palette.java. struct Palette: Equatable { var dark = true var seed: UInt32 = 0 var primary: UInt32 = 0 var onPrimary: UInt32 = 0 var primaryContainer: UInt32 = 0 var onPrimaryContainer: UInt32 = 0 var secondary: UInt32 = 0 var secondaryContainer: UInt32 = 0 var onSecondaryContainer: UInt32 = 0 var tertiary: UInt32 = 0 var tertiaryContainer: UInt32 = 0 var surface: UInt32 = 0 var surfaceLow: UInt32 = 0 var surfaceContainer: UInt32 = 0 var surfaceHigh: UInt32 = 0 var surfaceHighest: UInt32 = 0 var onSurface: UInt32 = 0 var onSurfaceVariant: UInt32 = 0 var outline: UInt32 = 0 var outlineVariant: UInt32 = 0 var error: UInt32 = 0 /// Android's default seed (-6651951 as ARGB). static let defaultSeed: UInt32 = 0x9A7FD1 static func fromSeed(_ rgb: UInt32, dark: Bool) -> Palette { let lch = rgbToOklch(rgb) let h = lch.2 let t1 = Tonal(h: h, c: min(max(lch.1, 0.085), 0.15)) let t2 = Tonal(h: h, c: 0.04) let t3 = Tonal(h: (60 + h).truncatingRemainder(dividingBy: 360), c: 0.075) let t4 = Tonal(h: h, c: 0.014) let t5 = Tonal(h: h, c: 0.025) var p = Palette() p.dark = dark p.seed = rgb if dark { p.primary = t1.t(80); p.onPrimary = t1.t(20) p.primaryContainer = t1.t(30); p.onPrimaryContainer = t1.t(90) p.secondary = t2.t(80); p.secondaryContainer = t2.t(30); p.onSecondaryContainer = t2.t(90) p.tertiary = t3.t(80); p.tertiaryContainer = t3.t(30) p.surface = t4.t(6); p.surfaceLow = t4.t(10); p.surfaceContainer = t4.t(12) p.surfaceHigh = t4.t(17); p.surfaceHighest = t4.t(22) p.onSurface = t4.t(90); p.onSurfaceVariant = t5.t(80) p.outline = t5.t(60); p.outlineVariant = t5.t(30) p.error = 0xF2B8B5 } else { p.primary = t1.t(40); p.onPrimary = t1.t(100) p.primaryContainer = t1.t(90); p.onPrimaryContainer = t1.t(10) p.secondary = t2.t(40); p.secondaryContainer = t2.t(90); p.onSecondaryContainer = t2.t(10) p.tertiary = t3.t(40); p.tertiaryContainer = t3.t(90) p.surface = t4.t(98); p.surfaceLow = t4.t(96); p.surfaceContainer = t4.t(94) p.surfaceHigh = t4.t(92); p.surfaceHighest = t4.t(90) p.onSurface = t4.t(10); p.onSurfaceVariant = t5.t(30) p.outline = t5.t(50); p.outlineVariant = t5.t(80) p.error = 0xB3261E } return p } struct Tonal { let h: Double let c: Double func t(_ tone: Double) -> UInt32 { oklchToRgb(toneToL(tone), c, h) } } static func toneToL(_ d: Double) -> Double { d <= 8 ? cbrt(d / 903.2963) : (16 + d) / 116 } static func lin(_ d: Double) -> Double { d <= 0.04045 ? d / 12.92 : pow((0.055 + d) / 1.055, 2.4) } static func delin(_ d: Double) -> Double { d <= 0.0031308 ? 12.92 * d : 1.055 * pow(d, 1 / 2.4) - 0.055 } static func rgbToOklch(_ rgb: UInt32) -> (Double, Double, Double) { let r = lin(Double((rgb >> 16) & 0xFF) / 255) let g = lin(Double((rgb >> 8) & 0xFF) / 255) let b = lin(Double(rgb & 0xFF) / 255) let l = cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b) let m = cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b) let s = cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b) let L = 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s let A = 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s let B = 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s let h = (atan2(B, A) * 180 / .pi + 360).truncatingRemainder(dividingBy: 360) return (L, hypot(A, B), h) } static func toLin(_ L: Double, _ C: Double, _ H: Double) -> (Double, Double, Double) { let a = cos(H * .pi / 180) * C let b = sin(H * .pi / 180) * C let l = pow(L + 0.3963377774 * a + 0.2158037573 * b, 3) let m = pow(L - 0.1055613458 * a - 0.0638541728 * b, 3) let s = pow(L - 0.0894841775 * a - 1.291485548 * b, 3) return (4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s, -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s, -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s) } static func inGamut(_ v: (Double, Double, Double)) -> Bool { let lo = -1e-4, hi = 1.0001 return v.0 >= lo && v.0 <= hi && v.1 >= lo && v.1 <= hi && v.2 >= lo && v.2 <= hi } static func oklchToRgb(_ L: Double, _ C: Double, _ H: Double) -> UInt32 { var v = toLin(L, C, H) if !inGamut(v) { var lo = 0.0, hi = C for _ in 0..<18 { let mid = (lo + hi) / 2 if inGamut(toLin(L, mid, H)) { lo = mid } else { hi = mid } } v = toLin(L, lo, H) } func ch(_ x: Double) -> UInt32 { UInt32(max(0, min(255, (delin(max(0, min(1, x))) * 255).rounded()))) } return (ch(v.0) << 16) | (ch(v.1) << 8) | ch(v.2) } static func mix(_ a: UInt32, _ b: UInt32, _ f: Double) -> UInt32 { func c(_ x: UInt32, _ s: UInt32) -> Double { Double((x >> s) & 0xFF) } func m(_ s: UInt32) -> UInt32 { UInt32((c(a, s) + (c(b, s) - c(a, s)) * f).rounded()) << s } return m(16) | m(8) | m(0) } /// Dominant colour of an image (32x32 OKLCH hue histogram), as in Palette.dominant(). static func dominant(_ image: CGImage?) -> UInt32? { guard let image else { return nil } let n = 32 var px = [UInt8](repeating: 0, count: n * n * 4) guard let ctx = CGContext(data: &px, width: n, height: n, bitsPerComponent: 8, bytesPerRow: n * 4, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return nil } ctx.interpolationQuality = .medium ctx.draw(image, in: CGRect(x: 0, y: 0, width: n, height: n)) var w = [Double](repeating: 0, count: 24), cx = w, sx = w, cc = w for i in 0..<(n * n) { let rgb = (UInt32(px[i * 4]) << 16) | (UInt32(px[i * 4 + 1]) << 8) | UInt32(px[i * 4 + 2]) let (L, C, H) = rgbToOklch(rgb) guard C >= 0.03, L >= 0.2, L <= 0.95 else { continue } let bin = Int(H / 15) % 24 let wt = C * C w[bin] += wt cx[bin] += cos(H * .pi / 180) * wt sx[bin] += sin(H * .pi / 180) * wt cc[bin] += wt * C } var best = -1 for i in 0..<24 where w[i] > 0 && (best < 0 || w[i] > w[best]) { best = i } guard best >= 0 else { return nil } let hue = (atan2(sx[best], cx[best]) * 180 / .pi + 360).truncatingRemainder(dividingBy: 360) return oklchToRgb(0.65, max(cc[best] / w[best], 0.1), hue) } } extension Color { init(rgb: UInt32, alpha: Double = 1) { self.init(.sRGB, red: Double((rgb >> 16) & 0xFF) / 255, green: Double((rgb >> 8) & 0xFF) / 255, blue: Double(rgb & 0xFF) / 255, opacity: alpha) } } /// SwiftUI-friendly accessors. extension Palette { var cPrimary: Color { Color(rgb: primary) } var cOnPrimary: Color { Color(rgb: onPrimary) } var cPrimaryC: Color { Color(rgb: primaryContainer) } var cOnPrimaryC: Color { Color(rgb: onPrimaryContainer) } var cSecondary: Color { Color(rgb: secondary) } var cSecondaryC: Color { Color(rgb: secondaryContainer) } var cOnSecondaryC: Color { Color(rgb: onSecondaryContainer) } var cTertiary: Color { Color(rgb: tertiary) } var cSurface: Color { Color(rgb: surface) } var cSurfaceLow: Color { Color(rgb: surfaceLow) } var cSurfaceC: Color { Color(rgb: surfaceContainer) } var cSurfaceHigh: Color { Color(rgb: surfaceHigh) } var cSurfaceHighest: Color { Color(rgb: surfaceHighest) } var cOnSurface: Color { Color(rgb: onSurface) } var cOnSurfaceVar: Color { Color(rgb: onSurfaceVariant) } var cOutline: Color { Color(rgb: outline) } var cOutlineVar: Color { Color(rgb: outlineVariant) } }