Skip to content

Instantly share code, notes, and snippets.

@ObuchiYuki
Created April 2, 2025 05:34
Show Gist options
  • Save ObuchiYuki/281b23e16ad4109d0e0f3aab156025fe to your computer and use it in GitHub Desktop.
Save ObuchiYuki/281b23e16ad4109d0e0f3aab156025fe to your computer and use it in GitHub Desktop.
Vibrancy Effect for SwiftUI
//
// VibrancyEffect.swift
// MFileViewer
//
// Created by yuki on 2025/02/07.
//
import SwiftUI
public struct VibrancyEffect<Content: View>: View {
public var content: Content
public var style: Style
public var thickness: Thickness
public var saturation: CGFloat
public var vibrancy: Bool = true
public enum Style {
case light
case dark
}
public enum Thickness {
case ultraThin // 0.1
case thin // 0.3
case regular // 0.5
case thick // 0.7
case ultraThick // 0.9
case custom(CGFloat)
}
public init(
style: Style = .light,
thickness: Thickness = Thickness.regular,
saturation: CGFloat = 0.7,
@ViewBuilder content: () -> Content
) {
self.style = style
self.thickness = thickness
self.saturation = saturation
self.content = content()
}
public func enableVibrancy(_ enable: Bool) -> Self {
var copy = self
copy.vibrancy = enable
return copy
}
public var body: some View {
ZStack {
self.content
.foregroundColor(self.overlayColor())
.blendMode(.overlay)
.opacity(self.vibrancy ? 1 : 0)
self.content
.foregroundColor(self.contentColor())
.opacity(self.vibrancy ? self.opacityForThickness() : 1)
}
}
private func opacityForThickness() -> Double {
switch self.thickness {
case .ultraThin: return 0.1
case .thin: return 0.3
case .regular: return 0.5
case .thick: return 0.7
case .ultraThick: return 0.9
case .custom(let value): return Double(value)
}
}
private func contentColor() -> Color {
switch self.style {
case .light: return Color.white
case .dark: return Color.black
}
}
private func overlayColor() -> Color {
switch self.style {
case .light: return Color(white: self.saturation)
case .dark: return Color(white: 1.0 - self.saturation)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment