Skip to content

Instantly share code, notes, and snippets.

@niw
Created June 20, 2014 23:19
Show Gist options
  • Save niw/9de0027b917b0cdf822c to your computer and use it in GitHub Desktop.
Save niw/9de0027b917b0cdf822c to your computer and use it in GitHub Desktop.
Create color list file (.clr) used for Apple color pallet, stored in ~/Library/Colors
#import <AppKit/AppKit.h>
int main(int argc, char *argv[])
{
@autoreleasepool {
NSColorList *colorList = [[NSColorList alloc] initWithName:@"Colors"];
if (argc < 2) {
printf("Usage: %s name:rrggbb,name:rrggbb,...\n", argv[0]);
return 0;
}
NSString *colors = [NSString stringWithUTF8String:argv[1]];
for (NSString *colorString in [colors componentsSeparatedByString:@","]) {
NSString *key;
NSArray *keyAndColor = [colorString componentsSeparatedByString:@":"];
if ([keyAndColor count] > 1) {
key = keyAndColor[0];
colorString = keyAndColor[1];
} else {
key = [NSString stringWithFormat:@"#%@", [colorString lowercaseString]];
}
NSScanner *scanner = [NSScanner scannerWithString:colorString];
unsigned int colorInt;
[scanner scanHexInt:&colorInt];
CGFloat r = (CGFloat)((colorInt >> 16) & 0xff) / 0xff;
CGFloat g = (CGFloat)((colorInt >> 8) & 0xff) /0xff;
CGFloat b = (CGFloat)((colorInt) & 0xff) / 0xff;
[colorList setColor:[NSColor colorWithRed:r green:g blue:b alpha:1.0f] forKey:key];
}
[colorList writeToFile:@"Colors.clr"];
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment