diff options
| -rw-r--r-- | xM/gen-icon.swift | 34 | 
1 files changed, 13 insertions, 21 deletions
diff --git a/xM/gen-icon.swift b/xM/gen-icon.swift index 5e29016..712e9e6 100644 --- a/xM/gen-icon.swift +++ b/xM/gen-icon.swift @@ -1,4 +1,4 @@ -// gen-icon.awk: generate a program icon for xM in the Apple icon format +// gen-icon.swift: generate a program icon for xM in the Apple icon format  //  // Copyright (c) 2023, Přemysl Eric Janouch <p@janouch.name>  // SPDX-License-Identifier: 0BSD @@ -10,24 +10,17 @@ import Foundation  import ImageIO  import UniformTypeIdentifiers -// XXX: Not even AppKit provides real superelliptic squircles; screw it. -func drawSquircle(context: CGContext, bounds: CGRect, radius: CGFloat) { -	context.move(to: CGPointMake(bounds.minX, bounds.maxY - radius)) -	context.addArc( -		center: CGPointMake(bounds.minX + radius, bounds.maxY - radius), -		radius: radius, startAngle: .pi, endAngle: .pi / 2, clockwise: true) -	context.addLine(to: CGPointMake(bounds.maxX - radius, bounds.maxY)) -	context.addArc( -		center: CGPointMake(bounds.maxX - radius, bounds.maxY - radius), -		radius: radius, startAngle: .pi / 2, endAngle: 0, clockwise: true) -	context.addLine(to: CGPointMake(bounds.maxX, bounds.maxY - radius)) -	context.addArc( -		center: CGPointMake(bounds.maxX - radius, bounds.minY + radius), -		radius: radius, startAngle: 0, endAngle: .pi / -2, clockwise: true) -	context.addLine(to: CGPointMake(bounds.minX + radius, bounds.minY)) -	context.addArc( -		center: CGPointMake(bounds.minX + radius, bounds.minY + radius), -		radius: radius, startAngle: .pi / -2, endAngle: .pi, clockwise: true) +// Apple uses something that's close to a "quintic superellipse" in their icons, +// but doesn't quite match. Either way, it looks better than rounded rectangles. +func addSquircle(context: CGContext, bounds: CGRect) { +	context.move(to: CGPoint(x: bounds.maxX, y: bounds.midY)) +	for theta in stride(from: 0.0, to: .pi * 2, by: .pi / 1e4) { +		let x = pow(abs(cos(theta)), 2 / 5.0) * bounds.width / 2 +			* CGFloat(signOf: cos(theta), magnitudeOf: 1) + bounds.midX +		let y = pow(abs(sin(theta)), 2 / 5.0) * bounds.height / 2 +			* CGFloat(signOf: sin(theta), magnitudeOf: 1) + bounds.midY +		context.addLine(to: CGPoint(x: x, y: y)) +	}  	context.closePath()  } @@ -42,8 +35,7 @@ func drawIcon(scale: CGFloat) -> CGImage? {  	context.scaleBy(x: scale, y: scale)  	let bounds = CGRectMake(100, 100, size.width - 200, size.height - 200) -	// The radius was something like size.{width,height}/6.4, at least for iOS. -	drawSquircle(context: context, bounds: bounds, radius: 180) +	addSquircle(context: context, bounds: bounds)  	let squircle = context.path!  	// Gradients don't draw shadows, so draw it separately.  | 
