我可以将两个UIColor混合在一起吗?

时间:2012-10-30 04:42:22

标签: objective-c uicolor

我的colorA是[UIColor blueColor],colorB是[UIColor redColor]。我可以渲染一个[UIColor purple]吗?如何实施?感谢。

6 个答案:

答案 0 :(得分:29)

不雅,但它有效:

UIColor* blend( UIColor* c1, UIColor* c2, float alpha )
{
    alpha = MIN( 1.f, MAX( 0.f, alpha ) );
    float beta = 1.f - alpha;
    CGFloat r1, g1, b1, a1, r2, g2, b2, a2;
    [c1 getRed:&r1 green:&g1 blue:&b1 alpha:&a1];
    [c2 getRed:&r2 green:&g2 blue:&b2 alpha:&a2];
    CGFloat r = r1 * beta + r2 * alpha;
    CGFloat g = g1 * beta + g2 * alpha;
    CGFloat b = b1 * beta + b2 * alpha;
    return [UIColor colorWithRed:r green:g blue:b alpha:1.f];
}

更优雅:

<强>的UIColor + Extensions.h:

@interface UIColor (Extensions)

- (UIColor*)blendWithColor:(UIColor*)color2 alpha:(CGFloat)alpha2;

@end

<强>的UIColor + Extensions.m:

@implementation UIColor (Extensions)

- (UIColor*)blendWithColor:(UIColor*)color2 alpha:(CGFloat)alpha2
{
    alpha2 = MIN( 1.0, MAX( 0.0, alpha2 ) );
    CGFloat beta = 1.0 - alpha2;
    CGFloat r1, g1, b1, a1, r2, g2, b2, a2;
    [self getRed:&r1 green:&g1 blue:&b1 alpha:&a1];
    [color2 getRed:&r2 green:&g2 blue:&b2 alpha:&a2];
    CGFloat red     = r1 * beta + r2 * alpha2;
    CGFloat green   = g1 * beta + g2 * alpha2;
    CGFloat blue    = b1 * beta + b2 * alpha2;
    CGFloat alpha   = a1 * beta + a2 * alpha2;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

@end

Swift 3/4:

extension UIColor
{
func mixin(infusion:UIColor, alpha:CGFloat) -> UIColor {
    let alpha2 = min(1.0, max(0, alpha))
    let beta = 1.0 - alpha2

    var r1:CGFloat = 0, r2:CGFloat = 0
    var g1:CGFloat = 0, g2:CGFloat = 0
    var b1:CGFloat = 0, b2:CGFloat = 0
    var a1:CGFloat = 0, a2:CGFloat = 0
    if getRed(&r1, green: &g1, blue: &b1, alpha: &a1) &&
        infusion.getRed(&r2, green: &g2, blue: &b2, alpha: &a2)
    {
        let red     = r1 * beta + r2 * alpha2;
        let green   = g1 * beta + g2 * alpha2;
        let blue    = b1 * beta + b2 * alpha2;
        let alpha   = a1 * beta + a2 * alpha2;
        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }
    // epique de las failuree
    return self
}
}

答案 1 :(得分:4)

使用-[UIColor getRed:green:blue:alpha:]向每个输入颜色询问其RGBA组件。平均每个组件的组件,并使用+[UIColor colorWithRed:green:blue:alpha:]创建新颜色。

答案 2 :(得分:1)

Swift5 UIColor 扩展

extension UIColor {
    func add(_ overlay: UIColor) -> UIColor {
        var bgR: CGFloat = 0
        var bgG: CGFloat = 0
        var bgB: CGFloat = 0
        var bgA: CGFloat = 0
        
        var fgR: CGFloat = 0
        var fgG: CGFloat = 0
        var fgB: CGFloat = 0
        var fgA: CGFloat = 0
        
        self.getRed(&bgR, green: &bgG, blue: &bgB, alpha: &bgA)
        overlay.getRed(&fgR, green: &fgG, blue: &fgB, alpha: &fgA)
        
        let r = fgA * fgR + (1 - fgA) * bgR
        let g = fgA * fgG + (1 - fgA) * bgG
        let b = fgA * fgB + (1 - fgA) * bgB
        
        return UIColor(red: r, green: g, blue: b, alpha: 1.0)
    }
    
    static func +(lhs: UIColor, rhs: UIColor) -> UIColor {
        return lhs.add(rhs)
    }
}

用法

let opacity: CGFloat = 0.6
let start = UIColor.red
let end = UIColor.blue
let combined = start.withAlphaComponent(opacity) + end.withAlphaComponent(1-opacity)

答案 3 :(得分:0)

如您在问题中所提到的,您不能直接混合两个UIColors。无论如何,有一个例子可以使用其他方案混合两种颜色。相同的链接如下:

Link Here

答案 4 :(得分:0)

由于现在是2020年,并且我们提供了“深色模式”,因此许多“颜色”不再只是颜色,而是自动适应颜色变化。通过调用UIColor(dynamicProvider:...)创建它们。

您应该检查两种颜色是否都是动态颜色,如果是,则返回动态颜色。混合一种动态颜色和一种非动态颜色实际上没有任何意义,因此在这种情况下,将返回一种非动态颜色。

答案 5 :(得分:-1)

如果你想使用紫色,那就不是什么大问题了。所有UIColor都是rgb颜色。你可以简单地为每种颜色传递RGB值,并且可以获得所需的颜色。使用此链接转换任何颜色的十六进制值要在UIColor中转换的颜色。 http://www.touch-code-magazine.com/web-color-to-uicolor-convertor/

喜欢绿色的#color代码是#008400: [UIColor colorWithRed:0绿色:0.518蓝色:0 alpha:1] / #008400 /