Last active
March 31, 2021 17:53
-
-
Save yusuke024/08011b4b29b45ec0f553 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ColorListGenerator.m | |
// | |
@import AppKit; | |
NSColor *colorFromRGB(unsigned rgbValue) { | |
CGFloat r = (CGFloat)((rgbValue & 0xFF0000) >> 16); | |
CGFloat g = (CGFloat)((rgbValue & 0x00FF00) >> 8); | |
CGFloat b = (CGFloat)(rgbValue & 0x0000FF); | |
NSLog(@"%.0f %.0f %.0f", r, g, b); | |
const CGFloat components[4] = {r/255.0f, g/255.0f, b/255.0f, 1.0f}; | |
NSColor *color = [NSColor colorWithColorSpace:[NSColorSpace genericRGBColorSpace] | |
components:components | |
count:4]; | |
NSLog(@"%@", color.colorSpaceName); | |
return color; | |
} | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
NSFileHandle *stdinFileHandle = [NSFileHandle fileHandleWithStandardInput]; | |
NSData *data = [stdinFileHandle readDataToEndOfFile]; | |
NSArray *lines = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] | |
componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; | |
NSColorList *colorList = [[NSColorList alloc] initWithName:@"Color"]; | |
NSUInteger i = 0; | |
for (NSString *line in lines) { | |
NSArray *tokens = [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; | |
if (tokens.count >= 2) { | |
NSString *colorName = [tokens firstObject]; | |
NSString *colorCode = [[tokens lastObject] stringByReplacingOccurrencesOfString:@"#" withString:@"0x"]; | |
unsigned int colorHex; | |
[[NSScanner scannerWithString:colorCode] scanHexInt:&colorHex]; | |
NSLog(@"%@ %@", colorName, colorCode); | |
NSColor *color = [colorFromRGB(colorHex) colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]]; | |
[colorList insertColor:color key:colorName atIndex:i++]; | |
} | |
} | |
[colorList writeToFile:@"Color.clr"]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment