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
This commit is contained in:
sh95014 2024-02-13 02:33:59 -08:00 committed by GitHub
parent 9ddf0e65fc
commit 29312d1be2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 1 deletions

View file

@ -167,12 +167,14 @@ import SwiftUI
public var tintColor: Color { public var tintColor: Color {
didSet { didSet {
themeStorage.tintColor = tintColor themeStorage.tintColor = tintColor
computeContrastingTintColor()
} }
} }
public var primaryBackgroundColor: Color { public var primaryBackgroundColor: Color {
didSet { didSet {
themeStorage.primaryBackgroundColor = primaryBackgroundColor themeStorage.primaryBackgroundColor = primaryBackgroundColor
computeContrastingTintColor()
} }
} }
@ -185,6 +187,33 @@ import SwiftUI
public var labelColor: Color { public var labelColor: Color {
didSet { didSet {
themeStorage.labelColor = labelColor 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 primaryBackgroundColor = themeStorage.primaryBackgroundColor
secondaryBackgroundColor = themeStorage.secondaryBackgroundColor secondaryBackgroundColor = themeStorage.secondaryBackgroundColor
labelColor = themeStorage.labelColor labelColor = themeStorage.labelColor
contrastingTintColor = .red // real work done in computeContrastingTintColor()
avatarPosition = themeStorage.avatarPosition avatarPosition = themeStorage.avatarPosition
avatarShape = themeStorage.avatarShape avatarShape = themeStorage.avatarShape
storedSet = themeStorage.storedSet storedSet = themeStorage.storedSet
@ -293,6 +323,8 @@ import SwiftUI
chosenFontData = themeStorage.chosenFontData chosenFontData = themeStorage.chosenFontData
statusActionSecondary = themeStorage.statusActionSecondary statusActionSecondary = themeStorage.statusActionSecondary
selectedSet = storedSet selectedSet = storedSet
computeContrastingTintColor()
} }
public static var allColorSet: [ColorSet] { public static var allColorSet: [ColorSet] {

View file

@ -212,7 +212,7 @@ struct BlurOverLay: View {
.matchedGeometryEffect(id: "eye", in: buttonSpace) .matchedGeometryEffect(id: "eye", in: buttonSpace)
} }
.lineLimit(1) .lineLimit(1)
.foregroundColor(theme.labelColor) .foregroundColor(theme.contrastingTintColor)
} else { } else {
Image(systemName: "eye.slash") Image(systemName: "eye.slash")
.transition(.opacity) .transition(.opacity)