Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OskarGroth/960e20e73e0a45a536403fe063bd5a0b to your computer and use it in GitHub Desktop.
Save OskarGroth/960e20e73e0a45a536403fe063bd5a0b to your computer and use it in GitHub Desktop.
AppleLogo.swift
//
// ContentView.swift
// AppleLogo
//
// Created by Oskar Groth on 2021-06-17.
//
import SwiftUI
struct ContentView: View {
static let colors = [Color.green, .yellow, .orange, .red, .purple, .blue]
var body: some View {
Canvas { context, size in
let image = context.resolve(Image(systemName: "applelogo"))
let ratio = min(size.width / image.size.width, size.height / image.size.height)
let drawSize = CGSize(width: image.size.width * ratio, height: image.size.height * ratio)
let height = size.height/CGFloat(Self.colors.count + 2)
context.clipToLayer(content: { innerContext in
innerContext.draw(image, in: .init(origin: .init(x: (size.width - drawSize.width)/2, y: -2), size: drawSize))
})
for (index, color) in Self.colors.enumerated() {
context.fill(
Path(
CGRect(origin: .init(x: 0, y: index == 0 ? 0 : Int(height) * (2 + index)),
size: .init(width: size.width, height: index == 0 ? height * 3 : height))
), with: .color(color)
)
}
}
.frame(width: 128, height: 128)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
@mattyoung
Copy link

mattyoung commented Jun 17, 2021

For more optimizing: move the .green color special case out of the loop to avoid extra conditional code inside the loop:

struct AppleLogoCanvasV2: View {
    // .green is special, not in here
    static let colors = [Color.yellow, .orange, .red, .purple, .blue]
    
    var body: some View {
        Canvas { context, size in
            let image = context.resolve(Image(systemName: "applelogo"))
            let ratio = min(size.width / image.size.width, size.height / image.size.height)
            let drawSize = CGSize(width: image.size.width * ratio, height: image.size.height * ratio)
            let height = size.height/Double(Self.colors.count + 3)
            context.clipToLayer(content: { innerContext in
                innerContext.draw(image, in: .init(origin: .init(x: (size.width - drawSize.width)/2, y: -2), size: drawSize))
            })

            // special case for .green
            context.fill(
                Path(
                    CGRect(origin: .init(x: 0, y: 0),
                           size: .init(width: size.width, height: height * 3))
                ), with: .color(.green)
            )

            for (index, color) in Self.colors.enumerated() {
                context.fill(
                    Path(
                        CGRect(origin: .init(x: 0, y: Int(height) * (3 + index)),
                               size: .init(width: size.width, height: height))
                    ), with: .color(color)
                )
            }
        }
        .frame(width: 128, height: 128)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment