Cotton code
74 lines · 3.0 KB · SwiftRaw
// The seed arithmetic under every piece of paper — hashing, not look.//// Moved out of `Paper/` so the paper's files carry only the theme's numbers:// what is here picks WHICH ring a sheet reads (a salt, a multiplier, the// xorshift stream), never how deep, how busy or how big anything is drawn.// Changing any of it re-deals every sheet's edge, so it is fixed in code and// never a theme knob.import Foundation/// The xorshift stream `PaperNoise` reads, and the mixing that starts it.public enum SeededNoise {    /// The golden-ratio multiplier that spreads a salt across the state, and    /// the state a zero would otherwise collapse to.    static let spread: UInt64 = 0x9E37_79B9_7F4A_7C15    /// How finely one step of the stream is cut into 0…1.    static let resolution: UInt64 = 100_000    /// The fewest control points a ring can interpolate between.    static let minimumRing = 2    /// Steps one index (a side, a corner) into its own salt.    public static let lane: UInt64 = 0x0001_0001    /// The stream's first state for a seed's hash and a salt.    static func start(_ hash: UInt64, salt: UInt64) -> UInt64 {        var state = hash ^ (salt &* spread)        if state == 0 { state = spread }        return state    }    /// Advances the stream one step and reads it, in 0…1.    static func advance(_ state: inout UInt64) -> Double {        state ^= state << 13        state ^= state >> 7        state ^= state << 17        return Double(state % resolution) / Double(resolution)    }    /// Raised cosine: flat where two control points meet, so an edge has no    /// visible kinks.    static func eased(_ fraction: Double) -> Double {        (1 - cos(fraction * Double.pi)) / 2    }}/// The salts — one per thing that reads noise, so two uses of one seed never/// read the same ring. Each spells its use in ASCII.public enum PaperSalt {    // PaperShape    public static let wave: UInt64 = 0x7761_7665  // "wave"    public static let corner: UInt64 = 0x636f_726e  // "corn"    public static let cornerLeaving: UInt64 = 0x55    public static let longWave: UInt64 = 0x11    public static let ripple: UInt64 = 0x22    public static let nicks: UInt64 = 0x33    public static let envelope: UInt64 = 0x44    // PaperPhoto    public static let flip: UInt64 = 0x666c_6970  // "flip"    public static let flipVertical: UInt64 = 0x5f    // Marks    public static let rule: UInt64 = 0x7275_6c65  // "rule"    public static let underline: UInt64 = 0x756e_6472  // "undr"    public static let circle: UInt64 = 0x6369_7263  // "circ"    public static let star: UInt64 = 0x7374_6172  // "star"    public static let progress: UInt64 = 0x7072_6772  // "prgr"}/// The cached sheet pictures' budget — memory bookkeeping, not look: a sheet/// over it is drawn in layers, which draw the same pixels.public enum PaperSurfaceImageBudget {    /// The most pixels one cached picture may hold.    public static let maxPixels: CGFloat = 4_194_304    public static let countLimit = 256    public static let costLimit = 32 * 1024 * 1024}