Skip to content

Commit

Permalink
Update Layer+Extensions.swift
Browse files Browse the repository at this point in the history
Added the optional foreground color to the constrastTextColor extension
  • Loading branch information
huntertdiamond authored Oct 6, 2023
1 parent 0044c09 commit 6de91f2
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Layers/Source/Layer+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,24 @@ struct ContrastTextColor: ViewModifier {
var background: Color
var light: Color = .white
var dark: Color = .black
var foregroundColor: Color? = nil

func body(content: Content) -> some View {
content
.foregroundColor(background.isDark ? light : dark)
if let fgColor = foregroundColor {
return content.foregroundColor(fgColor)
} else {
return content.foregroundColor(background.isDark ? light : dark)
}
}
}

extension View {
func contrastTextColor(background: Color, light: Color, dark: Color) -> some View {
modifier(ContrastTextColor(background: background, light: light, dark: dark))
func contrastTextColor(background: Color, light: Color = .white, dark: Color = .black, foregroundColor: Color? = nil) -> some View {
modifier(ContrastTextColor(background: background, light: light, dark: dark, foregroundColor: foregroundColor))
}
}


extension Color {
private enum Luminance {
static let red: CGFloat = 0.2126
Expand Down

0 comments on commit 6de91f2

Please sign in to comment.