Repository navigation
Expand file tree
/
Copy pathmakeicon.swift
More file actions
279 lines (251 loc) · 11.5 KB
/
Copy pathmakeicon.swift
File metadata and controls
279 lines (251 loc) · 11.5 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import AppKit
// 使用 1024 点设计网格,图形坐标从左上角起算。
let canvasSize = 1024
func point(_ x: CGFloat, _ y: CGFloat) -> NSPoint {
NSPoint(x: x, y: CGFloat(canvasSize) - y)
}
func color(_ hex: UInt32, alpha: CGFloat = 1) -> NSColor {
NSColor(
srgbRed: CGFloat((hex >> 16) & 0xFF) / 255,
green: CGFloat((hex >> 8) & 0xFF) / 255,
blue: CGFloat(hex & 0xFF) / 255,
alpha: alpha
)
}
func roundedRect(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat, radius: CGFloat) -> NSBezierPath {
NSBezierPath(
roundedRect: NSRect(origin: point(x, y + height), size: NSSize(width: width, height: height)),
xRadius: radius, yRadius: radius
)
}
func ellipse(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> NSBezierPath {
NSBezierPath(ovalIn: NSRect(origin: point(x, y + height), size: NSSize(width: width, height: height)))
}
func gradient(_ colors: [NSColor], from start: NSPoint, to end: NSPoint) {
// 所有色标均由 sRGB 颜色组成,且至少有两个色标。
guard let gradient = NSGradient(colors: colors) else {
preconditionFailure("无效的图标渐变")
}
gradient.draw(from: start, to: end, options: [.drawsBeforeStartingLocation, .drawsAfterEndingLocation])
}
func clipped(to path: NSBezierPath, draw: () -> Void) {
NSGraphicsContext.saveGraphicsState()
defer { NSGraphicsContext.restoreGraphicsState() }
path.addClip()
draw()
}
func shadowed(blur: CGFloat, offset: NSSize, opacity: CGFloat, draw: () -> Void) {
NSGraphicsContext.saveGraphicsState()
defer { NSGraphicsContext.restoreGraphicsState() }
let shadow = NSShadow()
shadow.shadowColor = color(0x57321F, alpha: opacity)
shadow.shadowBlurRadius = blur
shadow.shadowOffset = offset
shadow.set()
draw()
}
func rotated(_ degrees: CGFloat, around center: NSPoint, draw: () -> Void) {
NSGraphicsContext.saveGraphicsState()
defer { NSGraphicsContext.restoreGraphicsState() }
let transform = NSAffineTransform()
transform.translateX(by: center.x, yBy: center.y)
transform.rotate(byDegrees: degrees)
transform.translateX(by: -center.x, yBy: -center.y)
transform.concat()
draw()
}
func mirrored(_ path: NSBezierPath) -> NSBezierPath {
let transform = NSAffineTransform()
transform.translateX(by: CGFloat(canvasSize), yBy: 0)
transform.scaleX(by: -1, yBy: 1)
let result = NSBezierPath(cgPath: path.cgPath)
result.transform(using: transform as AffineTransform)
return result
}
func filled(_ path: NSBezierPath, colors: [UInt32], from start: NSPoint, to end: NSPoint) {
clipped(to: path) {
gradient(colors.map { color($0) }, from: start, to: end)
}
}
func tileOutline() -> NSBezierPath {
let path = NSBezierPath()
path.move(to: point(327, 100))
path.line(to: point(697, 100))
path.curve(to: point(924, 327), controlPoint1: point(860, 100), controlPoint2: point(924, 164))
path.line(to: point(924, 697))
path.curve(to: point(697, 924), controlPoint1: point(924, 860), controlPoint2: point(860, 924))
path.line(to: point(327, 924))
path.curve(to: point(100, 697), controlPoint1: point(164, 924), controlPoint2: point(100, 860))
path.line(to: point(100, 327))
path.curve(to: point(327, 100), controlPoint1: point(100, 164), controlPoint2: point(164, 100))
path.close()
return path
}
func drawHornsAndEars() {
let horn = NSBezierPath()
horn.move(to: point(363, 365))
horn.curve(to: point(280, 316), controlPoint1: point(320, 370), controlPoint2: point(290, 346))
horn.curve(to: point(252, 222), controlPoint1: point(257, 284), controlPoint2: point(246, 247))
horn.curve(to: point(267, 216), controlPoint1: point(253, 211), controlPoint2: point(261, 208))
horn.curve(to: point(310, 273), controlPoint1: point(281, 241), controlPoint2: point(289, 260))
horn.curve(to: point(372, 297), controlPoint1: point(329, 284), controlPoint2: point(350, 288))
horn.close()
let ear = NSBezierPath()
ear.move(to: point(359, 399))
ear.curve(to: point(206, 364), controlPoint1: point(319, 349), controlPoint2: point(250, 349))
ear.curve(to: point(193, 385), controlPoint1: point(190, 368), controlPoint2: point(186, 376))
ear.curve(to: point(276, 475), controlPoint1: point(205, 430), controlPoint2: point(232, 469))
ear.curve(to: point(361, 446), controlPoint1: point(315, 482), controlPoint2: point(342, 468))
ear.close()
let innerEar = NSBezierPath()
innerEar.move(to: point(326, 409))
innerEar.curve(to: point(225, 387), controlPoint1: point(293, 383), controlPoint2: point(252, 380))
innerEar.curve(to: point(282, 449), controlPoint1: point(238, 421), controlPoint2: point(257, 446))
innerEar.curve(to: point(326, 409), controlPoint1: point(303, 451), controlPoint2: point(319, 430))
innerEar.close()
for path in [horn, mirrored(horn)] {
shadowed(blur: 14, offset: NSSize(width: 0, height: -8), opacity: 0.16) {
color(0xB99A6B).setFill()
path.fill()
}
filled(path, colors: [0xFFF9DD, 0xF0D5A3, 0xC49C64], from: point(512, 217), to: point(512, 370))
}
for path in [ear, mirrored(ear)] {
shadowed(blur: 14, offset: NSSize(width: 0, height: -9), opacity: 0.18) {
color(0x49372F).setFill()
path.fill()
}
filled(path, colors: [0x6A5143, 0x3C302B], from: point(512, 355), to: point(512, 478))
}
for path in [innerEar, mirrored(innerEar)] {
filled(path, colors: [0xE6AD97, 0xBF806B], from: point(512, 383), to: point(512, 450))
}
}
func faceOutline() -> NSBezierPath {
let face = NSBezierPath()
face.move(to: point(512, 292))
face.curve(to: point(689, 395), controlPoint1: point(616, 281), controlPoint2: point(676, 320))
face.curve(to: point(691, 541), controlPoint1: point(711, 437), controlPoint2: point(704, 494))
face.curve(to: point(653, 704), controlPoint1: point(680, 613), controlPoint2: point(681, 666))
face.curve(to: point(512, 779), controlPoint1: point(626, 750), controlPoint2: point(569, 779))
face.curve(to: point(371, 704), controlPoint1: point(455, 779), controlPoint2: point(398, 750))
face.curve(to: point(333, 541), controlPoint1: point(343, 666), controlPoint2: point(344, 613))
face.curve(to: point(335, 395), controlPoint1: point(320, 494), controlPoint2: point(313, 437))
face.curve(to: point(512, 292), controlPoint1: point(348, 320), controlPoint2: point(408, 281))
face.close()
return face
}
func drawFace() {
let face = faceOutline()
shadowed(blur: 29, offset: NSSize(width: 0, height: -18), opacity: 0.23) {
color(0xD3B991).setFill()
face.fill()
}
filled(face, colors: [0xFFFDF1, 0xF7EFDC, 0xDFCFB3], from: point(410, 300), to: point(670, 765))
// 非对称奶牛花纹:一块包住左眼,另一块轻轻掠过右额。
let patch = NSBezierPath()
patch.move(to: point(329, 331))
patch.curve(to: point(449, 320), controlPoint1: point(374, 298), controlPoint2: point(416, 300))
patch.curve(to: point(475, 411), controlPoint1: point(486, 347), controlPoint2: point(491, 384))
patch.curve(to: point(478, 493), controlPoint1: point(459, 448), controlPoint2: point(491, 460))
patch.curve(to: point(397, 561), controlPoint1: point(467, 536), controlPoint2: point(439, 566))
patch.curve(to: point(320, 517), controlPoint1: point(357, 562), controlPoint2: point(331, 547))
patch.close()
let temple = NSBezierPath()
temple.move(to: point(603, 300))
temple.curve(to: point(606, 381), controlPoint1: point(572, 326), controlPoint2: point(576, 360))
temple.curve(to: point(706, 441), controlPoint1: point(638, 404), controlPoint2: point(683, 392))
temple.line(to: point(733, 320))
temple.close()
clipped(to: face) {
for path in [patch, temple] {
filled(path, colors: [0x645044, 0x3F332E], from: point(360, 304), to: point(480, 557))
}
}
// 一撮有弧度的额发,让正面轮廓带一点性格。
let tuft = NSBezierPath()
tuft.move(to: point(439, 326))
tuft.curve(to: point(484, 280), controlPoint1: point(446, 301), controlPoint2: point(458, 282))
tuft.curve(to: point(513, 288), controlPoint1: point(495, 278), controlPoint2: point(505, 282))
tuft.curve(to: point(554, 272), controlPoint1: point(527, 278), controlPoint2: point(542, 273))
tuft.curve(to: point(532, 321), controlPoint1: point(553, 294), controlPoint2: point(546, 310))
tuft.curve(to: point(487, 342), controlPoint1: point(521, 336), controlPoint2: point(504, 341))
tuft.close()
filled(tuft, colors: [0xFFFDF0, 0xE6D7BD], from: point(481, 280), to: point(511, 344))
}
func drawEyesAndMuzzle() {
for x: CGFloat in [409, 574] {
let eye = ellipse(x, 462, 44, 58)
color(0x261F1C).setFill()
if x < 500 {
// 左眼位于深色斑块中,留出一圈奶油色眼白。
color(0xFFF5E2).setFill()
ellipse(x - 8, 454, 60, 74).fill()
color(0x261F1C).setFill()
}
eye.fill()
color(0xFFFFFF, alpha: 0.96).setFill()
ellipse(x + 8, 470, 13, 15).fill()
}
let muzzle = roundedRect(337, 570, 350, 198, radius: 94)
shadowed(blur: 15, offset: NSSize(width: 0, height: -9), opacity: 0.16) {
color(0xC58270).setFill()
muzzle.fill()
}
filled(muzzle, colors: [0xF6C7B5, 0xECA894, 0xD88D79], from: point(421, 570), to: point(593, 768))
for (x, angle): (CGFloat, CGFloat) in [(413, -16), (568, 16)] {
rotated(angle, around: point(x + 21, 637)) {
filled(ellipse(x, 613, 42, 48), colors: [0x815047, 0x9E6559],
from: point(x, 613), to: point(x, 661))
}
}
let smile = NSBezierPath()
smile.move(to: point(471, 706))
smile.curve(to: point(553, 706), controlPoint1: point(492, 724), controlPoint2: point(532, 724))
smile.lineWidth = 7
smile.lineCapStyle = .round
color(0xAA6D5F, alpha: 0.8).setStroke()
smile.stroke()
}
func drawIcon() {
let tile = tileOutline()
shadowed(blur: 15, offset: NSSize(width: 0, height: -9), opacity: 0.16) {
color(0xDB9B51).setFill()
tile.fill()
}
filled(tile, colors: [0xFFE6B1, 0xF4C57F, 0xEBA85F], from: point(300, 100), to: point(730, 924))
clipped(to: tile) {
color(0xFFF5DB, alpha: 0.65).setStroke()
tile.lineWidth = 4
tile.stroke()
}
drawHornsAndEars()
drawFace()
drawEyesAndMuzzle()
}
enum IconError: Error {
case missingOutputPath
case imageCreationFailed
}
func renderIcon(to url: URL) throws {
guard let bitmap = NSBitmapImageRep(
bitmapDataPlanes: nil, pixelsWide: canvasSize, pixelsHigh: canvasSize,
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0
), let context = NSGraphicsContext(bitmapImageRep: bitmap) else {
throw IconError.imageCreationFailed
}
NSGraphicsContext.saveGraphicsState()
defer { NSGraphicsContext.restoreGraphicsState() }
NSGraphicsContext.current = context
context.cgContext.clear(CGRect(x: 0, y: 0, width: canvasSize, height: canvasSize))
drawIcon()
guard let png = bitmap.representation(using: .png, properties: [:]) else {
throw IconError.imageCreationFailed
}
try png.write(to: url, options: .atomic)
}
guard CommandLine.arguments.count == 2 else {
throw IconError.missingOutputPath
}
try renderIcon(to: URL(fileURLWithPath: CommandLine.arguments[1]))