// The one source of every variation a piece of paper has — split from the // paper's arithmetic (`Theme/PaperMetrics.swift`) because it is a function, // not a constant: the same seed is the same edge in every theme. import Foundation /// Deterministic, smooth, seeded noise — the one source of every variation a /// piece of paper has. /// /// Not `Hasher` and not `random`: `Hasher` is seeded per launch and a random /// edge would redraw on every layout pass, which is the jitter the owner's rule /// forbids. FNV-1a over the seed's UTF-8 feeds an xorshift stream, which fills a /// small ring of values; reading it at a unit position interpolates the ring /// with a raised cosine, so the edge wanders rather than stepping. public struct PaperNoise: Sendable { private let ring: [Double] /// `count` control points around the ring. More points is a busier edge; /// every caller names its own count, from the theme's scale. public init(seed: String, salt: UInt64, count: Int) { var state = SeededNoise.start(PaperNoise.fnv1a(seed), salt: salt) let points = max(SeededNoise.minimumRing, count) var values: [Double] = [] values.reserveCapacity(points) for _ in 0.. Double { let count = ring.count let position = (unit - unit.rounded(.down)) * Double(count) let index = Int(position) % count let next = (index + 1) % count let fraction = position - Double(Int(position)) // Raised cosine: the curve is flat where two control points meet, so // the edge has no visible kinks. let eased = SeededNoise.eased(fraction) return ring[index] * (1 - eased) + ring[next] * eased } /// One value in 0…1 from a seed and a salt, with no ring at all — for the /// things that vary once per object rather than along an edge. public static func unit(seed: String, salt: UInt64) -> Double { var state = SeededNoise.start(fnv1a(seed), salt: salt) return SeededNoise.advance(&state) } /// FNV-1a over the seed's UTF-8 — the same number on every launch and every /// device, which `Hasher` is not. The caps' hash, reused rather than /// re-derived. public static func fnv1a(_ text: String) -> UInt64 { CapColour.fnv1a(text) } }