Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassComponents/Feedback/GlassTone.swift8// Module : SPGlassComponents9// 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 SwiftUI42import SPGlassCore43import SPGlassTokens44import SPGlassThemes45import SPGlassEffects4647/// The colours of an opaque surface tinted with a status colour, with a measured text contrast.48public struct GlassTintedColors: Equatable, Sendable {49 /// Fill of the surface: the theme surface mixed with a little of the tint.50 public let background: GlassRGBA51 /// Text colour, at least 4.5:1 against `background` (WCAG AA) always reachable on an opaque fill.52 public let foreground: GlassRGBA53 /// Colour of the icon and the outline: the tint when it reaches 3:1 against `background`, else `foreground`.54 public let accent: GlassRGBA5556 /// Share of the tint in the background.57 public static let tintFraction = 0.145859 /// Derives the colours from three theme roles. Pure, so that contrast can be tested for every theme.60 ///61 /// - Parameter fraction: share of the tint in the background, ``tintFraction`` by default.62 public static func resolve(tint: GlassRGBA, surface: GlassRGBA, text: GlassRGBA, fraction: Double = tintFraction) -> GlassTintedColors {63 let base = surface.isOpaque ? surface : surface.opacity(1)64 let background = base.mixed(with: tint.opacity(1), fraction: min(max(fraction, 0), 1))65 let foreground = legible(preferred: text.opacity(1), on: background, minimum: 4.5)66 let accent = tint.opacity(1).contrastRatio(against: background) >= 3 ? tint.opacity(1) : foreground67 return GlassTintedColors(background: background, foreground: foreground, accent: accent)68 }6970 /// `preferred` when it reaches `minimum` against `background`, otherwise white or black, whichever71 /// contrasts more.72 public static func legible(preferred: GlassRGBA, on background: GlassRGBA, minimum: Double) -> GlassRGBA {73 if preferred.contrastRatio(against: background) >= minimum { return preferred }74 // Against any opaque background, the better of pure white and pure black reaches at least 4.58:1.75 return GlassRGBA.white.contrastRatio(against: background) >= GlassRGBA.black.contrastRatio(against: background) ? .white : .black76 }77}7879/// SwiftUI colours of a tinted opaque surface for the current theme.80struct GlassTintedPalette {81 let background: Color82 let foreground: Color83 /// Colour of icons and edges: the tint when it is visible enough on `background`.84 let accent: Color8586 /// Uses measured colours when the theme defines the three roles. A theme that follows the system colours87 /// cannot be measured: the surface is then mixed by SwiftUI, text keeps the primary label colour and88 /// icons keep the system status colour, which the system already adapts to both appearances.89 init(_ token: GlassColorToken, theme: ResolvedGlassTheme, fraction: Double = GlassTintedColors.tintFraction) {90 if let tint = theme.rgba(token), let surface = theme.rgba(.surface), let text = theme.rgba(.textPrimary) {91 let colors = GlassTintedColors.resolve(tint: tint, surface: surface, text: text, fraction: fraction)92 background = Color(glass: colors.background)93 foreground = Color(glass: colors.foreground)94 accent = Color(glass: colors.accent)95 } else {96 background = theme.color(.surface).mix(with: theme.color(token), by: fraction)97 foreground = theme.color(.textPrimary)98 accent = theme.color(token)99 }100 }101}102103/// A solid status fill with its legible content colour.104struct GlassSolidPalette {105 let fill: Color106 let content: Color107108 /// A defined role is filled solid and uses `theme.onColor`. An undefined role follows the system colour:109 /// it is resolved in the given environment and the content is white or near-black, whichever contrasts110 /// more with it.111 init(_ token: GlassColorToken, theme: ResolvedGlassTheme, environment: EnvironmentValues) {112 fill = theme.color(token)113 if theme.rgba(token) != nil {114 content = theme.onColor(token)115 } else {116 let resolved = GlassRGBA(theme.color(token), in: environment).opacity(1)117 let dark = GlassRGBA(rgb: 0x0B0B0F)118 content = GlassRGBA.white.contrastRatio(against: resolved) >= dark.contrastRatio(against: resolved) ? .white : Color(glass: dark)119 }120 }121}122123/// SwiftUI colours of a status-tinted opaque area, for content components built on the kit.124public struct GlassTintedSwatch {125 /// Fill: the theme surface mixed with a little of the tint.126 public let background: Color127 /// Text colour on `background`.128 public let foreground: Color129 /// Icon and edge colour on `background`.130 public let accent: Color131132 /// - Parameters:133 /// - token: the status colour role.134 /// - fraction: share of the tint in the background.135 public init(_ token: GlassColorToken, theme: ResolvedGlassTheme, fraction: Double = GlassTintedColors.tintFraction) {136 let palette = GlassTintedPalette(token, theme: theme, fraction: fraction)137 background = palette.background138 foreground = palette.foreground139 accent = palette.accent140 }141}142143/// A small rounded tile holding one SF Symbol, tinted with a tone.144///145/// ```swift146/// GlassIconTile(systemImage: "shippingbox", tone: .accent)147/// ```148///149/// The tile is decoration for a row, a banner or a timeline event: it is hidden from assistive technologies,150/// so the text next to it must carry the meaning.151public struct GlassIconTile: View {152 @GlassThemed private var theme153 private let systemImage: String154 private let tone: GlassBadgeTone155 private let size: CGFloat156157 /// - Parameters:158 /// - tone: selects the colour of the symbol and of the tile's light fill.159 /// - size: side of the tile in points, at least 16.160 public init(systemImage: String, tone: GlassBadgeTone = .accent, size: CGFloat = 32) {161 self.systemImage = systemImage162 self.tone = tone163 self.size = max(16, size)164 }165166 public var body: some View {167 let colors = colors168 Image(systemName: systemImage)169 .font(.system(size: size * 0.46, weight: .semibold))170 .foregroundStyle(colors.glyph)171 .frame(width: size, height: size)172 .background(colors.fill, in: RoundedRectangle(cornerRadius: size * 0.3, style: .continuous))173 .accessibilityHidden(true)174 }175176 private var colors: (fill: Color, glyph: Color) {177 guard let token = tone.colorToken else {178 return (theme.color(.textPrimary).opacity(0.08), theme.color(.textSecondary))179 }180 let palette = GlassTintedPalette(token, theme: theme, fraction: 0.16)181 return (palette.background, palette.accent)182 }183}184185/// Meaning of a ``GlassBadge``; it selects the colour and, for a status badge, the symbol.186public enum GlassBadgeTone: String, CaseIterable, Sendable {187 case neutral, accent, success, warning, critical188189 /// Theme colour role of the fill; `nil` for the neutral surface.190 public var colorToken: GlassColorToken? {191 switch self {192 case .neutral: nil193 case .accent: .accent194 case .success: .success195 case .warning: .warning196 case .critical: .destructive197 }198 }199200 /// Symbol of a status badge. Every tone has a different shape, so the status survives without colour.201 public var statusSystemImage: String {202 switch self {203 case .neutral: "circle.fill"204 case .accent: "info.circle.fill"205 case .success: "checkmark.circle.fill"206 case .warning: "exclamationmark.triangle.fill"207 case .critical: "xmark.octagon.fill"208 }209 }210}211