0

我得到 HSB 为 29、90、100。如何将其转换为 0...1 范围?UIColor 只在这个范围内初始化,所以我有一个问题。

let red: CGFloat = 1
let green: CGFloat = 0.5372
let blue: CGFloat = 0.0941


let hueOut = 29
let satOut = 90
let brightnessOut = 100


let color = UIColor(red: red, green: green, blue: blue, alpha: 1.0)

///r 1,0 g 0,537 b 0,094 a 1,0

let color2 = UIColor(hue: hueOut, saturation: satOut, brightness: brightnessOut, alpha: 1.0)

///r -9,0 g 0,0 b -3,0 a 1,0
4

1 回答 1

0

看起来你的色调范围是0...360,你的饱和度和亮度是0...100。您只需要将整数转换为 double 并除以 360 或 100:

let color2 = UIColor(hue: Double(hueOut)/360, saturation: Double(satOut)/100, brightness: Double(brightnessOut)/100, alpha: 1.0)

这将导致r 1.0 g 0.535 b 0.1 a 1.0

于 2022-01-11T17:26:16.840 回答