gpt4 book ai didi

iOS( swift ): representing scaled value as UIColor

转载 作者:行者123 更新时间:2023-11-28 14:31:49 28 4
gpt4 key购买 nike

有没有一种方法可以生成从 redUIColor 表(或者 UIColor 的数组) green 这样变量 var match = 100 将具有相应的颜色 greenvar match = 0 将具有相应的颜色颜色 red 和中间的变量值会随着它们的值接近 100 而变得更绿?

感谢您的帮助。

最佳答案

这是一个有用的 UIColor 扩展,它可以让您根据百分比混合两种颜色。

extension UIColor {
// This function calculates a new color by blending the two colors.
// A percent of 0.0 gives the "self" color
// A percent of 1.0 gives the "to" color
// Any other percent gives an appropriate color in between the two
func blend(to: UIColor, percent: Double) -> UIColor {
var fR : CGFloat = 0.0
var fG : CGFloat = 0.0
var fB : CGFloat = 0.0
var tR : CGFloat = 0.0
var tG : CGFloat = 0.0
var tB : CGFloat = 0.0

getRed(&fR, green: &fG, blue: &fB, alpha: nil)
to.getRed(&tR, green: &tG, blue: &tB, alpha: nil)

let dR = tR - fR
let dG = tG - fG
let dB = tB - fB

let perc = min(1.0, max(0.0, percent))
let rR = fR + dR * CGFloat(perc)
let rG = fG + dG * CGFloat(perc)
let rB = fB + dB * CGFloat(perc)

return UIColor(red: rR, green: rG, blue: rB, alpha: 1.0)
}
}

例子:

let red = UIColor.red.blend(to: .green, percent: 0)
let mix = UIColor.red.blend(to: .green, percent: 0.5)
let green = UIColor.red.blend(to: .green, percent: 1)

关于iOS( swift ): representing scaled value as UIColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51201344/

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