-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateIcon.swift
More file actions
110 lines (97 loc) · 3.96 KB
/
GenerateIcon.swift
File metadata and controls
110 lines (97 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import AppKit
private let outputDirectory = URL(fileURLWithPath: CommandLine.arguments[1], isDirectory: true)
private let sizes = [16, 32, 64, 128, 256, 512, 1024]
for size in sizes {
autoreleasepool {
let image = NSImage(size: NSSize(width: size, height: size))
image.lockFocus()
let inset = CGFloat(size) * 0.08
let roundedRect = NSRect(
x: inset,
y: inset,
width: CGFloat(size) - inset * 2,
height: CGFloat(size) - inset * 2
)
let clipPath = NSBezierPath(roundedRect: roundedRect, xRadius: CGFloat(size) * 0.22, yRadius: CGFloat(size) * 0.22)
clipPath.addClip()
let gradient = NSGradient(colors: [
NSColor(calibratedRed: 0.06, green: 0.10, blue: 0.23, alpha: 1.0),
NSColor(calibratedRed: 0.12, green: 0.24, blue: 0.53, alpha: 1.0),
NSColor(calibratedRed: 0.97, green: 0.53, blue: 0.18, alpha: 1.0)
])!
gradient.draw(in: roundedRect, angle: -55)
NSColor(calibratedWhite: 1.0, alpha: 0.14).setFill()
NSBezierPath(ovalIn: NSRect(
x: CGFloat(size) * 0.18,
y: CGFloat(size) * 0.56,
width: CGFloat(size) * 0.52,
height: CGFloat(size) * 0.52
)).fill()
NSColor(calibratedRed: 1.0, green: 0.88, blue: 0.46, alpha: 1.0).setFill()
NSBezierPath(ovalIn: NSRect(
x: CGFloat(size) * 0.60,
y: CGFloat(size) * 0.60,
width: CGFloat(size) * 0.16,
height: CGFloat(size) * 0.16
)).fill()
let moonRect = NSRect(
x: CGFloat(size) * 0.24,
y: CGFloat(size) * 0.24,
width: CGFloat(size) * 0.42,
height: CGFloat(size) * 0.42
)
NSColor.white.setFill()
NSBezierPath(ovalIn: moonRect).fill()
NSColor(calibratedRed: 0.12, green: 0.24, blue: 0.53, alpha: 1.0).setFill()
NSBezierPath(ovalIn: moonRect.offsetBy(dx: CGFloat(size) * 0.11, dy: CGFloat(size) * 0.03)).fill()
let slash = NSBezierPath()
slash.lineWidth = max(CGFloat(size) * 0.07, 2.0)
slash.lineCapStyle = .round
slash.move(to: NSPoint(x: CGFloat(size) * 0.26, y: CGFloat(size) * 0.24))
slash.line(to: NSPoint(x: CGFloat(size) * 0.76, y: CGFloat(size) * 0.74))
NSColor.white.setStroke()
slash.stroke()
image.unlockFocus()
guard
let tiff = image.tiffRepresentation,
let bitmap = NSBitmapImageRep(data: tiff),
let pngData = bitmap.representation(using: .png, properties: [:])
else {
fputs("Failed to generate icon at size \(size)\n", stderr)
exit(1)
}
let fileName: String
switch size {
case 16:
fileName = "icon_16x16.png"
case 32:
fileName = "icon_16x16@2x.png"
case 64:
fileName = "icon_32x32@2x.png"
case 128:
fileName = "icon_128x128.png"
case 256:
fileName = "icon_128x128@2x.png"
case 512:
fileName = "icon_256x256@2x.png"
case 1024:
fileName = "icon_512x512@2x.png"
default:
fileName = "icon_\(size)x\(size).png"
}
do {
try FileManager.default.createDirectory(at: outputDirectory, withIntermediateDirectories: true)
try pngData.write(to: outputDirectory.appendingPathComponent(fileName))
if size == 32 {
try pngData.write(to: outputDirectory.appendingPathComponent("icon_32x32.png"))
} else if size == 256 {
try pngData.write(to: outputDirectory.appendingPathComponent("icon_256x256.png"))
} else if size == 512 {
try pngData.write(to: outputDirectory.appendingPathComponent("icon_512x512.png"))
}
} catch {
fputs("Failed to write icon at size \(size): \(error)\n", stderr)
exit(1)
}
}
}