HSV转换结果不正确?

时间:2019-03-06 14:13:53

标签: ios swift uicolor

我有这段代码可以将RGB转换为HSV

  func rgbToHSV (_ r:Double, _ g:Double, _ b:Double) -> (Double, Double, Double) {

    let cor01 : (Double) -> CGFloat = { valor in
      return CGFloat(valor / 255)
    }

    let corRGB = UIColor(red: cor01(r),
                         green: cor01(g),
                         blue: cor01(b),
                         alpha: 1.0)

    var hue         : CGFloat = CGFloat.infinity
    var saturation  : CGFloat = CGFloat.infinity
    var brightness  : CGFloat = CGFloat.infinity
    var alpha       : CGFloat = CGFloat.infinity

    _ = corRGB.getHue(&hue,
                      saturation:&saturation,
                      brightness:&brightness,
                      alpha:&alpha)

    return (Double(hue), Double(saturation), Double(brightness))
  }

我将RGB =(168,131,126)转换为HSV并得到

HUE = 0.019841269841269771
SAT = 0.24999999999999994
BRI = 0.6588235294117647

我使用this sitethis site的在线转换,并且都给我7度的色调。

这是怎么回事?

1 个答案:

答案 0 :(得分:2)

其原因是0.01984126984126977的值是整体的一部分,其中整体为360度。您可以乘以360并得到7.1428571428571175的值。

let result = rgbToHSV(168, 131, 126)
print(result)
print(result.0 * 360)

输出:

(0.01984126984126977, 0.24999999999999994, 0.6588235294117647)
7.1428571428571175