Created
September 15, 2021 16:48
-
-
Save Exey/3d8c3c5bc76e0d298d88e8b933d191a9 to your computer and use it in GitHub Desktop.
ColorAnimation.swift
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
private struct ColorAnimation: AnimatableModifier { | |
var animatableData: Double | |
private let rgbaPair: [(Double, Double)] | |
init(from: Color, to: Color, percentToColor: Double) { | |
animatableData = percentToColor | |
let fromComponents = UIColor(from).cgColor.components! | |
let toComponents = UIColor(to).cgColor.components! | |
rgbaPair = Array(zip(fromComponents.map(Double.init), toComponents.map(Double.init))) | |
} | |
func body(content: Content) -> some View { | |
content | |
.foregroundColor(mixedColor) | |
} | |
// This is a very basic implementation of a color interpolation between two values. | |
private var mixedColor: Color { | |
let rgba = rgbaPair.map { $0.0 + ($0.1 - $0.0) * animatableData } | |
return Color(red: rgba[0], green: rgba[1], blue: rgba[2], opacity: rgba[3]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment