Skip to content

Instantly share code, notes, and snippets.

@chrisbrandow
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbrandow/27d4862d210d2efca55e to your computer and use it in GitHub Desktop.
Save chrisbrandow/27d4862d210d2efca55e to your computer and use it in GitHub Desktop.
command line tool to convert plist with hex color values to Os X color list
//
// main.m
// ColorListFromPList
//
// Created by Christopher Private on 7/12/15.
// Copyright (c) 2015 Flouu. All rights reserved.
//
@import AppKit;
static NSColor *colorWithHexString(NSString *hexString);
static BOOL isValidHex(NSString *hexString);
static BOOL stringIsEmpty(NSString *s);
NSArray *colorsFromDictionary(NSDictionary *dictionary, NSString *name);
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *path = [NSString stringWithUTF8String:argv[1]];
NSFileManager *fileManager = [NSFileManager defaultManager];
;
if (![fileManager fileExistsAtPath: path]) {
NSLog(@"no file at %@", path);
return 0;
}
NSMutableDictionary *plistDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSString *extension = [[fileManager displayNameAtPath:path] componentsSeparatedByString:@"."].lastObject;
if (![extension isEqualToString:@"plist"]) {
return 0;
}
NSString *pListName = [[fileManager displayNameAtPath:path] componentsSeparatedByString:@"."].firstObject;
NSArray *colors = colorsFromDictionary(plistDictionary, @"");
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"keyName" ascending:NO];
colors = [colors sortedArrayUsingDescriptors:@[descriptor]];
NSColorList *colorList = [[NSColorList alloc] initWithName:pListName];
for (NSDictionary *dict in colors) {
[colorList insertColor:dict[@"color"] key:dict[@"keyName"] atIndex:0];
}
[colorList writeToFile:fileManager.currentDirectoryPath];
}
return 0;
}
NSArray *colorsFromDictionary(NSDictionary *dictionary, NSString *name) {
NSMutableArray *array = [NSMutableArray new];
for (NSString *key in dictionary) {
if (isValidHex(dictionary[key])) {
NSString *newColorName = (name.length) ? [NSString stringWithFormat:@"%@-%@", name, key] : key;
[array addObject:@{@"keyName" : newColorName, @"color" : colorWithHexString(dictionary[key])}];
} else if ([dictionary[key] isKindOfClass:[NSDictionary class]]) {
[array addObjectsFromArray:colorsFromDictionary(dictionary[key], key)];
}
}
return [NSArray arrayWithArray:array];
}
static BOOL isValidHex(NSString *hexString) {
if (![hexString isKindOfClass:[NSString class]]) {
return NO;
}
if (stringIsEmpty(hexString)) {
return NO;
}
NSMutableString *s = [hexString mutableCopy];
[s replaceOccurrencesOfString:@"#" withString:@"" options:0 range:NSMakeRange(0, [hexString length])];
if (s.length != 6) {
return NO;
}
CFStringTrimWhitespace((__bridge CFMutableStringRef)s);
NSString *redString = [s substringToIndex:2];
NSString *greenString = [s substringWithRange:NSMakeRange(2, 2)];
NSString *blueString = [s substringWithRange:NSMakeRange(4, 2)];
unsigned int red = 0, green = 0, blue = 0;
[[NSScanner scannerWithString:redString] scanHexInt:&red];
[[NSScanner scannerWithString:greenString] scanHexInt:&green];
[[NSScanner scannerWithString:blueString] scanHexInt:&blue];
for (NSNumber *number in @[@(red), @(green), @(blue)]) {
if (number.intValue < 0 || number.intValue > 255) {
NSLog(@"bad string %@", hexString);
return NO;
}
}
return YES;
}
static NSColor *colorWithHexString(NSString *hexString) {
/*Picky. Crashes by design.*/
if (stringIsEmpty(hexString)) {
return [NSColor lightGrayColor];
}
NSMutableString *s = [hexString mutableCopy];
[s replaceOccurrencesOfString:@"#" withString:@"" options:0 range:NSMakeRange(0, [hexString length])];
CFStringTrimWhitespace((__bridge CFMutableStringRef)s);
NSString *redString = [s substringToIndex:2];
NSString *greenString = [s substringWithRange:NSMakeRange(2, 2)];
NSString *blueString = [s substringWithRange:NSMakeRange(4, 2)];
unsigned int red = 0, green = 0, blue = 0;
[[NSScanner scannerWithString:redString] scanHexInt:&red];
[[NSScanner scannerWithString:greenString] scanHexInt:&green];
[[NSScanner scannerWithString:blueString] scanHexInt:&blue];
return [NSColor colorWithRed:(CGFloat)red/255.0f green:(CGFloat)green/255.0f blue:(CGFloat)blue/255.0f alpha:1.0f];
}
static BOOL stringIsEmpty(NSString *s) {
return s == nil || [s length] == 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment