-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathCDColorListDumper.m
More file actions
67 lines (52 loc) · 2.42 KB
/
CDColorListDumper.m
File metadata and controls
67 lines (52 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// CDColorListDumper.m
// codegenutils
//
// Created by Jim Puls on 9/6/13.
// Licensed to Square, Inc. under one or more contributor license agreements.
// See the LICENSE file distributed with this work for the terms under
// which Square, Inc. licenses this file to you.
#import "CDColorListDumper.h"
@implementation CDColorListDumper
+ (NSString *)inputFileExtension;
{
return @"clr";
}
- (void)startWithCompletionHandler:(dispatch_block_t)completionBlock;
{
NSString *colorListName = [[self.inputURL lastPathComponent] stringByDeletingPathExtension];
self.className = [[NSString stringWithFormat:@"%@%@ColorList", self.classPrefix, colorListName]stringByReplacingOccurrencesOfString:@" " withString:@""];
NSColorList *colorList = [[NSColorList alloc] initWithName:colorListName fromFile:self.inputURL.path];
// Install this color list
[colorList writeToFile:nil];
self.interfaceContents = [NSMutableArray array];
self.implementationContents = [NSMutableArray array];
for (NSString *key in colorList.allKeys) {
NSColor *color = [colorList colorWithKey:key];
if ([color.colorSpaceName isEqualToString:NSCalibratedRGBColorSpace] || [color.colorSpaceName isEqualToString:NSDeviceRGBColorSpace]) {
CGFloat r, g, b, a;
[color getRed:&r green:&g blue:&b alpha:&a];
NSString *declaration = [NSString stringWithFormat:@"+ (UIColor *)%@Color;\n", [self methodNameForKey:key]];
[self.interfaceContents addObject:declaration];
NSMutableString *method = [declaration mutableCopy];
[method appendFormat:@"{\n return [UIColor colorWithRed:%.3ff green:%.3ff blue:%.3ff alpha:%.3ff];\n}\n", r, g, b, a];
[self.implementationContents addObject:method];
}
else if ([color.colorSpaceName isEqualToString:NSCalibratedWhiteColorSpace]) {
CGFloat w, a;
[color getWhite:&w alpha:&a];
NSString *declaration = [NSString stringWithFormat:@"+ (UIColor *)%@Color;\n", [self methodNameForKey:key]];
[self.interfaceContents addObject:declaration];
NSMutableString *method = [declaration mutableCopy];
[method appendFormat:@"{\n return [UIColor colorWithWhite:%.3ff alpha:%.3ff];\n}\n", w, a];
[self.implementationContents addObject:method];
}
else {
printf("Color %s isn't device RGB. Skipping.", [key UTF8String]);
continue;
}
}
[self writeOutputFiles];
completionBlock();
}
@end