Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Saving NSColor to prefs... any example code?

Saving NSColor to prefs... any example code?
Thread Tools
stupidFish23
Forum Regular
Join Date: Jan 2002
Location: Hungary
Status: Offline
Reply With Quote
May 15, 2002, 07:25 AM
 
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.
nothing is when everything is alright
[email protected] ICQ: 153647416
     
lindberg
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
May 15, 2002, 12:34 PM
 
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 ]
     
Rickster
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
May 15, 2002, 03:40 PM
 
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.
Rick Roe
icons.cx | weblog
     
Wixar
Junior Member
Join Date: Mar 2001
Status: Offline
Reply With Quote
May 16, 2002, 02:19 AM
 
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
     
stupidFish23  (op)
Forum Regular
Join Date: Jan 2002
Location: Hungary
Status: Offline
Reply With Quote
May 16, 2002, 02:21 AM
 
Wixar: thanks! This was exactly what I needed!!!

nothing is when everything is alright
[email protected] ICQ: 153647416
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 08:30 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,