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 SwiftUI4243/// The colours of an opaque surface tinted with a status colour, with a measured text contrast.44public struct GlassTintedColors: Equatable, Sendable {45 /// Fill of the surface: the theme surface mixed with a little of the tint.46 public let background: GlassRGBA47 /// Text colour, at least 4.5:1 against `background` (WCAG AA) always reachable on an opaque fill.48 public let foreground: GlassRGBA49 /// Colour of the icon and the outline: the tint when it reaches 3:1 against `background`, else `foreground`.50 public let accent: GlassRGBA5152 /// Share of the tint in the background.53 public static let tintFraction = 0.145455 /// Derives the colours from three theme roles. Pure, so that contrast can be tested for every theme.56 public static func resolve(tint: GlassRGBA, surface: GlassRGBA, text: GlassRGBA) -> GlassTintedColors {57 let base = surface.isOpaque ? surface : surface.opacity(1)58 let background = base.mixed(with: tint.opacity(1), fraction: tintFraction)59 let foreground = legible(preferred: text.opacity(1), on: background, minimum: 4.5)60 let accent = tint.opacity(1).contrastRatio(against: background) >= 3 ? tint.opacity(1) : foreground61 return GlassTintedColors(background: background, foreground: foreground, accent: accent)62 }6364 /// `preferred` when it reaches `minimum` against `background`, otherwise white or black, whichever65 /// contrasts more.66 public static func legible(preferred: GlassRGBA, on background: GlassRGBA, minimum: Double) -> GlassRGBA {67 if preferred.contrastRatio(against: background) >= minimum { return preferred }68 // Against any opaque background, the better of pure white and pure black reaches at least 4.58:1.69 return GlassRGBA.white.contrastRatio(against: background) >= GlassRGBA.black.contrastRatio(against: background) ? .white : .black70 }71}7273/// SwiftUI colours of a tinted opaque surface for the current theme.74struct GlassTintedPalette {75 let background: Color76 let foreground: Color77 let accent: Color7879 /// Uses measured colours when the theme defines the three roles. A theme that follows the system colours80 /// cannot be measured: the surface is then mixed by SwiftUI and text keeps the primary label colour.81 init(_ token: GlassColorToken, theme: ResolvedGlassTheme) {82 if let tint = theme.rgba(token), let surface = theme.rgba(.surface), let text = theme.rgba(.textPrimary) {83 let colors = GlassTintedColors.resolve(tint: tint, surface: surface, text: text)84 background = Color(glass: colors.background)85 foreground = Color(glass: colors.foreground)86 accent = Color(glass: colors.accent)87 } else {88 background = theme.color(.surface).mix(with: theme.color(token), by: GlassTintedColors.tintFraction)89 foreground = theme.color(.textPrimary)90 accent = theme.color(.textPrimary)91 }92 }93}9495/// A solid status fill with its legible content colour.96struct GlassSolidPalette {97 let fill: Color98 let content: Color99100 /// A defined role is filled solid and uses `theme.onColor`. An undefined role (system theme) falls back to101 /// the tinted surface, because the contrast of white on a system colour cannot be measured.102 init(_ token: GlassColorToken, theme: ResolvedGlassTheme) {103 if theme.rgba(token) != nil {104 fill = theme.color(token)105 content = theme.onColor(token)106 } else {107 let tinted = GlassTintedPalette(token, theme: theme)108 fill = tinted.background109 content = tinted.foreground110 }111 }112}113