~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/knotless

git that lives in your Atmosphere account

Forked from chadtmiller.com/knotless

0
0
24 lines · 875 B Raw History
import Foundation

/// The pattern and hue an account with no picture shows, as `identicon` and
/// `hue` in web/src/lib/format.ts make them, so it's the same on the pages.
enum Identicon {
    static func hue(_ seed: String) -> Int {
        var h: UInt32 = 7
        for byte in seed.utf8 { h = h &* 31 &+ UInt32(byte) }
        return Int(h % 360)
    }

    /// The cells of a 5×5 pattern, each its column and row, mirrored left to right.
    static func cells(_ seed: String) -> [(x: Int, y: Int)] {
        var h: UInt32 = 0x811c9dc5
        for byte in seed.utf8 { h = (h ^ UInt32(byte)) &* 0x01000193 }
        var cells: [(x: Int, y: Int)] = []
        for i in 0..<15 where (h >> UInt32(i)) & 1 == 1 {
            let (x, y) = (i / 5, i % 5)
            cells.append((x, y))
            if x < 2 { cells.append((4 - x, y)) }
        }
        return cells
    }
}