Last active
November 1, 2026 17:51
-
-
Save iccir/053ebec1ad3a53fad3dbfbf76743ea94 to your computer and use it in GitHub Desktop.
Generate app icon templates for macOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Generate images that replicate the shape and shadow of | |
| a macOS app icon. | |
| Writes results to '/tmp/macos_*.png' | |
| Public Domain | |
| */ | |
| #import <Foundation/Foundation.h> | |
| #import <AppKit/AppKit.h> | |
| #import <CoreGraphics/CoreGraphics.h> | |
| #import <QuartzCore/QuartzCore.h> | |
| #import <UniformTypeIdentifiers/UniformTypeIdentifiers.h> | |
| #import <CoreImage/CoreImage.h> | |
| static NSColor *sMakeRGBColor(int rgb, CGFloat alpha) | |
| { | |
| float r = (((rgb & 0xFF0000) >> 16) / 255.0); | |
| float g = (((rgb & 0x00FF00) >> 8) / 255.0); | |
| float b = (((rgb & 0x0000FF) >> 0) / 255.0); | |
| return [NSColor colorWithSRGBRed:r green:g blue:b alpha:alpha]; | |
| } | |
| static CGImageRef sCreateImage(size_t width, size_t height, BOOL isMask, void (^callback)(CGContextRef)) | |
| { | |
| if (!width || !height) return NULL; | |
| CFStringRef colorSpaceName = isMask ? kCGColorSpaceGenericGrayGamma2_2 : kCGColorSpaceSRGB; | |
| size_t componentCount = isMask ? 1 : 4; | |
| CGBitmapInfo bitmapInfo = isMask ? | |
| 0 | kCGImageAlphaNone : | |
| 0 | kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little; | |
| CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(colorSpaceName); | |
| if (!colorSpace) return NULL; | |
| CGImageRef result = NULL; | |
| CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, width * componentCount, colorSpace, bitmapInfo); | |
| if (context) { | |
| CGContextTranslateCTM(context, 0, height); | |
| CGContextScaleCTM(context, 1, -1); | |
| NSGraphicsContext *savedContext = [NSGraphicsContext currentContext]; | |
| [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithCGContext:context flipped:YES]]; | |
| callback(context); | |
| [NSGraphicsContext setCurrentContext:savedContext]; | |
| result = CGBitmapContextCreateImage(context); | |
| CFRelease(context); | |
| } | |
| CGColorSpaceRelease(colorSpace); | |
| return result; | |
| } | |
| CGImageRef sCreateMaskImage( | |
| CGFloat squareWidth, | |
| CGFloat cornerRadius | |
| ) { | |
| size_t maskWidth = fmod(squareWidth, 1.0) == 0.0 ? | |
| (size_t)squareWidth : | |
| (size_t)squareWidth + 2; | |
| return sCreateImage(maskWidth, maskWidth, YES, ^(CGContextRef context) { | |
| CGFloat offset = (maskWidth - squareWidth) / 2.0; | |
| [[NSColor blackColor] set]; | |
| CGContextFillRect(context, CGRectMake(0, 0, maskWidth, maskWidth)); | |
| CALayer *layer = [CALayer layer]; | |
| [layer setFrame:CGRectMake(0, 0, squareWidth, squareWidth)]; | |
| [layer setBackgroundColor:[[NSColor whiteColor] CGColor]]; | |
| [layer setCornerRadius:cornerRadius]; | |
| [layer setCornerCurve:kCACornerCurveContinuous]; | |
| CGContextTranslateCTM(context, offset, offset); | |
| [layer renderInContext:context]; | |
| }); | |
| } | |
| CGImageRef sCreateShadowImage( | |
| size_t imageWidth, | |
| CGImageRef maskImage, | |
| CGFloat shadowOpacity, | |
| CGFloat shadowOffset, | |
| CGFloat shadowRadius | |
| ) { | |
| size_t maskWidth = CGImageGetWidth(maskImage); | |
| CGImageRef inputImage = sCreateImage(imageWidth, imageWidth, NO, ^(CGContextRef context) { | |
| CGFloat offset = (imageWidth - maskWidth) / 2.0; | |
| CGRect maskRect = CGRectMake(offset, offset, maskWidth, maskWidth); | |
| maskRect.origin.y += shadowOffset; | |
| CGContextClipToMask(context, maskRect, maskImage); | |
| [[NSColor colorWithWhite:0 alpha:shadowOpacity] set]; | |
| CGContextFillRect(context, maskRect); | |
| }); | |
| CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"]; | |
| [blurFilter setValue:[CIImage imageWithCGImage:inputImage] forKey:kCIInputImageKey]; | |
| [blurFilter setValue:@(shadowRadius) forKey:kCIInputRadiusKey]; | |
| CGRect imageRect = CGRectMake(0, 0, imageWidth, imageWidth); | |
| CIImage *outputCIImage = [blurFilter valueForKey:kCIOutputImageKey]; | |
| CGImageRef outputImage = [[CIContext context] createCGImage:outputCIImage fromRect:imageRect]; | |
| CGImageRelease(inputImage); | |
| return outputImage; | |
| } | |
| static CGRect sGetCenteredFrame(CGRect bounds, CGSize size) | |
| { | |
| return CGRectMake( | |
| bounds.origin.x + ((bounds.size.width - size.width) / 2.0), | |
| bounds.origin.y + ((bounds.size.height - size.height) / 2.0), | |
| size.width, | |
| size.height | |
| ); | |
| } | |
| CGImageRef sCreateAppIcon( | |
| size_t width, size_t height, | |
| CGImageRef shadowImage, | |
| CGImageRef maskImage, | |
| CGImageRef contentImage | |
| ) { | |
| __auto_type getSize = ^(CGImageRef image) { | |
| return CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image)); | |
| }; | |
| return sCreateImage(width, height, NO, ^(CGContextRef context) { | |
| CGRect bounds = CGRectMake(0, 0, width, height); | |
| CGContextTranslateCTM(context, 0, height); | |
| CGContextScaleCTM(context, 1, -1); | |
| CGRect shadowFrame = sGetCenteredFrame(bounds, getSize(shadowImage)); | |
| CGRect maskFrame = sGetCenteredFrame(bounds, getSize(maskImage)); | |
| CGRect contentFrame = sGetCenteredFrame(bounds, getSize(contentImage)); | |
| CGContextDrawImage(context, shadowFrame, shadowImage); | |
| CGContextClipToMask(context, maskFrame, maskImage); | |
| CGContextDrawImage(context, contentFrame, contentImage); | |
| }); | |
| } | |
| int main(int argc, const char * argv[]) | |
| { | |
| const char *outputPath = "/tmp"; | |
| CGFloat iconWidth = 256.0; | |
| __auto_type createExampleImage = ^(int topColor, int bottomColor, int gridColor) { | |
| size_t x = 220; | |
| return sCreateImage(x, x, NO, ^(CGContextRef context) { | |
| NSGradient *gradient = [[NSGradient alloc] initWithColors:@[ | |
| sMakeRGBColor(topColor, 1.0), | |
| sMakeRGBColor(bottomColor, 1.0) | |
| ]]; | |
| [gradient drawInRect:CGRectMake(0, 0, x, x) angle:-90]; | |
| [sMakeRGBColor(gridColor, 1.0) set]; | |
| CGContextMoveToPoint(context, 0, 0); | |
| CGContextAddLineToPoint(context, x, x); | |
| CGContextMoveToPoint(context, 0, x); | |
| CGContextAddLineToPoint(context, x, 0); | |
| CGContextMoveToPoint(context, 0, x / 2.0); | |
| CGContextAddLineToPoint(context, x, x / 2.0); | |
| CGContextMoveToPoint(context, x / 2.0, 0); | |
| CGContextAddLineToPoint(context, x / 2.0, x); | |
| CGContextStrokePath(context); | |
| [[NSColor colorWithSRGBRed:0.85 green:0 blue:0 alpha:1.0] set]; | |
| CGContextFillEllipseInRect(context, CGRectMake(32, 32, 32, 32)); | |
| }); | |
| }; | |
| CGImageRef lightImage = createExampleImage(0xfcfcfc, 0xf8f8f8, 0x808080); | |
| CGImageRef darkImage = createExampleImage(0x303030, 0x141414, 0x808080); | |
| __auto_type writeImage = ^(NSString *name, CGImageRef image) { | |
| NSString *fullPath = [NSString stringWithFormat:@"%s/%@.png", outputPath, name]; | |
| NSURL *outURL = [NSURL fileURLWithPath:fullPath]; | |
| CGImageDestinationRef dest = CGImageDestinationCreateWithURL( | |
| (__bridge CFURLRef)outURL, | |
| (__bridge CFStringRef)[UTTypePNG identifier], | |
| 1, | |
| NULL | |
| ); | |
| if (dest) { | |
| CGImageDestinationAddImage(dest, image, NULL); | |
| CGImageDestinationFinalize(dest); | |
| CFRelease(dest); | |
| } else { | |
| fprintf(stderr, "Error: could not write output image to %s\n", outputPath); | |
| } | |
| }; | |
| __auto_type writeExampleImages = ^(NSString *prefix, CGImageRef shadowImage, CGImageRef maskImage) { | |
| CGImageRef example1 = sCreateAppIcon(iconWidth, iconWidth, shadowImage, maskImage, lightImage); | |
| CGImageRef example2 = sCreateAppIcon(iconWidth, iconWidth, shadowImage, maskImage, darkImage); | |
| writeImage([NSString stringWithFormat:@"%@_light", prefix], example1); | |
| writeImage([NSString stringWithFormat:@"%@_dark", prefix], example2); | |
| CGImageRelease(example1); | |
| CGImageRelease(example2); | |
| }; | |
| // macOS 11 - macOS 15 | |
| { | |
| CGFloat squareWidth = 206.0; | |
| CGFloat cornerRadius = 45.5; | |
| CGFloat shadowOpacity = 0.3; | |
| CGFloat shadowOffset = 3; | |
| CGFloat shadowRadius = 3; | |
| CGImageRef bigSurMask = sCreateMaskImage(squareWidth, cornerRadius); | |
| CGImageRef bigSurShadow = sCreateShadowImage(iconWidth, bigSurMask, | |
| shadowOpacity, | |
| shadowOffset, | |
| shadowRadius | |
| ); | |
| writeImage(@"macos_11_shadow", bigSurShadow); | |
| writeImage(@"macos_11_mask", bigSurMask); | |
| writeExampleImages(@"macos_11", bigSurShadow, bigSurMask); | |
| CGImageRelease(bigSurShadow); | |
| CGImageRelease(bigSurMask); | |
| } | |
| // macOS 26 - macOS 27 | |
| { | |
| CGFloat squareWidth = 208; | |
| CGFloat cornerRadius = 54; | |
| CGFloat shadowOpacity = 0.28; | |
| CGFloat shadowOffset = 2; | |
| CGFloat shadowRadius = 5; | |
| CGImageRef tahoeMask = sCreateMaskImage(squareWidth, cornerRadius); | |
| CGImageRef tahoeShadow = sCreateShadowImage(iconWidth, tahoeMask, | |
| shadowOpacity, | |
| shadowOffset, | |
| shadowRadius | |
| ); | |
| writeImage(@"macos_26_shadow", tahoeShadow); | |
| writeImage(@"macos_26_mask", tahoeMask); | |
| writeExampleImages(@"macos_26", tahoeShadow, tahoeMask); | |
| CGImageRelease(tahoeShadow); | |
| CGImageRelease(tahoeMask); | |
| } | |
| CGImageRelease(lightImage); | |
| CGImageRelease(darkImage); | |
| exit(0); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that in the latest beta of Golden Gate, dark icons appear to use a gradient of #202020 to #101010 (sRGB).