| 1 | #import "TCLANSIColors.h" |
|---|
| 2 | |
|---|
| 3 | @implementation TCLANSIColors |
|---|
| 4 | +(TCLANSIColors *)defaultANSIColors |
|---|
| 5 | { |
|---|
| 6 | TCLANSIColors *colors = [[TCLANSIColors alloc] init]; |
|---|
| 7 | return [colors autorelease]; |
|---|
| 8 | } |
|---|
| 9 | |
|---|
| 10 | +(TCLANSIColors *)fromDictionary:(NSDictionary *)aDictionary |
|---|
| 11 | { |
|---|
| 12 | TCLANSIColors *colors = [TCLANSIColors defaultANSIColors]; |
|---|
| 13 | for(NSString *name in [TCLANSIColors ansiColorNames]) { |
|---|
| 14 | [colors setValue:[aDictionary objectForKey:name] forKey:[name stringByAppendingString:@"Color"]]; |
|---|
| 15 | } |
|---|
| 16 | return colors; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | +(NSArray *)ansiColorNames |
|---|
| 20 | { |
|---|
| 21 | static NSArray *colorNames; |
|---|
| 22 | if(colorNames == nil) { |
|---|
| 23 | colorNames = [[NSArray arrayWithObjects:@"black", @"red", @"green", @"yellow", @"blue", @"magenta", @"cyan", @"white", nil] retain]; |
|---|
| 24 | } |
|---|
| 25 | return colorNames; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | -(void)dealloc |
|---|
| 29 | { |
|---|
| 30 | [super dealloc]; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | -(id)init |
|---|
| 34 | { |
|---|
| 35 | self = [super init]; |
|---|
| 36 | for(NSString *name in [TCLANSIColors ansiColorNames]) { |
|---|
| 37 | NSString *key = [name stringByAppendingString:@"Color"]; |
|---|
| 38 | [self setValue:[NSColor valueForKey:key] forKey:key]; |
|---|
| 39 | } |
|---|
| 40 | return self; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | -(NSDictionary *)ansiColorsAsDictionary |
|---|
| 44 | { |
|---|
| 45 | NSMutableDictionary *dict = [NSMutableDictionary dictionary]; |
|---|
| 46 | for(NSString *name in [TCLANSIColors ansiColorNames]) { |
|---|
| 47 | [dict setValue:[self valueForKey:[name stringByAppendingString:@"Color"]] forKey:name]; |
|---|
| 48 | } |
|---|
| 49 | return dict; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | -(NSColor *)colorFor:(unsigned int)code |
|---|
| 53 | { |
|---|
| 54 | NSColor *color = nil; |
|---|
| 55 | if(code > 0) { |
|---|
| 56 | NSString *name = [[TCLANSIColors ansiColorNames] objectAtIndex:code-1]; |
|---|
| 57 | if(name) { |
|---|
| 58 | color = [self valueForKey:[name stringByAppendingString:@"Color"]]; |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | return color; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | @end |
|---|