Skip to content
This repository was archived by the owner on Sep 6, 2018. It is now read-only.

Commit fb9dd25

Browse files
committed
Add support for multiple color palettes
1 parent b3444ac commit fb9dd25

12 files changed

+68
-47
lines changed

Sources/Parsers/ColorsFileParser.swift

+9-6
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,21 @@ public enum ColorsParserError: Error, CustomStringConvertible {
2929
}
3030
}
3131

32+
struct Palette {
33+
let name: String
34+
let colors: [String: UInt32]
35+
}
36+
3237
protocol ColorsFileTypeParser: class {
3338
static var extensions: [String] { get }
3439

3540
init()
36-
func parseFile(at path: Path) throws -> [String: UInt32]
41+
func parseFile(at path: Path) throws -> Palette
3742
}
3843

3944
public final class ColorsFileParser {
4045
private var parsers = [String: ColorsFileTypeParser.Type]()
41-
var colors = [String: UInt32]()
46+
var palettes = [Palette]()
4247

4348
public init() throws {
4449
try register(parser: ColorsCLRFileParser.self)
@@ -53,11 +58,9 @@ public final class ColorsFileParser {
5358
}
5459

5560
let parser = parserType.init()
56-
let colors = try parser.parseFile(at: path)
61+
let palette = try parser.parseFile(at: path)
5762

58-
for (name, value) in colors {
59-
self.colors[name] = value
60-
}
63+
palettes += [palette]
6164
}
6265

6366
func register(parser: ColorsFileTypeParser.Type) throws {

Sources/Parsers/ColorsFileParsers/ColorsCLRFileParser.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ final class ColorsCLRFileParser: ColorsFileTypeParser {
1414
static let userColors = "UserColors"
1515
}
1616

17-
func parseFile(at path: Path) throws -> [String: UInt32] {
17+
func parseFile(at path: Path) throws -> Palette {
1818
if let colorsList = NSColorList(name: Keys.userColors, fromFile: path.string) {
1919
var colors = [String: UInt32]()
2020

2121
for colorName in colorsList.allKeys {
2222
colors[colorName] = colorsList.color(withKey: colorName)?.hexValue
2323
}
2424

25-
return colors
25+
let name = path.lastComponentWithoutExtension
26+
return Palette(name: name, colors: colors)
2627
} else {
2728
throw ColorsParserError.invalidFile(path: path, reason: "Invalid color list")
2829
}

Sources/Parsers/ColorsFileParsers/ColorsJSONFileParser.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import PathKit
1010
final class ColorsJSONFileParser: ColorsFileTypeParser {
1111
static let extensions = ["json"]
1212

13-
func parseFile(at path: Path) throws -> [String: UInt32] {
13+
func parseFile(at path: Path) throws -> Palette {
1414
do {
1515
let json = try JSONSerialization.jsonObject(with: try path.read(), options: [])
1616
guard let dict = json as? [String: String] else {
@@ -23,7 +23,8 @@ final class ColorsJSONFileParser: ColorsFileTypeParser {
2323
colors[key] = try parse(hex: value, key: key)
2424
}
2525

26-
return colors
26+
let name = path.lastComponentWithoutExtension
27+
return Palette(name: name, colors: colors)
2728
} catch let error as ColorsParserError {
2829
throw error
2930
} catch let error {

Sources/Parsers/ColorsFileParsers/ColorsTXTFileParser.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ final class ColorsTextFileParser: ColorsFileTypeParser {
6565
// - One line per entry
6666
// - Each line composed by the color name, then ":", then the color hex representation
6767
// - Extra spaces will be skipped
68-
func parseFile(at path: Path) throws -> [String: UInt32] {
68+
func parseFile(at path: Path) throws -> Palette {
6969
do {
7070
let dict = try keyValueDict(from: path, withSeperator: ":")
7171
for key in dict.keys {
@@ -77,6 +77,7 @@ final class ColorsTextFileParser: ColorsFileTypeParser {
7777
throw ColorsParserError.invalidFile(path: path, reason: error.localizedDescription)
7878
}
7979

80-
return colors
80+
let name = path.lastComponentWithoutExtension
81+
return Palette(name: name, colors: colors)
8182
}
8283
}

Sources/Parsers/ColorsFileParsers/ColorsXMLFileParser.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class ColorsXMLFileParser: ColorsFileTypeParser {
1616
static let nameAttribute = "name"
1717
}
1818

19-
func parseFile(at path: Path) throws -> [String: UInt32] {
19+
func parseFile(at path: Path) throws -> Palette {
2020
guard let document = Kanna.XML(xml: try path.read(), encoding: .utf8) else {
2121
throw ColorsParserError.invalidFile(path: path, reason: "Unknown XML parser error.")
2222
}
@@ -34,6 +34,7 @@ final class ColorsXMLFileParser: ColorsFileTypeParser {
3434
colors[name] = try parse(hex: value, key: name)
3535
}
3636

37-
return colors
37+
let name = path.lastComponentWithoutExtension
38+
return Palette(name: name, colors: colors)
3839
}
3940
}

Sources/Stencil/ColorsContext.swift

+35-20
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,47 @@
77
import Foundation
88

99
/*
10-
- `colors`: `Array` of:
11-
- `name` : `String` — name of each color
12-
- `red` : `String` — hex value of the red component
13-
- `green`: `String` — hex value of the green component
14-
- `blue` : `String` — hex value of the blue component
15-
- `alpha`: `String` — hex value of the alpha component
10+
- `palettes`: `Array` of:
11+
- `name`: `String` — name of the palette
12+
- `colors`: `Array` of:
13+
- `name` : `String` — name of each color
14+
- `red` : `String` — hex value of the red component
15+
- `green`: `String` — hex value of the green component
16+
- `blue` : `String` — hex value of the blue component
17+
- `alpha`: `String` — hex value of the alpha component
1618
*/
1719
extension ColorsFileParser {
1820
public func stencilContext() -> [String: Any] {
19-
let colorMap = colors.map({ (color: (name: String, value: UInt32)) -> [String:String] in
20-
let name = color.name.trimmingCharacters(in: CharacterSet.whitespaces)
21-
let hex = "00000000" + String(color.value, radix: 16)
22-
let hexChars = Array(hex.characters.suffix(8))
23-
let comps = (0..<4).map { idx in String(hexChars[idx*2...idx*2+1]) }
21+
let palettes: [[String: Any]] = self.palettes
22+
.sorted(by: { $0.name < $1.name })
23+
.map { palette in
24+
let colors = palette.colors
25+
.sorted { $0.key < $1.key }
26+
.map(map(color:value:))
2427

25-
return [
26-
"name": name,
27-
"red": comps[0],
28-
"green": comps[1],
29-
"blue": comps[2],
30-
"alpha": comps[3]
31-
]
32-
}).sorted { $0["name"] ?? "" < $1["name"] ?? "" }
28+
return [
29+
"name": palette.name,
30+
"colors": colors
31+
]
32+
}
3333

3434
return [
35-
"colors": colorMap
35+
"palettes": palettes
36+
]
37+
}
38+
39+
private func map(color name: String, value: UInt32) -> [String: String] {
40+
let name = name.trimmingCharacters(in: .whitespaces)
41+
let hex = "00000000" + String(value, radix: 16)
42+
let hexChars = Array(hex.characters.suffix(8))
43+
let comps = (0..<4).map { idx in String(hexChars[idx*2...idx*2+1]) }
44+
45+
return [
46+
"name": name,
47+
"red": comps[0],
48+
"green": comps[1],
49+
"blue": comps[2],
50+
"alpha": comps[3]
3651
]
3752
}
3853
}

Tests/SwiftGenKitTests/ColorsCLRFileTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import PathKit
1111
class ColorsCLRFileTests: XCTestCase {
1212
func testFileWithDefaults() throws {
1313
let parser = try ColorsFileParser()
14-
parser.colors = try ColorsCLRFileParser().parseFile(at: Fixtures.path(for: "colors.clr", sub: .colors))
14+
parser.palettes = [try ColorsCLRFileParser().parseFile(at: Fixtures.path(for: "colors.clr", sub: .colors))]
1515

1616
let result = parser.stencilContext()
1717
XCTDiffContexts(result, expected: "defaults.plist", sub: .colors)

Tests/SwiftGenKitTests/ColorsJSONFileTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import PathKit
1111
class ColorsJSONFileTests: XCTestCase {
1212
func testFileWithDefaults() throws {
1313
let parser = try ColorsFileParser()
14-
parser.colors = try ColorsJSONFileParser().parseFile(at: Fixtures.path(for: "colors.json", sub: .colors))
14+
parser.palettes = [try ColorsJSONFileParser().parseFile(at: Fixtures.path(for: "colors.json", sub: .colors))]
1515

1616
let result = parser.stencilContext()
1717
XCTDiffContexts(result, expected: "defaults.plist", sub: .colors)

Tests/SwiftGenKitTests/ColorsTests.swift

+7-8
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ import XCTest
1010

1111
final class TestFileParser1: ColorsFileTypeParser {
1212
static let extensions = ["test1"]
13-
func parseFile(at path: Path) throws -> [String: UInt32] {
14-
return ["test1": 1]
13+
func parseFile(at path: Path) throws -> Palette {
14+
return Palette(name: "test1", colors: [:])
1515
}
1616
}
1717

1818
final class TestFileParser2: ColorsFileTypeParser {
1919
static let extensions = ["test2"]
20-
func parseFile(at path: Path) throws -> [String: UInt32] {
21-
return ["test2": 1]
20+
func parseFile(at path: Path) throws -> Palette {
21+
return Palette(name: "test2", colors: [:])
2222
}
2323
}
2424

2525
final class TestFileParser3: ColorsFileTypeParser {
2626
static let extensions = ["test1"]
27-
func parseFile(at path: Path) throws -> [String: UInt32] {
28-
return [:]
27+
func parseFile(at path: Path) throws -> Palette {
28+
return Palette(name: "test3", colors: [:])
2929
}
3030
}
3131

@@ -45,8 +45,7 @@ class ColorParserTests: XCTestCase {
4545
try parser.register(parser: TestFileParser2.self)
4646

4747
try parser.parseFile(at: "someFile.test1")
48-
XCTAssertEqual(parser.colors["test1"], 1)
49-
XCTAssertNil(parser.colors["test2"])
48+
XCTAssertEqual(parser.palettes.first?.name, "test1")
5049
}
5150

5251
func testDispatchUnknownExtension() throws {

Tests/SwiftGenKitTests/ColorsTextFileTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import PathKit
1111
class ColorsTextFileTests: XCTestCase {
1212
func testFileWithDefaults() throws {
1313
let parser = try ColorsFileParser()
14-
parser.colors = try ColorsTextFileParser().parseFile(at: Fixtures.path(for: "colors.txt", sub: .colors))
14+
parser.palettes = [try ColorsTextFileParser().parseFile(at: Fixtures.path(for: "colors.txt", sub: .colors))]
1515

1616
let result = parser.stencilContext()
1717
XCTDiffContexts(result, expected: "text-defaults.plist", sub: .colors)

Tests/SwiftGenKitTests/ColorsXMLFileTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import PathKit
1111
class ColorsXMLFileTests: XCTestCase {
1212
func testFileWithDefaults() throws {
1313
let parser = try ColorsFileParser()
14-
parser.colors = try ColorsXMLFileParser().parseFile(at: Fixtures.path(for: "colors.xml", sub: .colors))
14+
parser.palettes = [try ColorsXMLFileParser().parseFile(at: Fixtures.path(for: "colors.xml", sub: .colors))]
1515

1616
let result = parser.stencilContext()
1717
XCTDiffContexts(result, expected: "defaults.plist", sub: .colors)

0 commit comments

Comments
 (0)