Created
April 7, 2023 03:19
-
-
Save christianselig/5497c7798bc51a806931f35e982402b5 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 UIColor { | |
/// Blends this color into the specified color, as if this color was overlaid on top of the other at the specified alpha, but our result will instead be opaque. For instance if self was green, and we passed white and 0.1 alpha/opacity, the result would be a light, faded green. | |
/// | |
/// - Note: This process is also called alpha compositing. | |
func blend(intoColor otherColor: UIColor, atOpacity alpha: CGFloat) -> UIColor { | |
let sourceRGB = rgb | |
let otherRGB = otherColor.rgb | |
return UIColor( | |
red: (sourceRGB.red * alpha) + (otherRGB.red * (1.0 - alpha)), | |
green: (sourceRGB.green * alpha) + (otherRGB.green * (1.0 - alpha)), | |
blue: (sourceRGB.blue * alpha) + (otherRGB.blue * (1.0 - alpha)), | |
alpha: 1.0 | |
) | |
} | |
var rgb: (red: CGFloat, green: CGFloat, blue: CGFloat) { | |
var red: CGFloat = 0.0 | |
var green: CGFloat = 0.0 | |
var blue: CGFloat = 0.0 | |
getRed(&red, green: &green, blue: &blue, alpha: nil) | |
return (red, green, blue) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can be used like: