From 6de91f26d23ef100e4c01bfc1b6357c10dc67378 Mon Sep 17 00:00:00 2001 From: Hunter Diamond <106129005+huntertdiamond@users.noreply.github.com> Date: Fri, 6 Oct 2023 14:32:45 -0400 Subject: [PATCH] Update Layer+Extensions.swift Added the optional foreground color to the constrastTextColor extension --- Layers/Source/Layer+Extensions.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Layers/Source/Layer+Extensions.swift b/Layers/Source/Layer+Extensions.swift index d9bfcc1..d850cd8 100644 --- a/Layers/Source/Layer+Extensions.swift +++ b/Layers/Source/Layer+Extensions.swift @@ -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