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 Foundation42import SPGlassCore43import SPGlassTokens4445/// A finding of ``GlassThemeValidator``.46public struct GlassThemeIssue: Hashable, Sendable, CustomStringConvertible {47 public enum Severity: String, Sendable { case error, warning }4849 public enum Code: String, Sendable {50 case invalidIdentifier, emptyName, nameTooLong, summaryTooLong, unsupportedSchema51 case spacingOutOfRange, spacingNotAscending, radiusOutOfRange, durationOutOfRange52 case lowTextContrast, lowStatusContrast, translucentText53 }5455 public let severity: Severity56 public let code: Code57 public let message: String5859 public var description: String { "\(severity.rawValue): \(message)" }60}6162/// Checks a theme for values that cannot be used (errors) or that harm legibility (warnings).63public enum GlassThemeValidator {64 /// WCAG AA threshold for body text.65 public static let minimumTextContrast = 4.566 /// WCAG AA threshold for large text and meaningful graphics.67 public static let minimumGraphicContrast = 3.06869 public static func validate(_ theme: GlassTheme) -> [GlassThemeIssue] {70 var issues: [GlassThemeIssue] = []71 func add(_ severity: GlassThemeIssue.Severity, _ code: GlassThemeIssue.Code, _ message: String) {72 issues.append(GlassThemeIssue(severity: severity, code: code, message: message))73 }7475 if theme.schemaVersion < 1 || theme.schemaVersion > GlassKitInfo.schemaVersion {76 add(.error, .unsupportedSchema, "Schema version \(theme.schemaVersion) is not supported (current: \(GlassKitInfo.schemaVersion)).")77 }78 if !theme.id.isValid {79 add(.error, .invalidIdentifier, "The identifier must be 1 to 64 lower-case letters, digits or hyphens.")80 }81 if theme.name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {82 add(.error, .emptyName, "The theme has no name.")83 }84 if theme.name.count > 80 { add(.error, .nameTooLong, "The name exceeds 80 characters.") }85 if theme.summary.count > 500 { add(.error, .summaryTooLong, "The summary exceeds 500 characters.") }8687 let spacing = theme.spacing.values88 if spacing.contains(where: { !$0.isFinite || $0 < 0 || $0 > 128 }) {89 add(.error, .spacingOutOfRange, "Spacing values must be between 0 and 128 points.")90 } else if zip(spacing, spacing.dropFirst()).contains(where: { $0 > $1 }) {91 add(.warning, .spacingNotAscending, "The spacing scale is not ascending.")92 }93 if theme.radii.values.contains(where: { !$0.isFinite || $0 < 0 || $0 > 64 }) {94 add(.error, .radiusOutOfRange, "Corner radii must be between 0 and 64 points.")95 }96 let durations = [theme.motion.fast, theme.motion.standard, theme.motion.slow]97 if durations.contains(where: { !$0.isFinite || $0 < 0 || $0 > 2 }) {98 add(.error, .durationOutOfRange, "Animation durations must be between 0 and 2 seconds.")99 }100101 for (label, palette) in [("light", theme.light), ("dark", theme.dark)] {102 issues.append(contentsOf: contrastIssues(palette: palette, appearance: label))103 }104 return issues105 }106107 /// True when the theme has no error. Warnings do not block import or export.108 public static func isUsable(_ theme: GlassTheme) -> Bool {109 !validate(theme).contains { $0.severity == .error }110 }111112 /// Contrast findings for one palette. Roles left to the system are skipped: their colours are not known here.113 public static func contrastIssues(palette: GlassPalette, appearance: String) -> [GlassThemeIssue] {114 var issues: [GlassThemeIssue] = []115 let backgrounds: [(GlassColorToken, GlassRGBA)] = [.background, .surface].compactMap { token in116 palette[token].map { (token, $0) }117 }118 for text in [GlassColorToken.textPrimary, .textSecondary] {119 guard let colour = palette[text] else { continue }120 if !colour.isOpaque {121 issues.append(.init(severity: .warning, code: .translucentText, message: "\(appearance): \(text.rawValue) is translucent; its contrast depends on what is behind it."))122 }123 for (name, background) in backgrounds {124 let ratio = colour.contrastRatio(against: background)125 if ratio < minimumTextContrast {126 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."))127 }128 }129 }130 for status in [GlassColorToken.primary, .accent, .destructive, .success, .warning, .focusRing] {131 guard let colour = palette[status], let background = palette[.background] else { continue }132 let ratio = colour.contrastRatio(against: background)133 if ratio < minimumGraphicContrast {134 issues.append(.init(severity: .warning, code: .lowStatusContrast, message: "\(appearance): \(status.rawValue) on background has a contrast of \(format(ratio)):1, below \(format(minimumGraphicContrast)):1."))135 }136 }137 return issues138 }139140 private static func format(_ value: Double) -> String {141 String(format: "%.2f", locale: Locale(identifier: "en_US_POSIX"), value)142 }143}144