Re: NSColor to color name/NSString:
Re: NSColor to color name/NSString:
- Subject: Re: NSColor to color name/NSString:
- From: Bill Monk <email@hidden>
- Date: Mon, 11 Jul 2005 12:05:21 -0500
Trevor,
The Hillegass Cocoa book p. shows a method for mapping RGB
components back to named colors which you can probably adapt to you
needs. As colors are chosen in a picker, it looks up the name (if
any) and inserts it into a text field, styled in the chosen color.
Sample code (sans explanations) is here http://www.bignerdranch.com/
products/Solutions.tgz
It works perfectly for colors selected from pickers with a finite
number of colors, such as Crayons. Less perfectly for pickers with a
continuous gamut like Spectrum, where obviously there are many more
colors than names; it tests whether a color is "close enough" (using
a factor you specify) to a named color before using a name. However,
since you specifically mentioned the Crayons picker, this may be all
you need
On Jul 8, 2005, at 5:24 PM, Ricky Sharp wrote:
>>I don't think you can access those names.
Yes, you can. Here's some code to inspect all named colors in the
system's NSColorLists
//
// get available color lists and inspect the names and values of
the colors in each list
//
NSArray *allColorLists = [NSColorList availableColorLists];
NSColorList *thisColorList;
NSString *thisColorListName;
NSEnumerator *colorListEnumerator = [allColorLists
objectEnumerator];
while (thisColorList = [colorListEnumerator nextObject]) {
thisColorListName = [thisColorList name];
NSArray *colorNames = [thisColorList allKeys];
NSString *thisColorName;
NSColor *thisColor;
NSEnumerator *colorNameEnumerator = [colorNames
objectEnumerator];
while (thisColorName = [colorNameEnumerator nextObject]) {
thisColor = [thisColorList colorWithKey:thisColorName];
}
}
// grab a named color from a named color list
thisColorList = [NSColorList colorListNamed:@"Crayons"];
NSColor *someColor = [thisColorList colorWithKey:@"Spindrift"];
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden