|
|
Saving NSColor to prefs... any example code?
|
|
|
|
Forum Regular
Join Date: Jan 2002
Location: Hungary
Status:
Offline
|
|
Hi!
Does anyone have some example code how to save an NSColor to the user prefs and reload it after, say, startup of the app?
Thanks.
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
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 ]
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
The open-source OmniAppKit framework provides extensions to NSColor and NSUserDefaults which provide mostly-fail-safe ways to store colors in user preferences. (The caveats? It doesn't handle pattern colors or new types of colors defined at runtime, at least not without help.) Also, these extensions don't have much in the way of OmniFrameworks dependencies, so you can rip 'em right out and use 'em in your own app without linking the frameworks if that's more convenient for you.
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Mar 2001
Status:
Offline
|
|
A simple (if cheesy) way to store virtually ANY data type in NSUserDefaults is to convert it to NSData using NSCoding.
To save an NSColor:
NSData* data=[NSArchiver archivedDataWithRootObject:theColor];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"TheColor"];
To restore it:
NSData* data=[[NSUserDefaults standardUserDefaults] objectForKey:@"TheColor"];
NSColor* theColor = [NSUnarchiver unarchiveObjectWithData:data];
Hope this helps.
-Peter
|
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Jan 2002
Location: Hungary
Status:
Offline
|
|
Wixar: thanks! This was exactly what I needed!!!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|