-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate_icon.swift
More file actions
executable file
·73 lines (59 loc) · 1.91 KB
/
generate_icon.swift
File metadata and controls
executable file
·73 lines (59 loc) · 1.91 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
#!/usr/bin/swift
import Cocoa
import AppKit
// 创建一个 1024x1024 的图标
let size = CGSize(width: 1024, height: 1024)
let image = NSImage(size: size)
image.lockFocus()
// 绘制蓝色圆角矩形背景
let rect = NSRect(origin: .zero, size: size)
let cornerRadius: CGFloat = 180.0 // 圆角半径
// 创建蓝色渐变(从浅蓝到深蓝)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let colors = [
NSColor(red: 0.4, green: 0.7, blue: 1.0, alpha: 1.0).cgColor, // 浅蓝
NSColor(red: 0.2, green: 0.5, blue: 0.9, alpha: 1.0).cgColor // 深蓝
] as CFArray
let gradient = CGGradient(
colorsSpace: colorSpace,
colors: colors,
locations: [0.0, 1.0]
)!
// 创建圆角矩形路径
let path = NSBezierPath(roundedRect: rect, xRadius: cornerRadius, yRadius: cornerRadius)
// 裁剪到圆角矩形
path.addClip()
// 绘制渐变
let context = NSGraphicsContext.current!.cgContext
context.drawLinearGradient(
gradient,
start: CGPoint(x: 0, y: size.height),
end: CGPoint(x: 0, y: 0),
options: []
)
// 绘制白色的终端符号 "> _"
let attributes: [NSAttributedString.Key: Any] = [
.font: NSFont.systemFont(ofSize: 400, weight: .semibold),
.foregroundColor: NSColor.white
]
let text = "> _" as NSString
let textSize = text.size(withAttributes: attributes)
let textRect = NSRect(
x: (size.width - textSize.width) / 2,
y: (size.height - textSize.height) / 2 + 20,
width: textSize.width,
height: textSize.height
)
text.draw(in: textRect, withAttributes: attributes)
image.unlockFocus()
// 保存为 PNG
if let tiffData = image.tiffRepresentation,
let bitmapImage = NSBitmapImageRep(data: tiffData),
let pngData = bitmapImage.representation(using: .png, properties: [:]) {
let url = URL(fileURLWithPath: "Resources/icon.png")
try? pngData.write(to: url)
print("✅ 图标已生成: Resources/icon.png")
} else {
print("❌ 生成图标失败")
exit(1)
}