|
7 | 7 | import Foundation
|
8 | 8 |
|
9 | 9 | /*
|
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 |
16 | 18 | */
|
17 | 19 | extension ColorsFileParser {
|
18 | 20 | 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:)) |
24 | 27 |
|
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 | + } |
33 | 33 |
|
34 | 34 | 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] |
36 | 51 | ]
|
37 | 52 | }
|
38 | 53 | }
|
0 commit comments