The following code successfully detects if the color is black, but it doesn't detect when the color is white or any other color.
Any idea why the following code only works to detect black but not white?
func bgColor(bgColor: Color?)->Color{
var col:Color = .systemBackground
if bgColor == Color(UIColor(red: 0, green: 0, blue: 0, alpha: 1)){
// Works fine, detects black
col = .red
} else if bgColor == Color(UIColor(red: 1, green: 1, blue: 1, alpha: 1)){
// Does NOT work, doesn't detect white
col = .blue
}
return col
}
EDIT:
I have a ViewModifier
that I'm using on a symbol-avatar-image, in this avatar image the user can select a color from a Color Picker, my issue is that I have the .systemBackground
as the default background color, and if the user selects white or black for their avatar, black is not visible in dark mode and white is not visible in light mode so, what I would like to do is detect if the user selected white, I need to return a dark background color for light mode and if the user selects black I would like to return a light background for when in dark mode.
struct AvatarImageModifier : ViewModifier {
@Environment(\.colorScheme) var colorScheme
var avatarColor:Color?
func body(content: Content) -> some View { content
.background(bgColor(bgColor: avatarColor))
}
func bgColor(bgColor: Color?)->Color{
var col:Color = .systemBackground
if bgColor == Color(UIColor(red: 0, green: 0, blue: 0, alpha: 1)){
if colorScheme == .dark{
col = .systemGray3
}else{
col = .systemBackground
}
} else if bgColor == Color(UIColor(red: 1, green: 1, blue: 1, alpha: 1)){
if colorScheme == .light{
col = .systemGray3
}
}
return col
}
}