gpt4 book ai didi

Swift 在色轮上生成随机颜色

转载 作者:行者123 更新时间:2023-11-28 16:07:41 25 4
gpt4 key购买 nike

我正在使用 SwiftHSVColorPicker框架,需要在色轮上生成随机颜色。我目前的工作方式有效,但亮度不佳。这是我的代码

func generateRandomColor() -> UIColor {
let lowerx : UInt32 = UInt32(0.0)
let upperx : UInt32 = 707
let randomNumberx = arc4random_uniform(upperx - lowerx) + lowerx
let lowery : UInt32 = UInt32(0.0)
let uppery : UInt32 = 707
let randomNumbery = arc4random_uniform(upperx - lowerx) + lowerx
let c = Colorwheel.colorWheel.hueSaturationAtPoint(CGPoint(x: Double(randomNumberx), y: Double(randomNumbery)))
let brightness = 1.0
return UIColor(hue: c.hue, saturation: c.saturation, brightness: CGFloat(brightness), alpha: 1.0)
}

最佳答案

你为什么不使用类似的东西

func getRandomColor() -> UIColor{

let randomRed:CGFloat = CGFloat(arc4random()) / CGFloat(UInt32.max)
let randomGreen:CGFloat = CGFloat(arc4random()) / CGFloat(UInt32.max)
let randomBlue:CGFloat = CGFloat(arc4random()) / CGFloat(UInt32.max)

return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)

}

编辑:

试试这个,在这个hue中,brightness也有

func generateRandomColor() -> UIColor {
let hue : CGFloat = CGFloat(arc4random() % 256) / 256 // use 256 to get full range from 0.0 to 1.0
let saturation : CGFloat = CGFloat(arc4random() % 128) / 256 + 0.5 // from 0.5 to 1.0 to stay away from white
let brightness : CGFloat = CGFloat(arc4random() % 128) / 256 + 0.5 // from 0.5 to 1.0 to stay away from black

return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1)
}

SwiftHSVColorPicker 结果

enter image description here

关于Swift 在色轮上生成随机颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40029397/

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