Created
May 6, 2024 03:00
-
-
Save theoknock/586c641859d7ff426d4f3b45a6420743 to your computer and use it in GitHub Desktop.
Simulates a color at opacity < 1.0 when placed over a black or white background
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func hueAccentColor(angle: CGFloat) -> Color { | |
return Color(hue: CGFloat(angle / 360.0), saturation: 1.0, brightness: 1.0, opacity: 1.0) | |
} | |
func whiteColor() -> Color { | |
return Color.init(uiColor: .white) // return Color(hue: CGFloat(hueAngle / 360.0), saturation: 0.0, brightness: 1.0, opacity: 1.0) | |
} | |
func blackColor() -> Color { | |
return Color.init(uiColor: .black) // return Color(hue: CGFloat(hueAngle / 360.0), saturation: 0.0, brightness: 0.0, opacity: 1.0) | |
} | |
enum UserInterfaceStyleMode: Int { | |
case dark = 0 | |
case light = 1 | |
} | |
enum UserInterfaceStyleSource { | |
case system | |
case user(UserInterfaceStyleMode) | |
} | |
func styleMode(source: UserInterfaceStyleSource) -> UserInterfaceStyleMode { | |
var mode: Int { | |
switch source { | |
case .system: | |
return UITraitCollection.current.userInterfaceStyle.rawValue | |
case .user(let mode): | |
return mode.rawValue | |
} | |
} | |
return UserInterfaceStyleMode(rawValue: mode) ?? .light | |
} | |
var userInterfaceStyleSource: UserInterfaceStyleSource.Type = UserInterfaceStyleSource.self | |
var userInterfaceStyleMode: UserInterfaceStyleMode.Type = UserInterfaceStyleMode.self | |
func opaqueHueColor(angle: CGFloat, opacity: CGFloat?, userInterfaceStyleSource: UserInterfaceStyleSource) -> some View { | |
return ((styleMode(source: userInterfaceStyleSource) != UserInterfaceStyleMode.light) | |
? whiteColor().opacity(opacity ?? 0.5) : blackColor().opacity(opacity ?? 0.5)) | |
.background { | |
hueAccentColor(angle: angle) | |
} | |
.overlay { | |
((styleMode(source: userInterfaceStyleSource) == UserInterfaceStyleMode.light) ? whiteColor().opacity(opacity ?? 0.5) : blackColor().opacity(opacity ?? 0.5)) | |
} | |
} | |
/* Usage | |
colorManager.opaqueHueColor(angle: 216.0, opacity: 0.5, userInterfaceStyleSource: ColorManager.UserInterfaceStyleSource.system) | |
colorManager.opaqueHueColor(angle: 216.0, opacity: 0.5, userInterfaceStyleSource: ColorManager.UserInterfaceStyleSource.user(ColorManager.UserInterfaceStyleMode.dark)) | |
colorManager.opaqueHueColor(angle: 216.0, opacity: 0.5, userInterfaceStyleSource: ColorManager.UserInterfaceStyleSource.user(ColorManager.UserInterfaceStyleMode.light)) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment