Branchwell

Graphley / app · graphley.ai / Sources / AnalyticsUI / Brand.swift

1import SwiftUI
2
3/// The Stacked tide icon drawn in SwiftUI, from the same curves as brand/graphley-icon.svg
4/// (scripts/icon-waves.py). Use it wherever the app shows its own brand: empty states, About.
5public struct TideMark: View {
6 let size: CGFloat
7 public init(size: CGFloat = 40) { self.size = size }
8 public var body: some View {
9 ZStack {
10 Tide.brandTide
11 TideWave(offset: 0, spread: 0).fill(Tide.brandFoam)
12 TideWave(offset: 162, spread: 40).fill(Tide.brandLagoon)
13 TideWave(offset: 325, spread: 70).fill(Tide.brandAmber)
14 }
15 .frame(width: size, height: size)
16 .clipShape(RoundedRectangle(cornerRadius: size * 0.225, style: .continuous))
17 .accessibilityHidden(true)
18 }
19}
20
21/// One band of the icon: everything below a smooth tide line. Coordinates follow the icon's
22/// 1024 × 1024 viewBox and scale to the shape's rect.
23public struct TideWave: Shape {
24 let offset: CGFloat, spread: CGFloat
25 public init(offset: CGFloat, spread: CGFloat) { self.offset = offset; self.spread = spread }
26
27 static let x0: CGFloat = -80, x1: CGFloat = 1104
28
29 static func smooth(_ t: CGFloat) -> CGFloat {
30 let t = min(1, max(0, t)); return t * t * t * (t * (6 * t - 15) + 10)
31 }
32 static func crest(_ x: CGFloat) -> CGFloat {
33 let dip = 34 * exp(-pow((x - 300) / 230, 2))
34 return 585 + dip - 470 * smooth((x - 220) / 1050)
35 }
36 func y(_ x: CGFloat) -> CGFloat { Self.crest(x) + offset + spread * (x - Self.x0) / (Self.x1 - Self.x0) }
37
38 public func path(in rect: CGRect) -> Path {
39 let k = rect.width / 1024, n = 12
40 let pts = (0...n).map { i -> CGPoint in
41 let x = Self.x0 + (Self.x1 - Self.x0) * CGFloat(i) / CGFloat(n)
42 return CGPoint(x: rect.minX + x * k, y: rect.minY + y(x) * k)
43 }
44 var p = Path()
45 p.move(to: pts[0])
46 for i in 0..<n { // Catmull-Rom to cubic Bézier: tangents match at every join.
47 let p0 = pts[max(i - 1, 0)], p1 = pts[i], p2 = pts[i + 1], p3 = pts[min(i + 2, n)]
48 p.addCurve(to: p2,
49 control1: CGPoint(x: p1.x + (p2.x - p0.x) / 6, y: p1.y + (p2.y - p0.y) / 6),
50 control2: CGPoint(x: p2.x - (p3.x - p1.x) / 6, y: p2.y - (p3.y - p1.y) / 6))
51 }
52 p.addLine(to: CGPoint(x: rect.minX + Self.x1 * k, y: rect.maxY + 80 * k))
53 p.addLine(to: CGPoint(x: rect.minX + Self.x0 * k, y: rect.maxY + 80 * k))
54 p.closeSubpath()
55 return p
56 }
57}