Last active
June 8, 2020 14:58
-
-
Save ha1f/185805424e0b667fa4237b4e63f27e65 to your computer and use it in GitHub Desktop.
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
extension CGRect { | |
/// 線形補間した矩形を返す | |
/// - parameter rate: 変化割合。0 <= rate <= 1の範囲で指定する。 | |
func lerp(to rect: CGRect, rate: CGFloat) -> CGRect { | |
let lerp = _lerpCurried(rate) | |
return CGRect( | |
x: lerp(origin.x, rect.origin.x), | |
y: lerp(origin.y, rect.origin.y), | |
width: lerp(width, rect.width), | |
height: lerp(height, rect.height) | |
) | |
} | |
} | |
extension UIColor { | |
/// 線形補間した色を返す | |
func lerp(_ otherColor: UIColor, rate: CGFloat) -> UIColor { | |
var r1: CGFloat = 0, g1: CGFloat = 0, b1: CGFloat = 0, a1: CGFloat = 0, r2: CGFloat = 0, g2: CGFloat = 0, b2: CGFloat = 0, a2: CGFloat = 0 | |
self.getRed(&r1, green: &g1, blue: &b1, alpha: &a1) | |
otherColor.getRed(&r2, green: &g2, blue: &b2, alpha: &a2) | |
let lerp = _lerpCurried(rate) | |
return UIColor( | |
red: lerp(r1, r2), | |
green: lerp(g1, g2), | |
blue: lerp(b1, b2), | |
alpha: lerp(a1, a2) | |
) | |
} | |
} | |
private func _lerpCurried(_ rate: CGFloat) -> (CGFloat, CGFloat) -> CGFloat { | |
let rateInversed = 1 - rate | |
return { from, to in from * rateInversed + to * rate } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment