Cotton code
41 lines · 1.5 KB · SwiftRaw
// The twelve enamels as flat colours — what a cap is in a theme whose caps are// flat discs rather than photographed bottle caps (`ThemeCapTexture.flat`).// Each is the enamel's own colour read off its photograph (`cap-<colour>`),// so a database keeps its colour when its theme flattens its cap. The colour// of an OBJECT, like the dyes beside it, not a role of the palette.import SwiftUIextension CapColour {    /// The enamel, flat.    public var flat: ThemeColor {        let hex: String        switch self {        case .cream: hex = "#EFE6D2"        case .red: hex = "#C23B32"        case .navy: hex = "#22344F"        case .yellow: hex = "#E3B53A"        case .green: hex = "#3E6B48"        case .silver: hex = "#B9BCBF"        case .orange: hex = "#D9722E"        case .burgundy: hex = "#6E2333"        case .sky: hex = "#8DB7D9"        case .pink: hex = "#E8A5B0"        case .black: hex = "#222222"        case .teal: hex = "#2F7A7A"        }        return ThemeColor(hex: hex) ?? ThemeColor(red: 0.5, green: 0.5, blue: 0.5)    }}extension CapColour {    /// The enamel nearest `colour` — what a cap wears for a colour it has no    /// photograph of (a brand's accent).    public static func nearest(to colour: ThemeColor) -> CapColour {        func distance(_ cap: CapColour) -> Double {            let flat = cap.flat            return pow(flat.red - colour.red, 2) + pow(flat.green - colour.green, 2) + pow(flat.blue - colour.blue, 2)        }        return allCases.min { distance($0) < distance($1) } ?? .navy    }}