Add ANSI color contrast adjustment for log view

Automatically adjust log text colors when contrast against background
is below WCAG AA standard (4.5:1). Light mode darkens colors, dark mode
lightens them. Supports iOS, macOS, and tvOS.
This commit is contained in:
世界
2026-01-08 22:12:45 +08:00
parent 1707a3412a
commit b0f1eed827
3 changed files with 193 additions and 12 deletions
+18 -2
View File
@@ -221,6 +221,7 @@ private struct LogContentInnerView: View {
@EnvironmentObject private var environments: ExtensionEnvironments
@ObservedObject var dataModel: LogDataModel
@ObservedObject var viewModel: LogViewModel
@Environment(\.colorScheme) private var colorScheme
private let logFont = Font.system(.caption2, design: .monospaced)
var body: some View {
@@ -248,7 +249,7 @@ private struct LogContentInnerView: View {
return ScrollView {
LazyVStack(alignment: .leading, spacing: 8) {
ForEach(logList.indices, id: \.self) { index in
Text(ANSIColors.parseAnsiString(logList[index]))
Text(contrastAdjustedText(for: logList[index]))
.font(logFont)
.focusable()
}
@@ -321,8 +322,23 @@ private struct LogContentInnerView: View {
}
#if os(tvOS)
private func highlightedText(for message: String) -> AttributedString {
private func contrastAdjustedText(for message: String) -> AttributedString {
var attributedString = ANSIColors.parseAnsiString(message)
let backgroundColor: UIColor = colorScheme == .dark ? .black : .white
for run in attributedString.runs {
if let fgColor = run.foregroundColor {
let uiColor = UIColor(fgColor)
let adjusted = uiColor.adjustedForContrast(against: backgroundColor)
attributedString[run.range].foregroundColor = Color(adjusted)
}
}
return attributedString
}
private func highlightedText(for message: String) -> AttributedString {
var attributedString = contrastAdjustedText(for: message)
if !viewModel.searchText.isEmpty {
let searchText = viewModel.searchText