ios - 如何在 UIColor 中获得随机的暖色/冷色?
问题描述
我想randomly
选择2种颜色。但它必须是随机cold
颜色和随机warm
颜色。
我正在使用它extension
来获取随机颜色:
extension UIColor {
static var random: UIColor {
return UIColor(red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1),
alpha: 1.0)
}
}
根据这篇文章,我们可以假设:
if (B > R) {
color = cool
} else {
color = warm
}
如何修改我的随机扩展以选择随机暖色或随机冷色。
有什么建议吗?
解决方案
如果你首先生成你的数字,你可以blue
在你的计算中使用这个值red
......
extension UIColor {
static var warm: UIColor {
let red: CGFloat = .random(in: 0...1)
let blue: CGFloat = .random(in: 0..<red)
return UIColor(red: red, green: .random(in: 0...1), blue: blue, alpha: 1)
}
static var cool: UIColor {
let blue: CGFloat = .random(in: 0...1)
let red: CGFloat = .random(in: 0..<blue)
return UIColor(red: red, green: .random(in: 0...1), blue: blue, alpha: 1)
}
}
这将满足您对冷是红色小于蓝色和暖色是蓝色小于红色的要求。
推荐阅读
- android - 如何在 android studio 中调试您的 Release APK 或任何 APK
- android - 分发具有不同包名的 Android TV 版本
- javascript - K6在http post请求中通过没有密码的CA证书
- php - PHP脚本发送视频后返回原始页面
- java - org.springframework.http.converter.HttpMessageNotReadableException 发送 GET 请求时
- r - 从多个矩阵计算均值和出现次数
- loops - 使用此条件统一崩溃错误,即使有限制
- c# - Finding perpendicular and parallel points
- android - 如何在我的 Android Studio 项目中添加异步任务?
- php - Paypal Customer Dispute API - 提供证据总是返回 MISSING_OR_INVALID_REQUEST_BODY