如何将HSV(HSB)转换为0..1范围



我得到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

看起来你的色调范围是0...360,饱和度和亮度是0...100。你只需要将整数转换为两倍,然后除以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

相关内容

  • 没有找到相关文章

最新更新