// The theme reader: a writing theme as the core answers it, every colour// named by its role. The surface, the list row and the settings samples// read colours from here and nowhere else — none is written in Swift.importFoundationpublicstructWritingTheme: Hashable, Sendable, Identifiable {publicvar id: Stringpublicvar name: String/// Drawn for the phone's dark appearance.publicvar dark: Boolpublicvar colours: Colours/// One appearance's colours, by role (`notes.themes`, `writing.json`).publicstructColours: Codable, Hashable, Sendable {/// The page.publicvar ground: String/// The words.publicvar text: String/// The title and headings.publicvar heading: String/// Previews, dates, quotes, a mark at the cursor, the hint.publicvar secondary: String/// The one accent: the compose button, the caret, a bullet, a box, a tag, a link.publicvar accent: String/// Hairlines between rows, a divider, a table's grid.publicvar rule: Stringpublicvar highlight: String/// Code's ground, and a raised surface: the bar, the panel, a field.publicvar codeGround: Stringpublicvar selection: String/// Links: Notes' paper's text colour, drawn underlined so a link is never taken for the ink; the accent where/// a theme names none.publicvar link: String?/// Deleting for good, Empty bin, every act a sheet marks red: the paper's red, whatever the ink (Notes CONTRACT/// §10 "Ink"); the accent where a theme names none.publicvar danger: String?enumCodingKeys: String, CodingKey {case ground, text, heading, secondary, accent, rule, highlight, selection, link, dangercase codeGround = "code_ground" }publicinit(ground: String, text: String, heading: String, secondary: String, accent: String, rule: String, highlight: String, codeGround: String, selection: String, link: String? = nil, danger: String? = nil) {self.ground = groundself.text = textself.heading = headingself.secondary = secondaryself.accent = accentself.rule = ruleself.highlight = highlightself.codeGround = codeGroundself.selection = selectionself.link = linkself.danger = danger } }publicinit(id: String, name: String, dark: Bool, colours: Colours) {self.id = idself.name = nameself.dark = darkself.colours = colours }}/// The person's typography, as the core keeps it (`notes.settings`).publicstructWritingType: Hashable, Sendable {/// The face's id: `sans` (the bundled Nunito Sans), `figtree` (Notes'),/// `serif`, `mono`, `system`.publicvar face: String/// Code's face: `mono` (the phone's), or `jetbrains` (Notes').publicvar code: String = "mono"/// The space after a paragraph, as a multiple of the body's size.publicvar paragraph: Double = 0.15/// The body's size in points at the phone's default text size; the/// phone's text size scales it.publicvar size: Double/// The body's line height as a multiple of its size.publicvar lineHeight: Double/// The widest the text column grows, in characters of the body; 0 is the/// screen's whole width.publicvar width: Double/// The title's and each heading level's size, as a multiple of the body's.publicvar scale: Scale/// A list row's sizes.publicvar list: ListSizespublicstructScale: Hashable, Sendable {publicvar title: Doublepublicvar h1: Doublepublicvar h2: Doublepublicvar h3: Doublepublicinit(title: Double, h1: Double, h2: Double, h3: Double) {self.title = titleself.h1 = h1self.h2 = h2self.h3 = h3 }/// The surface's own steps: a title well above a heading, headings a/// little above the body.publicstaticlet standard = Scale(title: 1.6, h1: 1.4, h2: 1.22, h3: 1.08) }publicstructListSizes: Hashable, Sendable {publicvar title: Doublepublicvar preview: Doublepublicvar date: Doublepublicinit(title: Double, preview: Double, date: Double) {self.title = titleself.preview = previewself.date = date }/// A row's sizes follow the body's.publicstaticfunc standard(_ size: Double) -> ListSizes {ListSizes(title: size, preview: (size * 0.88).rounded(), date: max(11, (size * 0.72).rounded())) } }publicinit(face: String, size: Double, lineHeight: Double, width: Double, scale: Scale = .standard, list: ListSizes? = nil, code: String = "mono", paragraph: Double = 0.15) {self.face = faceself.code = codeself.paragraph = paragraphself.size = sizeself.lineHeight = lineHeightself.width = widthself.scale = scaleself.list = list ?? .standard(size) }}/// `#RRGGBB` or `#RRGGBBAA` as red, green, blue and alpha in 0…1; nil for/// anything else.publicfunc writingRGBA(_ hex: String) -> (Double, Double, Double, Double)? {var s = hex.trimmingCharacters(in: .whitespaces)if s.hasPrefix("#") { s.removeFirst() }guard s.count == 6 || s.count == 8, let v = UInt64(s, radix: 16) else { returnnil }if s.count == 6 {return (Double((v >> 16) & 0xFF) / 255, Double((v >> 8) & 0xFF) / 255, Double(v & 0xFF) / 255, 1) }return (Double((v >> 24) & 0xFF) / 255, Double((v >> 16) & 0xFF) / 255, Double((v >> 8) & 0xFF) / 255, Double(v & 0xFF) / 255)}#if canImport(UIKit)importSwiftUIimport UIKitpublicextensionWritingTheme {/// A role's colour. A colour the core wrote wrongly is drawn magenta, so/// it is seen and fixed, never quietly replaced.staticfunc ui(_ hex: String) -> UIColor {guardlet (r, g, b, a) = writingRGBA(hex) else { return .magenta }return UIColor(red: r, green: g, blue: b, alpha: a) }var ground: UIColor { Self.ui(colours.ground) }var ink: UIColor { Self.ui(colours.text) }var heading: UIColor { Self.ui(colours.heading) }var dim: UIColor { Self.ui(colours.secondary) }/// The quietest words: the empty page's hint, a mark at the cursor.var faint: UIColor { dim.withAlphaComponent(0.7) }var rule: UIColor { Self.ui(colours.rule) }var accent: UIColor { Self.ui(colours.accent) }/// What is drawn on the accent: the page's own colour.var onAccent: UIColor { ground }/// A tag chip: the accent, faint, under the accent's words.var chip: UIColor { accent.withAlphaComponent(dark ? 0.24 : 0.13) }var chipInk: UIColor { accent }var highlight: UIColor { Self.ui(colours.highlight) }var code: UIColor { Self.ui(colours.codeGround) }var link: UIColor { colours.link.map(Self.ui) ?? accent }/// A red act's colour: the paper's red, whatever the ink.publicvar danger: UIColor { colours.danger.map(Self.ui) ?? accent }var check: UIColor { accent }var selection: UIColor { Self.ui(colours.selection) }/// A raised surface: the bar, the panel, the search field, a round button.var sheet: UIColor { code }var bar: UIColor { sheet }func color(_ ui: UIColor) -> Color { Color(uiColor: ui) }}#endif