Skip to content

Instantly share code, notes, and snippets.

@CodeSlicing
Created November 15, 2020 11:13
Show Gist options
  • Select an option

  • Save CodeSlicing/0e46d40b64a192f67200dd00098c89e3 to your computer and use it in GitHub Desktop.

Select an option

Save CodeSlicing/0e46d40b64a192f67200dd00098c89e3 to your computer and use it in GitHub Desktop.
Chrome Icon source which is compatible with PureSwiftUI v2.0.1+ (or PureSwiftUITools v2.0.1+)
//
// ChromeIcon.swift
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright © 2020 Adam Fordyce. All rights reserved.
//
import SwiftUI
import PureSwiftUI
private let yellow: Color = .rgb8(255, 203, 66)
private let green: Color = .rgb8(14, 156, 92)
private let red: Color = .rgb8(218, 72, 59)
private let blue: Color = .rgb8(70, 135, 243)
private let outerColors = [yellow, green, red]
private let innerRadiusRatio: CGFloat = 0.46
private let outerRadiusRatio: CGFloat = 0.73
private let blueRadiusRatio: CGFloat = 0.37
private let innerLayoutGuide = LayoutGuideConfig.polar(rings: [innerRadiusRatio], segments: 3)
private let outerLayoutGuide = LayoutGuideConfig.polar(rings: 1, segments: 3).rotated(62.7.degrees)
struct ChromeIcon: View {
var body: some View {
GeometryReader { (geo: GeometryProxy) in
ZStack {
RoundedRectangle(geo.widthScaled(0.2))
.fill(Color.white)
ForEach(0..<outerColors.count) { index in
ZStack {
let sliceShape = Slice()
.fill(outerColors[index])
sliceShape
Shadow()
.fill(Color(white: 0.9))
.blur(geo.widthScaled(0.005))
.mask(sliceShape)
.blendMode(.multiply)
}
.rotate((120 * index).degrees)
}
.frame(geo.widthScaled(outerRadiusRatio))
Circle()
.fill(Color.white)
.frame(geo.widthScaled(outerRadiusRatio * innerRadiusRatio))
Circle()
.fill(blue)
.frame(geo.widthScaled(outerRadiusRatio * blueRadiusRatio))
}
}
}
}
private struct Slice: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
let gInner = innerLayoutGuide.layout(in: rect)
let gOuter = outerLayoutGuide.layout(in: rect)
path.move(rect.center)
path.line(gInner[1, 0])
path.line(gOuter[1, 0])
path.arc(rect.center, radius: rect.halfHeight, startAngle: rect.center.angleTo(gOuter[1, 0]), delta: 120.degrees)
path.line(gInner[1, 1])
return path
}
}
private struct Shadow: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
var gInner = innerLayoutGuide.layout(in: rect)
let gOuter = outerLayoutGuide.layout(in: rect)
path.move(gInner[1, 0])
path.line(gInner.top)
path.line(gOuter[1, 0])
gInner = gInner
.rotated(48.degrees)
path.line(gInner[1, 0])
return path
}
}
struct ChromeIcon_Previews: PreviewProvider {
struct ChromeIcon_Harness: View {
var body: some View {
VStack {
ChromeIcon()
.frame(400)
HStack {
ChromeIcon()
.frame(200)
ChromeIcon()
.frame(50)
}
}
}
}
static var previews: some View {
ChromeIcon_Harness()
.padding()
.previewSizeThatFits()
.background(Color(white: 0.1))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment