Cotton code
105 lines · 4.7 KB · SwiftRaw
// Cotton Story's look (contract, 17 Nov 2026): typographic, flat and calm.// A near-white ground in light and a near-black one in dark, following the// phone; one ink each; one accent, a warm red, for the current thing, the// caret and the Plus, never for decoration. Every Story surface (its faces,// the writer, the script reader) takes its colours from here and nowhere// else, so the two themes are one decision.//// Unlike the other apps' materials these flip with the phone's appearance:// Story is the one app whose owner asked for both themes. Built with// `UIColor(red:green:blue:alpha:)` inside a dynamic provider, the one// constructor this file is exempt from (`swift-sweep-palette-guard`).import SwiftUI#if canImport(UIKit)import UIKit#endifpublic enum StoryMaterial: CaseIterable, Sendable {    /// The ground every Story surface lies on: the paper, #F4ECDE / #121212.    case ground    /// A card or a page laid on the ground: a soft tone step off it.    case sheet    /// The script reader's page: white in light, the sheet's tone in dark.    case page    /// The words: #161616 / #ECECE6.    case ink    /// Words that step back: a count, a status, a scene number.    case quiet    /// Words further back still: the hint in an empty field.    case faint    /// The hairline round a card and under a heading.    case hairline    /// One speaker's bubble in the chat face, the left side.    case bubble    /// The other speaker's bubble, the right side: the ink itself.    case bubbleOther    /// The words in the right side's bubble.    case bubbleOtherInk    /// The one accent: #E5482D. The current thing, the caret, the Plus.    case accent    /// Light and dark, as 0...1 channels.    private var channels: (light: (Double, Double, Double, Double), dark: (Double, Double, Double, Double)) {        switch self {        case .ground: ((0.957, 0.925, 0.871, 1), (0.071, 0.071, 0.071, 1))        case .sheet: ((1, 1, 1, 1), (0.110, 0.110, 0.106, 1))        case .page: ((1, 1, 1, 1), (0.110, 0.110, 0.106, 1))        case .ink: ((0.086, 0.086, 0.086, 1), (0.925, 0.925, 0.902, 1))        case .quiet: ((0.086, 0.086, 0.086, 0.58), (0.925, 0.925, 0.902, 0.6))        case .faint: ((0.086, 0.086, 0.086, 0.34), (0.925, 0.925, 0.902, 0.36))        case .hairline: ((0.086, 0.086, 0.086, 0.12), (0.925, 0.925, 0.902, 0.14))        case .bubble: ((0.925, 0.925, 0.906, 1), (0.165, 0.165, 0.161, 1))        case .bubbleOther: ((0.086, 0.086, 0.086, 1), (0.925, 0.925, 0.902, 1))        case .bubbleOtherInk: ((0.980, 0.980, 0.969, 1), (0.071, 0.071, 0.071, 1))        case .accent: ((0.898, 0.282, 0.176, 1), (0.937, 0.341, 0.239, 1))        }    }    #if canImport(UIKit)    /// The colour for UIKit (the writer's text view), following the trait.    /// One instance per material, so the same material is the same colour    /// wherever it is asked for (a fresh dynamic colour never equals another).    public var uiColour: UIColor { Self.uiColours[self]! }    private static let uiColours: [StoryMaterial: UIColor] = Dictionary(uniqueKeysWithValues: allCases.map { material in        let (light, dark) = material.channels        return (material, UIColor { traits in            let c = traits.userInterfaceStyle == .dark ? dark : light            return UIColor(red: c.0, green: c.1, blue: c.2, alpha: c.3)        })    })    /// The colour, following the phone's appearance.    public var colour: Color { Color(uiColor: uiColour) }    #else    public var colour: Color {        let c = channels.light        return Color(.sRGB, red: c.0, green: c.1, blue: c.2, opacity: c.3)    }    #endif    /// The light colour alone, for what is measured rather than drawn (a    /// ground's luminance) and for the PDF, which is always paper.    public var lightColour: Color {        let c = channels.light        return Color(.sRGB, red: c.0, green: c.1, blue: c.2, opacity: c.3)    }}/// Story's type: the script in Courier Prime, the interface in the system's/// own sans. Both grow with the phone's text size (`relativeTo:`), and both/// fall back to the system's fonts for a script Courier Prime does not/// carry (Telugu draws in the system's Telugu face beside the Latin).public enum StoryType {    /// Script text: a scene's body, a heading, the reader's page.    public static func script(_ size: CGFloat, relativeTo style: Font.TextStyle = .body) -> Font {        _ = CottonFonts.type(size)        return .custom(CottonFonts.typeFace, size: size, relativeTo: style)    }    /// Interface text in the system sans.    public static func sans(_ style: Font.TextStyle, weight: Font.Weight = .regular) -> Font {        .system(style, design: .default, weight: weight)    }}