Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassTokens/GlassColorToken.swift8// Module : SPGlassTokens9// 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/// Semantic colour roles. Components ask for a role, never for a literal colour.44public enum GlassColorToken: String, Codable, CaseIterable, Sendable {45 case primary46 case secondary47 case accent48 case destructive49 case success50 case warning51 case background52 case surface53 case textPrimary54 case textSecondary55 case separator56 case focusRing57}5859/// Colours of one appearance (light or dark). A role without a value follows the system colour for that role.60public struct GlassPalette: Hashable, Sendable {61 private var storage: [GlassColorToken: GlassRGBA]6263 public init(_ values: [GlassColorToken: GlassRGBA] = [:]) {64 storage = values65 }6667 public subscript(token: GlassColorToken) -> GlassRGBA? {68 get { storage[token] }69 set { storage[token] = newValue }70 }7172 /// Roles that have an explicit value, in declaration order.73 public var definedTokens: [GlassColorToken] {74 GlassColorToken.allCases.filter { storage[$0] != nil }75 }7677 public var isEmpty: Bool { storage.isEmpty }7879 /// Values of `other` replace the values of `self`; roles absent from `other` are kept.80 public func merging(_ other: GlassPalette) -> GlassPalette {81 GlassPalette(storage.merging(other.storage) { _, new in new })82 }83}8485extension GlassPalette: Codable {86 private struct Key: CodingKey {87 var stringValue: String88 var intValue: Int? { nil }89 init(stringValue: String) { self.stringValue = stringValue }90 init?(intValue: Int) { nil }91 }9293 public init(from decoder: any Decoder) throws {94 let container = try decoder.container(keyedBy: Key.self)95 var values: [GlassColorToken: GlassRGBA] = [:]96 for key in container.allKeys {97 guard let token = GlassColorToken(rawValue: key.stringValue) else {98 throw DecodingError.dataCorruptedError(forKey: key, in: container, debugDescription: "Unknown colour token \"\(key.stringValue.prefix(48))\".")99 }100 values[token] = try container.decode(GlassRGBA.self, forKey: key)101 }102 storage = values103 }104105 public func encode(to encoder: any Encoder) throws {106 var container = encoder.container(keyedBy: Key.self)107 for token in GlassColorToken.allCases {108 if let value = storage[token] {109 try container.encode(value, forKey: Key(stringValue: token.rawValue))110 }111 }112 }113}114