SPB Git

Repository Public

Theme.swift 7.4 KB 144 lines swift

1//2//  VQuant iOS3//  File: VQuant/DesignSystem/Theme.swift4//  Description: Design tokens shared by the app and its widgets: the Onyx palette of the web app, gain / loss colours,5//               number formats, and the sparkle mark.6//7//  Author: Simon-Pierre Boucher8//  Contact: contact@spboucher.ai9//1011import SwiftUI12import UIKit13import VQuantUI1415/// The app's colour roles, mapped onto the shared "VQuant Next" tokens (`VQTokens`, generated from the web16/// repository's `tokens.json`): ink and paper surfaces, hairlines, and the Ember accent used sparingly.17enum Theme {18    static let background = VQTokens.Palette.ink019    static let surface = VQTokens.Palette.ink220    static let surface2 = VQTokens.Palette.ink321    static let border = VQTokens.Palette.line22    static let textSecondary = VQTokens.Palette.text223    /// Which pair colours gains and losses: classic green / red, or the colour-vision-safe blue / orange. Set from the24    /// account preference at launch and when it changes; direction is never colour alone (a sign and an arrow too).25    nonisolated(unsafe) static var upDown = VQTokens.UpDown.classic26    static var up: Color { upDown.up }27    static var down: Color { upDown.down }28    static let flat = VQTokens.Palette.text229    static let warning = VQTokens.Palette.warn30    static let info = VQTokens.Palette.info31    static let critical = VQTokens.Palette.critical32    static let brand = LinearGradient(colors: [Color(hex: 0xFF8A2A), Color(hex: 0xFF3D6E), Color(hex: 0xA855F7)], startPoint: .topLeading, endPoint: .bottomTrailing)33    /// Accent for text, icons and controls: the tokens' text-safe ember. The brand orange (#FF6A1A, `ember`) reaches34    /// only 2.9:1 on a light background (audit of 1.0), so it is kept for fills behind dark text.35    static let orange = VQTokens.Palette.accentText36    /// The brand ember itself: fills only (primary buttons, the AI mark), with `emberInk` on top.37    static let ember = VQTokens.Palette.accent38    static let emberInk = VQTokens.Palette.accentFg39    static let pink = Color(hex: 0xF0356F)4041    static func direction(_ value: Double?) -> Color {42        guard let value, value != 0 else { return flat }43        return value > 0 ? up : down44    }4546    static let corner: CGFloat = VQTokens.Radius.lg47}4849extension Font {50    /// New York, the display serif of the design system, at a Dynamic Type text style.51    static func serif(_ style: Font.TextStyle, weight: Font.Weight = .semibold) -> Font { .system(style, design: .serif).weight(weight) }52    /// SF Mono with tabular figures at a text style (numbers and code).53    static func mono(_ style: Font.TextStyle, weight: Font.Weight = .regular) -> Font { .system(style, design: .monospaced).weight(weight) }54}5556extension UIColor {57    convenience init(hex: UInt32) {58        self.init(red: CGFloat((hex >> 16) & 0xFF) / 255, green: CGFloat((hex >> 8) & 0xFF) / 255, blue: CGFloat(hex & 0xFF) / 255, alpha: 1)59    }60}61extension Color {62    init(hex: UInt32) { self.init(UIColor(hex: hex)) }63}6465/// Number formats of the app (French Canada conventions, tabular digits applied by the views).66enum Fmt {67    static let locale = Locale(identifier: "fr_CA")6869    static func price(_ v: Double?) -> String {70        guard let v else { return "—" }71        let digits = abs(v) < 1 ? 4 : 272        return v.formatted(.number.precision(.fractionLength(digits)).locale(locale))73    }74    static func money(_ v: Double?, currency: String = "USD") -> String {75        guard let v else { return "—" }76        return v.formatted(.currency(code: currency).precision(.fractionLength(2)).locale(locale))77    }78    static func signed(_ v: Double?) -> String {79        guard let v else { return "—" }80        return v.formatted(.number.precision(.fractionLength(abs(v) < 0.01 && v != 0 ? 4 : 2)).sign(strategy: .always(includingZero: false)).locale(locale))81    }82    /// `v` is already in percent (1.25 → +1,25 %).83    static func percent(_ v: Double?, digits: Int = 2) -> String {84        guard let v else { return "—" }85        return v.formatted(.number.precision(.fractionLength(digits)).sign(strategy: .always(includingZero: false)).locale(locale)) + " %"86    }87    /// `v` is a fraction (0.0125 → +1,25 %).88    static func fraction(_ v: Double?, digits: Int = 2) -> String { percent(v.map { $0 * 100 }, digits: digits) }89    static func compact(_ v: Double?) -> String {90        guard let v else { return "—" }91        return v.formatted(.number.notation(.compactName).precision(.significantDigits(3)).locale(locale))92    }93    static func quantity(_ v: Double) -> String { v.formatted(.number.precision(.fractionLength(0...6)).locale(locale)) }94    static func time(_ d: Date?) -> String {95        guard let d else { return "—" }96        return d.formatted(Date.FormatStyle(date: .omitted, time: .shortened, locale: locale))97    }98    static func dateTime(_ d: Date?) -> String {99        guard let d else { return "—" }100        return d.formatted(Date.FormatStyle(date: .abbreviated, time: .shortened, locale: locale))101    }102    static func day(_ d: Date?) -> String {103        guard let d else { return "—" }104        return d.formatted(Date.FormatStyle(date: .abbreviated, time: .omitted, locale: locale))105    }106    static func relative(_ d: Date?, now: Date = Date()) -> String {107        guard let d else { return "—" }108        let f = RelativeDateTimeFormatter(); f.locale = locale; f.unitsStyle = .short109        return abs(now.timeIntervalSince(d)) < 5 ? "à l’instant" : f.localizedString(for: d, relativeTo: now)110    }111    static func duration(_ seconds: TimeInterval) -> String {112        let s = Int(max(0, seconds))113        return s < 60 ? "\(s) s" : s < 3600 ? "\(s / 60) min \(s % 60) s" : "\(s / 3600) h \((s % 3600) / 60) min"114    }115    static func bytes(_ n: Int) -> String { ByteCountFormatter.string(fromByteCount: Int64(n), countStyle: .file) }116    /// `forecast_horizon` → `Forecast horizon`.117    static func label(_ key: String) -> String {118        let words = key.replacingOccurrences(of: "_", with: " ").replacingOccurrences(of: ".", with: " · ")119        return words.prefix(1).uppercased() + words.dropFirst()120    }121}122123/// The VQuant mark (`shared/vqLogo.ts`), drawn natively.124struct SparkleMark: View {125    var body: some View {126        Canvas { ctx, size in127            let s = min(size.width, size.height) / 64128            func star(_ p: [(CGFloat, CGFloat)]) -> Path {129                var path = Path(); path.move(to: CGPoint(x: p[0].0 * s, y: p[0].1 * s))130                var i = 1131                while i + 2 < p.count {132                    path.addCurve(to: CGPoint(x: p[i + 2].0 * s, y: p[i + 2].1 * s), control1: CGPoint(x: p[i].0 * s, y: p[i].1 * s), control2: CGPoint(x: p[i + 1].0 * s, y: p[i + 1].1 * s)); i += 3133                }134                path.closeSubpath(); return path135            }136            var path = star([(27, 9), (28.9, 23.6), (33.4, 30.1), (51, 35), (33.4, 39.9), (28.9, 46.4), (27, 61), (25.1, 46.4), (20.6, 39.9), (3, 35), (20.6, 30.1), (25.1, 23.6), (27, 9)])137            path.addPath(star([(50, 3), (50.8, 9.1), (52.9, 11.9), (60, 14), (52.9, 16.1), (50.8, 18.9), (50, 25), (49.2, 18.9), (47.1, 16.1), (40, 14), (47.1, 11.9), (49.2, 9.1), (50, 3)]))138            ctx.fill(path, with: .linearGradient(Gradient(colors: [Color(hex: 0xFF8A2A), Color(hex: 0xFF3D6E), Color(hex: 0xA855F7)]), startPoint: CGPoint(x: 6 * s, y: 8 * s), endPoint: CGPoint(x: 58 * s, y: 58 * s)))139        }140        .aspectRatio(1, contentMode: .fit)141        .accessibilityHidden(true)142    }143}144
Files main
Menu

Theme