25 lines
771 B
Swift
25 lines
771 B
Swift
import Foundation
|
|
import SwiftUI
|
|
|
|
extension Color {
|
|
|
|
func blend(to other: Color, intensity: CGFloat = 0.5) -> Color {
|
|
Color(UIColor(self).blend(to: UIColor(other), intensity: intensity))
|
|
}
|
|
}
|
|
|
|
extension UIColor {
|
|
|
|
func blend(to other: UIColor, intensity: CGFloat = 0.5) -> UIColor {
|
|
let l2 = max(0.0, min(1.0, intensity))
|
|
let l1 = 1 - l2
|
|
var (r1, g1, b1, a1): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0)
|
|
var (r2, g2, b2, a2): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0)
|
|
|
|
getRed(&r1, green: &g1, blue: &b1, alpha: &a1)
|
|
other.getRed(&r2, green: &g2, blue: &b2, alpha: &a2)
|
|
|
|
return UIColor(red: l1*r1 + l2*r2, green: l1*g1 + l2*g2, blue: l1*b1 + l2*b2, alpha: l1*a1 + l2*a2)
|
|
}
|
|
}
|