diff --git a/bitchat/Utils/Color+Peer.swift b/bitchat/Utils/Color+Peer.swift index bb5ae528..9a307a02 100644 --- a/bitchat/Utils/Color+Peer.swift +++ b/bitchat/Utils/Color+Peer.swift @@ -8,6 +8,34 @@ import SwiftUI +enum PeerColorContrast { + /// Blue and purple hues (210°–306°) have low perceived luminance at standard HSB + /// brightness on dark/black backgrounds. Lifting brightness and easing saturation + /// keeps every peer color readable (>= 4.5:1 WCAG AA on black) in dark mode while + /// preserving ring-dependent separation for overflow palette entries. + static func adjustForDarkMode( + hue: Double, + saturation: Double, + brightness: Double, + ring: Int = 0 + ) -> (saturation: Double, brightness: Double) { + let normalizedHue = hue - floor(hue) + guard normalizedHue >= 0.58 && normalizedHue <= 0.85 else { + return (saturation, brightness) + } + // Smooth bell curve peaking around deep blue/purple (~0.70). + let center = 0.70 + let halfWidth = 0.15 + let weight = max(0.0, 1.0 - abs(normalizedHue - center) / halfWidth) + let ringStep = Double(max(0, ring)) + let maxSat = max(0.25, 0.50 + (1.0 - weight) * 0.10 - ringStep * 0.12) + let minBri = min(1.0, 0.92 + weight * 0.06) + let adjustedSaturation = min(saturation, maxSat) + let adjustedBrightness = max(brightness, minBri) + return (min(1.0, max(0.0, adjustedSaturation)), min(1.0, max(0.0, adjustedBrightness))) + } +} + extension Color { private static let maxPeerColorCacheEntries = 512 @@ -67,8 +95,13 @@ extension Color { let sRange: Double = 0.20 let bBase: Double = isDark ? 0.75 : 0.45 let bRange: Double = isDark ? 0.16 : 0.14 - let saturation = min(1.0, max(0.50, sBase + (sRand - 0.5) * sRange)) - let brightness = min(1.0, max(0.35, bBase + (bRand - 0.5) * bRange)) + var saturation = min(1.0, max(0.50, sBase + (sRand - 0.5) * sRange)) + var brightness = min(1.0, max(0.35, bBase + (bRand - 0.5) * bRange)) + if isDark { + let adjusted = PeerColorContrast.adjustForDarkMode(hue: hue, saturation: saturation, brightness: brightness) + saturation = adjusted.saturation + brightness = adjusted.brightness + } let c = Color(hue: hue, saturation: saturation, brightness: brightness) Self.peerColorCache.store(c, for: cacheKey, expectedGeneration: lookup.generation) self = c diff --git a/bitchat/ViewModels/MinimalDistancePalette.swift b/bitchat/ViewModels/MinimalDistancePalette.swift index 4c67316e..6de08459 100644 --- a/bitchat/ViewModels/MinimalDistancePalette.swift +++ b/bitchat/ViewModels/MinimalDistancePalette.swift @@ -49,10 +49,20 @@ final class MinimalDistancePalette { @MainActor func color(for identifier: String, isDark: Bool) -> Color? { guard let entry = entries[identifier] else { return nil } - let saturation = isDark ? config.saturationDark : config.saturationLight + var saturation = isDark ? config.saturationDark : config.saturationLight let baseBrightness = isDark ? config.baseBrightnessDark : config.baseBrightnessLight let ringDelta = isDark ? config.ringBrightnessDeltaDark : config.ringBrightnessDeltaLight - let brightness = min(1.0, max(0.0, baseBrightness + ringDelta * Double(entry.ring))) + var brightness = min(1.0, max(0.0, baseBrightness + ringDelta * Double(entry.ring))) + if isDark { + let adjusted = PeerColorContrast.adjustForDarkMode( + hue: entry.hue, + saturation: saturation, + brightness: brightness, + ring: entry.ring + ) + saturation = adjusted.saturation + brightness = adjusted.brightness + } return Color(hue: entry.hue, saturation: saturation, brightness: brightness) } diff --git a/bitchatTests/PeerColorContrastTests.swift b/bitchatTests/PeerColorContrastTests.swift new file mode 100644 index 00000000..ad8e53a3 --- /dev/null +++ b/bitchatTests/PeerColorContrastTests.swift @@ -0,0 +1,43 @@ +import Foundation +import Testing +@testable import bitchat + +struct PeerColorContrastTests { + /// Converts HSB (0...1) to sRGB (0...1) and computes WCAG 2.1 contrast ratio against pure black. + private func contrastAgainstBlack(hue: Double, saturation: Double, brightness: Double) -> Double { + let h = (hue - floor(hue)) * 6.0 + let i = Int(floor(h)) % 6 + let f = h - floor(h) + let p = brightness * (1.0 - saturation) + let q = brightness * (1.0 - f * saturation) + let t = brightness * (1.0 - (1.0 - f) * saturation) + let (r, g, b): (Double, Double, Double) = switch i { + case 0: (brightness, t, p) + case 1: (q, brightness, p) + case 2: (p, brightness, t) + case 3: (p, q, brightness) + case 4: (t, p, brightness) + default: (brightness, p, q) + } + func linearize(_ c: Double) -> Double { + c <= 0.03928 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4) + } + let luminance = 0.2126 * linearize(r) + 0.7152 * linearize(g) + 0.0722 * linearize(b) + return (luminance + 0.05) / 0.05 + } + + @Test func darkModePurpleAndBlueHuesMeetWCAGAAAgainstBlack() { + for deg in stride(from: 210, through: 306, by: 6) { + let hue = Double(deg) / 360.0 + let adjusted = PeerColorContrast.adjustForDarkMode(hue: hue, saturation: 0.85, brightness: 0.75) + let ratio = contrastAgainstBlack(hue: hue, saturation: adjusted.saturation, brightness: adjusted.brightness) + #expect(ratio >= 4.5, "Hue \(deg)° contrast \(ratio) should be >= 4.5 on black") + } + } + + @Test func nonBluePurpleHuesRemainUnchanged() { + let adjusted = PeerColorContrast.adjustForDarkMode(hue: 0.33, saturation: 0.80, brightness: 0.75) + #expect(adjusted.saturation == 0.80) + #expect(adjusted.brightness == 0.75) + } +}