@@ -14,32 +14,35 @@ import CoreGraphics
1414import Foundation
1515
1616guard CommandLine . arguments. count == 3 else {
17- print ( " usage: make-icon <source> <output-1024.png> " )
18- exit ( 1 )
17+ print ( " usage: make-icon <source> <output-1024.png> " )
18+ exit ( 1 )
1919}
2020let srcURL = URL ( fileURLWithPath: CommandLine . arguments [ 1 ] )
2121let dstURL = URL ( fileURLWithPath: CommandLine . arguments [ 2 ] )
2222
2323guard let srcImage = NSImage ( contentsOf: srcURL) ,
24- let srcCG = srcImage. cgImage ( forProposedRect: nil , context: nil , hints: nil )
24+ let srcCG = srcImage. cgImage ( forProposedRect: nil , context: nil , hints: nil )
2525else {
26- print ( " failed to load source image at \( srcURL. path) " )
27- exit ( 1 )
26+ print ( " failed to load source image at \( srcURL. path) " )
27+ exit ( 1 )
2828}
2929
3030let srcW = srcCG. width
3131let srcH = srcCG. height
3232let side = min ( srcW, srcH)
3333let cropOriginX = ( srcW - side) / 2
3434let cropOriginY = ( srcH - side) / 2
35- guard let cropped = srcCG. cropping ( to: CGRect (
36- x: cropOriginX,
37- y: cropOriginY,
38- width: side,
39- height: side
40- ) ) else {
41- print ( " crop failed " )
42- exit ( 1 )
35+ guard
36+ let cropped = srcCG. cropping (
37+ to: CGRect (
38+ x: cropOriginX,
39+ y: cropOriginY,
40+ width: side,
41+ height: side
42+ ) )
43+ else {
44+ print ( " crop failed " )
45+ exit ( 1 )
4346}
4447
4548let canvas : CGFloat = 1024
@@ -49,33 +52,33 @@ let inset = (canvas - inner) / 2
4952let cornerRadius = inner * 0.2237
5053
5154guard let colorSpace = CGColorSpace ( name: CGColorSpace . sRGB) ,
52- let ctx = CGContext (
53- data: nil ,
54- width: Int ( canvas) ,
55- height: Int ( canvas) ,
56- bitsPerComponent: 8 ,
57- bytesPerRow: 0 ,
58- space: colorSpace,
59- bitmapInfo: CGImageAlphaInfo . premultipliedLast. rawValue
60- )
55+ let ctx = CGContext (
56+ data: nil ,
57+ width: Int ( canvas) ,
58+ height: Int ( canvas) ,
59+ bitsPerComponent: 8 ,
60+ bytesPerRow: 0 ,
61+ space: colorSpace,
62+ bitmapInfo: CGImageAlphaInfo . premultipliedLast. rawValue
63+ )
6164else {
62- print ( " CGContext init failed " )
63- exit ( 1 )
65+ print ( " CGContext init failed " )
66+ exit ( 1 )
6467}
6568
6669// Drop shadow on the squircle itself.
6770ctx. setShadow (
68- offset: CGSize ( width: 0 , height: - 8 ) ,
69- blur: 24 ,
70- color: CGColor ( red: 0 , green: 0 , blue: 0 , alpha: 0.45 )
71+ offset: CGSize ( width: 0 , height: - 8 ) ,
72+ blur: 24 ,
73+ color: CGColor ( red: 0 , green: 0 , blue: 0 , alpha: 0.45 )
7174)
7275
7376let innerRect = CGRect ( x: inset, y: inset, width: inner, height: inner)
7477let path = CGPath (
75- roundedRect: innerRect,
76- cornerWidth: cornerRadius,
77- cornerHeight: cornerRadius,
78- transform: nil
78+ roundedRect: innerRect,
79+ cornerWidth: cornerRadius,
80+ cornerHeight: cornerRadius,
81+ transform: nil
7982)
8083
8184ctx. saveGState ( )
@@ -92,31 +95,32 @@ ctx.draw(cropped, in: innerRect)
9295ctx. setShadow ( offset: . zero, blur: 0 , color: nil )
9396let centre = CGPoint ( x: innerRect. midX, y: innerRect. midY)
9497let endRadius = inner * 0.72
95- let colors = [
98+ let colors =
99+ [
96100 CGColor ( red: 0 , green: 0 , blue: 0 , alpha: 0.0 ) ,
97101 CGColor ( red: 0 , green: 0 , blue: 0 , alpha: 0.0 ) ,
98102 CGColor ( red: 0 , green: 0 , blue: 0 , alpha: 0.55 ) ,
99- ] as CFArray
103+ ] as CFArray
100104let locations : [ CGFloat ] = [ 0.0 , 0.55 , 1.0 ]
101105if let gradient = CGGradient ( colorsSpace: colorSpace, colors: colors, locations: locations) {
102- ctx. drawRadialGradient (
103- gradient,
104- startCenter: centre, startRadius: 0 ,
105- endCenter: centre, endRadius: endRadius,
106- options: [ ]
107- )
106+ ctx. drawRadialGradient (
107+ gradient,
108+ startCenter: centre, startRadius: 0 ,
109+ endCenter: centre, endRadius: endRadius,
110+ options: [ ]
111+ )
108112}
109113
110114ctx. restoreGState ( )
111115
112116guard let outCG = ctx. makeImage ( ) else {
113- print ( " makeImage failed " )
114- exit ( 1 )
117+ print ( " makeImage failed " )
118+ exit ( 1 )
115119}
116120let rep = NSBitmapImageRep ( cgImage: outCG)
117121guard let png = rep. representation ( using: . png, properties: [ : ] ) else {
118- print ( " png encode failed " )
119- exit ( 1 )
122+ print ( " png encode failed " )
123+ exit ( 1 )
120124}
121125try png. write ( to: dstURL)
122126print ( " wrote \( dstURL. path) " )
0 commit comments