Figured it out. I used a modified version of this NSImage+TintColor extension to create a swatch dictionary:
let namedColors:[String] = [
"aquamarine",
"black",
"blue",
...
}
let myColorSwatches:[String:NSImage] = { (colorNames:[String]) -> [String:NSImage] in
var toReturn:[String:NSImage] = [:]
for colorName in colorNames {
let image = NSImage(systemSymbolName: "rectangle.fill", accessibilityDescription: nil)!
image.isTemplate = false
image.lockFocus()
NSColor(named: colorName)!.set()
let imageRect = NSRect(origin: .zero, size: image.size)
imageRect.fill(using: .sourceIn)
image.unlockFocus()
toReturn[colorName] = image
}
return toReturn
}(namedColors)
... which I can then reference in my SwiftUI Picker:
Picker("Color:", selection: pickerColor) {
ForEach(namedColors, id: \.self) { colorName in
HStack {
Image(nsImage: myColorSwatches[colorName]!)
Text(colorName)
}
}
}
And just as a reminder in case anybody else wants to do this, I have a color set defined in my Assets.xcassets for each named color.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: