In a current Swift App, I need to replace ALL my uses of NSColor with a method that allows me to use “Named Colors” specified as strings.
It seems the definition of an NSColor is one of 4 methods
- specify the RGB value (such as NSColor(rgb:0xF3297A) )
- specify one of the sematic colors (such as NSColor(named:“TextColor”)
- specify one of the “standard colors” (such as NSColor.red)
- specify one of the “system colors” (such as NSColor.systemblue)
The first two methods I can incorporate into a NSColor Extension quite easily.
I check if the “name” starts with &C (for example) and extract the hex value and use the RGB initializer. Otherwise I check the name on a list and use the “Named” initializer
HOWEVER. The last two won’t work that way, since they don’t exist in the “namelist” like those in the second bullet do. So NSColor(named:“red”) is not a valid syntax. Now for these I could use the RGB initializer since they do not change values between light and dark. but that still leaves the last bullet.
Those colors that are “systemXXXX” DO have different values between light and dark
So I can’t figure out a way to implement those
The idea was to add an extention to NSColor
so I could say
let myColor = NSColor(nameof : "systemBlue")
Note : I know I can define my own versions in the Asset Catalog so I could use NAMED , but that is a duplication of what Apple already has done… but if there is no other way