SPB Git

Repository Public

Theme.swift 12.8 KB 250 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Sources/QyvoraUI/Theme.swift8// Module           : Qyvora UI9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0412// Last Modified    : 2026-12-0513//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI4243#if canImport(UIKit)44import UIKit45#elseif canImport(AppKit)46import AppKit47#endif4849/// One colour of the palette as plain sRGB values: a light and a dark value, and optionally the values used when50/// the system setting "Increase Contrast" is on. Kept as data so that the contrast of every pair shipped by the51/// design system is computed in the unit tests (`QContrast`).52public struct QColorSpec: Sendable, Equatable {53    public let light: UInt3254    public let dark: UInt3255    public let lightHighContrast: UInt3256    public let darkHighContrast: UInt325758    public init(light: UInt32, dark: UInt32, lightHighContrast: UInt32? = nil, darkHighContrast: UInt32? = nil) {59        self.light = light60        self.dark = dark61        self.lightHighContrast = lightHighContrast ?? light62        self.darkHighContrast = darkHighContrast ?? dark63    }6465    /// The sRGB value for an appearance.66    public func rgb(dark isDark: Bool, highContrast: Bool = false) -> UInt32 {67        if highContrast { return isDark ? darkHighContrast : lightHighContrast }68        return isDark ? dark : light69    }7071    /// The dynamic SwiftUI colour.72    public var color: Color {73        let spec = self74        #if canImport(UIKit)75        return Color(UIColor { traits in76            UIColor(rgb: spec.rgb(dark: traits.userInterfaceStyle == .dark, highContrast: traits.accessibilityContrast == .high))77        })78        #elseif canImport(AppKit)79        return Color(NSColor(name: nil) { appearance in80            let match = appearance.bestMatch(from: [.darkAqua, .aqua, .accessibilityHighContrastDarkAqua, .accessibilityHighContrastAqua])81            let isDark = match == .darkAqua || match == .accessibilityHighContrastDarkAqua82            let high = match == .accessibilityHighContrastDarkAqua || match == .accessibilityHighContrastAqua83            return NSColor(rgb: spec.rgb(dark: isDark, highContrast: high))84        })85        #else86        return Color(rgb: spec.dark)87        #endif88    }89}9091/// The palette as data. `QColor` exposes the same entries as SwiftUI colours.92public enum QPalette {93    // Surfaces, from the screen background up.94    public static let background = QColorSpec(light: 0xF6F5FA, dark: 0x07070B)95    public static let surface = QColorSpec(light: 0xFFFFFF, dark: 0x12121A)96    public static let surfaceRaised = QColorSpec(light: 0xEFEDF6, dark: 0x1B1B26)97    /// Wells: text fields, tracks, the inside of a control.98    public static let surfaceSunken = QColorSpec(light: 0xECEAF3, dark: 0x0C0C12)99100    /// Hairline separating surfaces. Decorative: it never carries meaning on its own.101    public static let stroke = QColorSpec(light: 0xE0DDEA, dark: 0x2A2A38, lightHighContrast: 0x85829A, darkHighContrast: 0x6E6B80)102    /// Boundary of a control that must be visible without its fill (3:1 against every surface).103    public static let strokeStrong = QColorSpec(light: 0x85829A, dark: 0x6E6B80, lightHighContrast: 0x555263, darkHighContrast: 0x9A97A8)104105    public static let textPrimary = QColorSpec(light: 0x14131A, dark: 0xF2F0F7)106    public static let textSecondary = QColorSpec(light: 0x555263, dark: 0x9A97A8, lightHighContrast: 0x3A3845, darkHighContrast: 0xC4C1D0)107    public static let textTertiary = QColorSpec(light: 0x686577, dark: 0x8A8798, lightHighContrast: 0x4A4857, darkHighContrast: 0xB0ADBD)108109    // The two accents.110    public static let violet = QColorSpec(light: 0x6D3FE0, dark: 0x9B7BFF, lightHighContrast: 0x5126BF, darkHighContrast: 0xB9A2FF)111    public static let blue = QColorSpec(light: 0x1C5FE0, dark: 0x4C8DFF, lightHighContrast: 0x1346B0, darkHighContrast: 0x86B1FF)112    /// Fills that carry white text (primary action). The same deep values in both appearances.113    public static let violetFill = QColorSpec(light: 0x6D3FE0, dark: 0x6D3FE0)114    public static let blueFill = QColorSpec(light: 0x1C5FE0, dark: 0x1C5FE0)115    public static let onAccent = QColorSpec(light: 0xFFFFFF, dark: 0xFFFFFF)116117    // Semantic states. Always paired with a glyph and a word.118    public static let positive = QColorSpec(light: 0x0A7550, dark: 0x3DD6A0, lightHighContrast: 0x065A3D, darkHighContrast: 0x7BE8C0)119    public static let negative = QColorSpec(light: 0xC22F2F, dark: 0xFF7A7A, lightHighContrast: 0x9A1F1F, darkHighContrast: 0xFFA3A3)120    public static let warning = QColorSpec(light: 0x8A5600, dark: 0xF5B94A, lightHighContrast: 0x6B4300, darkHighContrast: 0xFFD27F)121122    /// Legacy asset tints, kept for the screens written before the chain identity system (`QChain.tint`).123    public static let bitcoin = QColorSpec(light: 0x8A5A12, dark: 0xD9A866)124    public static let ethereum = QColorSpec(light: 0x4A55A8, dark: 0xA3ACE6)125126    /// Every surface a text or glyph may sit on.127    public static let surfaces: [(name: String, spec: QColorSpec)] = [128        ("background", background), ("surface", surface), ("surfaceRaised", surfaceRaised), ("surfaceSunken", surfaceSunken),129    ]130    /// Foregrounds used for body text: 4.5:1 on every surface.131    public static let textForegrounds: [(name: String, spec: QColorSpec)] = [132        ("textPrimary", textPrimary), ("textSecondary", textSecondary), ("textTertiary", textTertiary),133        ("violet", violet), ("blue", blue), ("positive", positive), ("negative", negative), ("warning", warning),134    ]135}136137/// QYVORA palette: deep black and graphite surfaces, one violet and one electric blue accent.138/// Every colour has a light and a dark value; the accents are used sparingly (actions, state, one chart line).139public enum QColor {140    static func dynamic(light: UInt32, dark: UInt32) -> Color { QColorSpec(light: light, dark: dark).color }141142    public static let background = QPalette.background.color143    public static let surface = QPalette.surface.color144    public static let surfaceRaised = QPalette.surfaceRaised.color145    public static let surfaceSunken = QPalette.surfaceSunken.color146    public static let stroke = QPalette.stroke.color147    public static let strokeStrong = QPalette.strokeStrong.color148    public static let textPrimary = QPalette.textPrimary.color149    public static let textSecondary = QPalette.textSecondary.color150    public static let textTertiary = QPalette.textTertiary.color151    public static let violet = QPalette.violet.color152    public static let blue = QPalette.blue.color153    public static let positive = QPalette.positive.color154    public static let negative = QPalette.negative.color155    public static let warning = QPalette.warning.color156    public static let bitcoin = QPalette.bitcoin.color157    public static let ethereum = QPalette.ethereum.color158    /// Text on a filled accent button.159    public static let onAccent = Color.white160161    /// The signature stroke: violet to blue, used for the primary action and the mark only. Built from the deep162    /// fill values so that white text keeps a 4.5:1 contrast along the whole gradient, in both appearances.163    public static let accentGradient = LinearGradient(colors: [QPalette.violetFill.color, QPalette.blueFill.color],164                                                      startPoint: .topLeading, endPoint: .bottomTrailing)165    /// The same stroke in the lighter accent values, for lines drawn on a surface (one chart line, a progress track).166    public static let accentLine = LinearGradient(colors: [violet, blue], startPoint: .leading, endPoint: .trailing)167}168169extension Color {170    init(rgb: UInt32) {171        self.init(.sRGB, red: Double((rgb >> 16) & 0xff) / 255, green: Double((rgb >> 8) & 0xff) / 255, blue: Double(rgb & 0xff) / 255, opacity: 1)172    }173}174175#if canImport(UIKit)176extension UIColor {177    convenience init(rgb: UInt32) {178        self.init(red: CGFloat((rgb >> 16) & 0xff) / 255, green: CGFloat((rgb >> 8) & 0xff) / 255, blue: CGFloat(rgb & 0xff) / 255, alpha: 1)179    }180}181#elseif canImport(AppKit)182extension NSColor {183    convenience init(rgb: UInt32) {184        self.init(srgbRed: CGFloat((rgb >> 16) & 0xff) / 255, green: CGFloat((rgb >> 8) & 0xff) / 255, blue: CGFloat(rgb & 0xff) / 255, alpha: 1)185    }186}187#endif188189/// Type scale. Dynamic Type is honoured everywhere (text styles, no fixed sizes except the balance figure, which190/// shrinks to fit). Hierarchy comes from size and weight, not from colour: one display figure per screen, one title191/// per card, body, then captions in the secondary text colour.192public enum QFont {193    public static let balance = Font.system(size: 44, weight: .semibold, design: .rounded).monospacedDigit()194    /// The one large figure of a screen (portfolio total, amount being sent). Scales with Dynamic Type.195    public static let display = Font.system(.largeTitle, design: .rounded, weight: .semibold).monospacedDigit()196    public static let titleLarge = Font.system(.title, design: .default, weight: .bold)197    public static let title = Font.system(.title2, design: .default, weight: .semibold)198    public static let headline = Font.system(.headline, design: .default, weight: .semibold)199    public static let subheadline = Font.system(.subheadline, design: .default, weight: .medium)200    public static let body = Font.system(.body)201    public static let callout = Font.system(.callout)202    public static let footnote = Font.system(.footnote)203    public static let caption = Font.system(.caption)204    public static let captionStrong = Font.system(.caption, design: .default, weight: .semibold)205    public static let amount = Font.system(.body, design: .rounded, weight: .medium).monospacedDigit()206    /// Secondary figures (fiat line, percentage).207    public static let figure = Font.system(.footnote, design: .rounded, weight: .medium).monospacedDigit()208    public static let mono = Font.system(.footnote, design: .monospaced)209    /// Addresses shown in full for comparison.210    public static let monoLarge = Font.system(.callout, design: .monospaced, weight: .medium)211    public static let eyebrow = Font.system(.caption2, design: .default, weight: .semibold)212    /// Tags such as "TEST": capitals with tracking (`QMetric.tagKerning`).213    public static let tag = Font.system(.caption2, design: .default, weight: .bold)214}215216public enum QMetric {217    public static let radius: CGFloat = 18218    public static let radiusSmall: CGFloat = 12219    /// Tags, sub-badges and other small inline shapes.220    public static let radiusTiny: CGFloat = 6221    /// Sheets and the hero card.222    public static let radiusLarge: CGFloat = 26223    public static let padding: CGFloat = 16224    public static let spacing: CGFloat = 12225    /// Horizontal margin of a screen.226    public static let screenPadding: CGFloat = 20227    /// Vertical distance between two sections of a screen.228    public static let sectionSpacing: CGFloat = 28229    public static let hairline: CGFloat = 1230    /// Minimum size of anything that can be tapped.231    public static let minTap: CGFloat = 44232    /// Minimum height of a list row.233    public static let rowMinHeight: CGFloat = 60234    /// Height of a primary or secondary button.235    public static let controlHeight: CGFloat = 52236    public static let tagKerning: CGFloat = 0.9237}238239/// Spacing scale on a 4-point grid. Components use these steps only.240public enum QSpace {241    public static let xxs: CGFloat = 2242    public static let xs: CGFloat = 4243    public static let s: CGFloat = 8244    public static let m: CGFloat = 12245    public static let l: CGFloat = 16246    public static let xl: CGFloat = 24247    public static let xxl: CGFloat = 32248    public static let xxxl: CGFloat = 48249}250
Files main
Menu

Theme