gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:30:05 25 4
gpt4 key购买 nike

我有 colorA,它是 [UIColor blueColor],还有 colorB,它是 [UIColor redColor]。我可以渲染 [UIColor purple] 吗?如何实现?谢谢。

最佳答案

不优雅,但它有效:

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
}
}

关于objective-c - 我可以将两个 UIColor 混合在一起吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13132743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com