├── README.md ├── clr2json ├── json2clr ├── sample └── sample.json └── test ├── result.clr ├── result.json ├── result2.json ├── test.clr ├── test.json ├── test.sh └── verify /README.md: -------------------------------------------------------------------------------- 1 | # json2clr 2 | 3 | Create color palette file (.clr) from JSON 4 | 5 | ## Usage 6 | 7 | ```shell 8 | $ json2clr -n PalleteName -i input.json -o output.clr 9 | ``` 10 | 11 | ## Input JSON format 12 | 13 | ```json 14 | [ 15 | { "name": "background black hex", "hex": "191919" }, 16 | { "name": "background black hex with A", "hex": "191919FF" }, 17 | { "name": "rgb color 255", "r": "255", "g": "0", "b": "100" }, 18 | { "name": "rgb color 1.0", "r": "1.0", "g": "0.0", "b": "0.5", "a": "0.8" } 19 | ] 20 | ``` 21 | 22 | -------------------------- 23 | 24 | # clr2json 25 | 26 | Create color JSON file from color palette (.clr) 27 | 28 | ## Usage 29 | 30 | ```shell 31 | $ clr2json -i input.clr -o output.json 32 | ``` 33 | -------------------------------------------------------------------------------- /clr2json: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env xcrun swift 2 | // vim: ft=swift 3 | 4 | import Foundation 5 | import AppKit 6 | 7 | // MARK: - usage 8 | 9 | let usage = "clr2json [-i ] [-o ]" 10 | 11 | let options = [ 12 | "-n": "palette name", 13 | "-i": "input clr file path", 14 | "-o": "output json file path", 15 | ] 16 | 17 | // MARK: - functions 18 | 19 | func printUsage() { 20 | print("Usage: \(usage)\n") 21 | print("Options:") 22 | for (option, description) in options { 23 | print("\t\(option)\t\(description)") 24 | } 25 | } 26 | 27 | // MARK: - options 28 | 29 | var inputPath: String! 30 | var outputPath: String! 31 | 32 | let arguments = CommandLine.arguments 33 | for i in 0.. NSColor? { 59 | var cString: String = value.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet()).uppercaseString 60 | 61 | if cString.hasPrefix("#") { 62 | cString = (cString as NSString).substringFromIndex(1) 63 | } 64 | 65 | if cString.characters.count != 6 { 66 | return nil 67 | } 68 | 69 | var rgbValue : UInt32 = 0 70 | NSScanner(string: cString).scanHexInt(&rgbValue) 71 | 72 | return NSColor( 73 | red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, 74 | green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0, 75 | blue: CGFloat(rgbValue & 0x0000FF) / 255.0, 76 | alpha: CGFloat(1.0) 77 | ) 78 | } 79 | 80 | class func colorFromStrings(red red: String?, green: String?, blue: String?, alpha: String?) -> NSColor? { 81 | let r = Color.floatValueFromString(red) ?? 0.0 82 | let g = Color.floatValueFromString(green) ?? 0.0 83 | let b = Color.floatValueFromString(blue) ?? 0.0 84 | let a = Color.floatValueFromString(alpha) ?? 1.0 85 | 86 | return NSColor(deviceRed: r, green: g, blue: b, alpha: a) 87 | } 88 | 89 | class func floatValueFromString(value: String?) -> CGFloat? { 90 | if let string = value { 91 | if let integer = Int(string) { 92 | return CGFloat(integer) / 255.0 93 | } else { 94 | return CGFloat((string as NSString).doubleValue) 95 | } 96 | } 97 | return nil 98 | } 99 | 100 | } 101 | 102 | // MARK: - options 103 | 104 | var paletteName: String! 105 | var inputPath: String! 106 | var outputPath: String! 107 | 108 | let arguments = Process.arguments 109 | for i in 0.. arguments.count { 19 | printUsage() 20 | exit(0) 21 | } 22 | 23 | let json1 = arguments[1] 24 | let json2 = arguments[2] 25 | 26 | 27 | --------------------------------------------------------------------------------