From 29312d1be2dfd2e95fc02718f6f59070e0931cfa Mon Sep 17 00:00:00 2001 From: sh95014 <95387068+sh95014@users.noreply.github.com> Date: Tue, 13 Feb 2024 02:33:59 -0800 Subject: [PATCH] select a contrasting color for label of "show sensitive content" button (#1965) * Custom layout for App Store links * select a contrasting color for label of "show sensitive content" button fix https://github.com/Dimillian/IceCubesApp/issues/1932 * move contrasting color to Theme and cache computed var --- .../Sources/DesignSystem/Theme.swift | 32 +++++++++++++++++++ .../Subviews/StatusRowMediaPreviewView.swift | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Packages/DesignSystem/Sources/DesignSystem/Theme.swift b/Packages/DesignSystem/Sources/DesignSystem/Theme.swift index 6961544a..5c7efbf8 100644 --- a/Packages/DesignSystem/Sources/DesignSystem/Theme.swift +++ b/Packages/DesignSystem/Sources/DesignSystem/Theme.swift @@ -167,12 +167,14 @@ import SwiftUI public var tintColor: Color { didSet { themeStorage.tintColor = tintColor + computeContrastingTintColor() } } public var primaryBackgroundColor: Color { didSet { themeStorage.primaryBackgroundColor = primaryBackgroundColor + computeContrastingTintColor() } } @@ -185,6 +187,33 @@ import SwiftUI public var labelColor: Color { didSet { themeStorage.labelColor = labelColor + computeContrastingTintColor() + } + } + + public private(set) var contrastingTintColor: Color + + // set contrastingTintColor to either labelColor or primaryBackgroundColor, whichever contrasts + // better against the tintColor + private func computeContrastingTintColor() { + @Environment(\.self) var environment + + func luminance(_ color: Color.Resolved) -> Float { + return 0.299 * color.red + 0.587 * color.green + 0.114 * color.blue; + } + + let resolvedTintColor = tintColor.resolve(in: environment) + let resolvedLabelColor = labelColor.resolve(in: environment) + let resolvedPrimaryBackgroundColor = primaryBackgroundColor.resolve(in: environment) + + let tintLuminance = luminance(resolvedTintColor) + let labelLuminance = luminance(resolvedLabelColor) + let primaryBackgroundLuminance = luminance(resolvedPrimaryBackgroundColor) + + if abs(tintLuminance - labelLuminance) > abs(tintLuminance - primaryBackgroundLuminance) { + contrastingTintColor = labelColor + } else { + contrastingTintColor = primaryBackgroundColor } } @@ -281,6 +310,7 @@ import SwiftUI primaryBackgroundColor = themeStorage.primaryBackgroundColor secondaryBackgroundColor = themeStorage.secondaryBackgroundColor labelColor = themeStorage.labelColor + contrastingTintColor = .red // real work done in computeContrastingTintColor() avatarPosition = themeStorage.avatarPosition avatarShape = themeStorage.avatarShape storedSet = themeStorage.storedSet @@ -293,6 +323,8 @@ import SwiftUI chosenFontData = themeStorage.chosenFontData statusActionSecondary = themeStorage.statusActionSecondary selectedSet = storedSet + + computeContrastingTintColor() } public static var allColorSet: [ColorSet] { diff --git a/Packages/StatusKit/Sources/StatusKit/Row/Subviews/StatusRowMediaPreviewView.swift b/Packages/StatusKit/Sources/StatusKit/Row/Subviews/StatusRowMediaPreviewView.swift index 18efbf56..4930402f 100644 --- a/Packages/StatusKit/Sources/StatusKit/Row/Subviews/StatusRowMediaPreviewView.swift +++ b/Packages/StatusKit/Sources/StatusKit/Row/Subviews/StatusRowMediaPreviewView.swift @@ -212,7 +212,7 @@ struct BlurOverLay: View { .matchedGeometryEffect(id: "eye", in: buttonSpace) } .lineLimit(1) - .foregroundColor(theme.labelColor) + .foregroundColor(theme.contrastingTintColor) } else { Image(systemName: "eye.slash") .transition(.opacity)