swift 颜色合成

最近有一个颜色叠加需求 找到篇文章是java的代码译成swift果真有用 分享一下 也当做笔记记录一下

func calculateBlend(a1: CGFloat, a2: CGFloat, c1: CGFloat, c2: CGFloat) ->     CGFloat {
    return (c1 * a1 * (1.0 - a2) + c2 * a2) / (a1 + a2 - a1 * a2)
 }

func colorBlend(color1: UIColor, color2: UIColor) -> UIColor {
    let alp1 = color1.RGB.a
    let alp2 = color2.RGB.a
    let alpBlend = alp1 + alp2 - alp1 * alp2

    let red1 = color1.RGB.r
    let red2 = color2.RGB.r
    let redBlend = self.calculateBlend(a1: alp1, a2: alp2, c1: red1, c2: red2)

    let green1 = color1.RGB.g
    let green2 = color2.RGB.g
    let greenBlend = self.calculateBlend(a1: alp1, a2: alp2, c1: green1, c2: green2)

    let blue1 = color1.RGB.b
    let blue2 = color2.RGB.b
    let blueBlend = self.calculateBlend(a1: alp1, a2: alp2, c1: blue1, c2: blue2)

    return UIColor.init(red: redBlend, green: greenBlend, blue: blueBlend, alpha: alpBlend)
}

、、、

你可能感兴趣的:(swift 颜色合成)