Created
February 12, 2021 17:49
-
-
Save CodeSlicing/f5e5acc18b35c0d2e7f601c1989909da to your computer and use it in GitHub Desktop.
Source-code for CodeSlicing Episode on inner-shadows
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
// | |
// InnerShadowsDemo.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. | |
// | |
// Created by Adam Fordyce on 12/02/2021. | |
// Copyright © 2021 Adam Fordyce. All rights reserved. | |
// | |
import SwiftUI | |
import PureSwiftUI | |
private let niceBackground = LinearGradient([.red, .orange], to: .trailing) | |
struct InnerShadowsDemo: View { | |
var body: some View { | |
Image("claw-marks") | |
.innerShadow(Color.rgb8(200, 0, 0), radius: 10, opacity: 0.8, offset: .x(5)) | |
.shadowColor(Color(white: 0.8), 5, x: -6, y: -2) | |
.shadowColor(Color(white: 0.2), 5, x: 6, y: 2) | |
.shadowColor(Color(white: 0.4), 10, x: 10, y: 5) | |
.shadowColor(Color(white: 0.75), 10, x: -10, y: -5) | |
.rotate(10.degrees) | |
} | |
} | |
private extension Image { | |
func innerShadow<V: View>(_ background: V, radius: CGFloat = 5, opacity: Double = 0.7, offset: CGPoint = .zero) -> some View { | |
Color.clear | |
.background(background) | |
.blendMode(.multiply) | |
.background( | |
ZStack { | |
Color(white: 1 - opacity) | |
self.renderingMode(.template).foregroundColor(.white).blur(radius).offset(offset) | |
} | |
) | |
.mask(self) | |
} | |
} | |
private extension Shape { | |
func innerShadow<V: View>(_ background: V, radius: CGFloat = 5, opacity: Double = 0.7, offset: CGPoint = .zero) -> some View { | |
self.fill(Color.clear).innerShadow(background, self, radius: radius, opacity: opacity, offset: offset) | |
} | |
} | |
private extension View { | |
func innerShadow<V: View, S: Shape>(_ background: V, _ shape: S, radius: CGFloat = 5, opacity: Double = 0.7, offset: CGPoint = .zero) -> some View { | |
self | |
.background(background) | |
.blendMode(.multiply) | |
.background( | |
ZStack { | |
shape.fill(Color(white: 1 - opacity)) | |
shape.fill(Color.white).blur(radius).offset(offset) | |
} | |
) | |
.mask(self.overlay(shape)) | |
} | |
} | |
private extension Text { | |
func innerShadow<V: View>(_ background: V, radius: CGFloat = 5, opacity: Double = 0.7, offset: CGPoint = .zero) -> some View { | |
self | |
.foregroundColor(.clear) | |
.overlay(background.mask(self)) | |
.overlay( | |
ZStack { | |
self.foregroundColor(Color(white: 1 - opacity)) | |
self.foregroundColor(.white).blur(radius).offset(offset) | |
} | |
.mask(self) | |
.blendMode(.multiply) | |
) | |
} | |
} | |
struct InnerShadowsDemo_Previews: PreviewProvider { | |
struct InnerShadowsDemo_Harness: View { | |
var body: some View { | |
InnerShadowsDemo() | |
.greedyFrame() | |
.background(LinearGradient([Color(white: 0.7), Color(white: 0.4)], to: .bottomTrailing)) | |
.ignoresSafeArea() | |
} | |
} | |
static var previews: some View { | |
InnerShadowsDemo_Harness() | |
.previewDevice(.iPhone_12_Pro_Max) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment