Depends on how seriously you want to keep all NSColor attributes, and
if you want the user to be able to edit the value in preferences.
This is one simplistic way (done as C functions), which is editable:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
void SetColorForKey(NSColor *aColor, NSString *name)
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
float r,g,b,a;
[[aColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&r green
:&g blue:&b alpha:&a];
[defaults setObject:[NSString stringWithFormat:<font color = orange>@"%f:%f:%f:%f"</font>, r,g,b,a] forKey:n
ame];
}
NSColor *GetColorForKey(NSString *name)
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *value = [defaults stringForKey:name];
float r, g, b, a = <font color = blue>1.0</font>;
if (!value) return nil;
sscanf([value cString], <font color = red>"%f:%f:%f:%f"</font>, &r, &g, &b, &a);
return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
}
</font>[/code]
That stores the RGBA values, and is editable by the user. However, not every color can be converted into RGB(A), and even some that can might lose a little bit of information in the process.
If you want to absolutely store the color without fail or loss, then store the NSData returned by [NSArchiver archivedDataWithRootObject:theColorObject], and use the corresponding NSUnarchiver method to get the NSColor object back from the data. If the color is important to your app, this is the approach you have to take. It's not editable by the user externally though (not even close).
[ 05-15-2002: Message edited by: lindberg ]