Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassThemes/GlassThemeValidator.swift8// Module : SPGlassThemes9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//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 : MIT33//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 Foundation4243/// A finding of ``GlassThemeValidator``.44public struct GlassThemeIssue: Hashable, Sendable, CustomStringConvertible {45 public enum Severity: String, Sendable { case error, warning }4647 public enum Code: String, Sendable {48 case invalidIdentifier, emptyName, nameTooLong, summaryTooLong, unsupportedSchema49 case spacingOutOfRange, spacingNotAscending, radiusOutOfRange, durationOutOfRange50 case lowTextContrast, lowStatusContrast, translucentText51 }5253 public let severity: Severity54 public let code: Code55 public let message: String5657 public var description: String { "\(severity.rawValue): \(message)" }58}5960/// Checks a theme for values that cannot be used (errors) or that harm legibility (warnings).61public enum GlassThemeValidator {62 /// WCAG AA threshold for body text.63 public static let minimumTextContrast = 4.564 /// WCAG AA threshold for large text and meaningful graphics.65 public static let minimumGraphicContrast = 3.06667 public static func validate(_ theme: GlassTheme) -> [GlassThemeIssue] {68 var issues: [GlassThemeIssue] = []69 func add(_ severity: GlassThemeIssue.Severity, _ code: GlassThemeIssue.Code, _ message: String) {70 issues.append(GlassThemeIssue(severity: severity, code: code, message: message))71 }7273 if theme.schemaVersion < 1 || theme.schemaVersion > GlassKitInfo.schemaVersion {74 add(.error, .unsupportedSchema, "Schema version \(theme.schemaVersion) is not supported (current: \(GlassKitInfo.schemaVersion)).")75 }76 if !theme.id.isValid {77 add(.error, .invalidIdentifier, "The identifier must be 1 to 64 lower-case letters, digits or hyphens.")78 }79 if theme.name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {80 add(.error, .emptyName, "The theme has no name.")81 }82 if theme.name.count > 80 { add(.error, .nameTooLong, "The name exceeds 80 characters.") }83 if theme.summary.count > 500 { add(.error, .summaryTooLong, "The summary exceeds 500 characters.") }8485 let spacing = theme.spacing.values86 if spacing.contains(where: { !$0.isFinite || $0 < 0 || $0 > 128 }) {87 add(.error, .spacingOutOfRange, "Spacing values must be between 0 and 128 points.")88 } else if zip(spacing, spacing.dropFirst()).contains(where: { $0 > $1 }) {89 add(.warning, .spacingNotAscending, "The spacing scale is not ascending.")90 }91 if theme.radii.values.contains(where: { !$0.isFinite || $0 < 0 || $0 > 64 }) {92 add(.error, .radiusOutOfRange, "Corner radii must be between 0 and 64 points.")93 }94 let durations = [theme.motion.fast, theme.motion.standard, theme.motion.slow]95 if durations.contains(where: { !$0.isFinite || $0 < 0 || $0 > 2 }) {96 add(.error, .durationOutOfRange, "Animation durations must be between 0 and 2 seconds.")97 }9899 for (label, palette) in [("light", theme.light), ("dark", theme.dark)] {100 issues.append(contentsOf: contrastIssues(palette: palette, appearance: label))101 }102 return issues103 }104105 /// True when the theme has no error. Warnings do not block import or export.106 public static func isUsable(_ theme: GlassTheme) -> Bool {107 !validate(theme).contains { $0.severity == .error }108 }109110 /// Contrast findings for one palette. Roles left to the system are skipped: their colours are not known here.111 public static func contrastIssues(palette: GlassPalette, appearance: String) -> [GlassThemeIssue] {112 var issues: [GlassThemeIssue] = []113 let backgrounds: [(GlassColorToken, GlassRGBA)] = [.background, .surface].compactMap { token in114 palette[token].map { (token, $0) }115 }116 for text in [GlassColorToken.textPrimary, .textSecondary] {117 guard let colour = palette[text] else { continue }118 if !colour.isOpaque {119 issues.append(.init(severity: .warning, code: .translucentText, message: "\(appearance): \(text.rawValue) is translucent; its contrast depends on what is behind it."))120 }121 for (name, background) in backgrounds {122 let ratio = colour.contrastRatio(against: background)123 if ratio < minimumTextContrast {124 issues.append(.init(severity: .warning, code: .lowTextContrast, message: "\(appearance): \(text.rawValue) on \(name.rawValue) has a contrast of \(format(ratio)):1, below \(format(minimumTextContrast)):1."))125 }126 }127 }128 for status in [GlassColorToken.primary, .accent, .destructive, .success, .warning, .focusRing] {129 guard let colour = palette[status], let background = palette[.background] else { continue }130 let ratio = colour.contrastRatio(against: background)131 if ratio < minimumGraphicContrast {132 issues.append(.init(severity: .warning, code: .lowStatusContrast, message: "\(appearance): \(status.rawValue) on background has a contrast of \(format(ratio)):1, below \(format(minimumGraphicContrast)):1."))133 }134 }135 return issues136 }137138 private static func format(_ value: Double) -> String {139 String(format: "%.2f", locale: Locale(identifier: "en_US_POSIX"), value)140 }141}142