我有 colorA[UIColor blueColor]
和 colorB [UIColor redColor]
。我可以渲染一个[UIColor purple]
吗?如何实施?谢谢。
6 回答
不优雅,但它有效:
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
斯威夫特 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
}
}
使用 . 询问每种输入颜色的 RGBA 分量-[UIColor getRed:green:blue:alpha:]
。平均每个组件,并使用创建新颜色+[UIColor colorWithRed:green:blue:alpha:]
。
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)
由于现在是 2020 年,并且我们有“深色模式”可用,许多“颜色”不再只是颜色,而是自动适应颜色变化。它们是通过调用 UIColor(dynamicProvider: ...) 创建的。
您应该检查两种颜色是否都是动态颜色,如果是,则返回动态颜色。混合一种动态颜色和一种非动态颜色并没有真正意义,因此在这种情况下返回非动态颜色。
正如您在问题中提到的那样,您不能直接混合两种 UIColors。无论如何,有一个使用其他方案混合两种颜色的示例。相同的链接如下:
如果你想使用紫色,那没什么大不了的。所有的 UIColor 都是 rgb 颜色。你可以简单地传递每种颜色的 RGB 值,并可以获得所需的颜色。使用此链接转换任何颜色的十六进制值以进行转换在 UIColor 中。 http://www.touch-code-magazine.com/web-color-to-uicolor-convertor/
比如绿色的#color 代码是#008400: [UIColor colorWithRed:0 green:0.518 blue:0 alpha:1] / #008400 /