Probably an obvious solution at this point, but, in case the color is not being resolved in context of a View
, you could use "empty" EnvironmentValues
like so:
let resolvedColor = color.resolve(in: EnvironmentValues())
I'm not sure what sort of input could be taken from the Environment, perhaps color space or some adjustments related to color scheme.
I should add, however, that, whenever I tried using Color.Resolved
with SwiftData, so far, it invariably ended up with a crash with this description:
AppName crashed due to fatalError in ModelCoders.swift at line 124.
Composite Coder only supports Keyed Container
This leads me to assume that SwiftData can't, currently, deal with the way Color.Resolved
encodes itself. In other words, you can use the convenience of Color.Resolved
to resolve colors (sounds like a totology :)) but not to store it in a SwiftData model. Here's a helper struct I ended up using with my @Model
:
struct ColorComponents: Codable {
let red: Float
let green: Float
let blue: Float
var color: Color {
Color(red: Double(red), green: Double(green), blue: Double(blue))
}
static func fromColor(_ color: Color) -> ColorComponents {
let resolved = color.resolve(in: EnvironmentValues())
return ColorComponents(
red: resolved.red,
green: resolved.green,
blue: resolved.blue
)
}
}