将 Obj-C RGBtoHSV() 函数转换为 Swift.min = MIN( r, MIN(g, b ));等等 [英] Translating Obj-C RGBtoHSV() function to Swift. min = MIN( r, MIN(g, b )); etc
问题描述
有人可以帮我完成这个功能吗?此功能位于使用过滤器算法检测颜色变体等差异的相机应用程序中.语法对我来说非常困难.我不知道如何处理参数中的指针、最小和最大变量语法、什么是增量等?有人可以帮我翻译吗?非常感激.
Could someone please help me with this function? This function is inside of a camera app that uses a filter algorithm to detect differences in colour variants etc. The syntax is very difficult for me. I don't know how to deal with the pointers in the arguments, the min and max variable syntax, what is delta etc? Could someone please translate for me? Much appreciated.
我还应该用 UIColor 做些什么吗?下面有人提到过.我不知道如何转换.
Also should I be doing something with UIColor? Someone mentioned it below. I have no idea how to convert though.
// r,g,b values are from 0 to 1 // h = [0,360], s = [0,1], v = [0,1]
// if s == 0, then h = -1 (undefined)
void RGBtoHSV( float r, float g, float b, float *h, float *s, float *v ) {
float min, max, delta;
min = MIN( r, MIN(g, b ));
max = MAX( r, MAX(g, b ));
*v = max;
delta = max - min;
if( max != 0 )
*s = delta / max;
else {
// r = g = b = 0
*s = 0;
*h = -1;
return;
}
if( r == max )
*h = ( g - b ) / delta;
else if( g == max )
*h=2+(b-r)/delta;
else
*h=4+(r-g)/delta;
*h *= 60;
if( *h < 0 )
*h += 360;
}
Swift 翻译尝试
// r,g,b values are from 0 to 1 // h = [0,360], s = [0,1], v = [0,1]
// if s == 0, then h = -1 (undefined)
func RGBtoHSV(r:Float, g:Float, b:Float, h:Float, s:Float, v:Float) {
var min:Float = 0.0
var max:Float = 0.0
var delta:Float = 0.0
min = MIN(r, MIN(g, b))
max = MAX(r, MAX(g, b))
var v = max
delta = max - min
if max != 0 {
var s = delta / max
}
else{
// r = g = b = 0
var s = 0
var h = -1
return
}
if r == max {
var h = (g - b) / delta
}
else if (g == max) {
var h = 2 + (b - r ) / delta
}
else{
var h = 4 + (r - g) / delta
var h = 60
}
if (h < 0) {
var h += 360 // how to deal with all of these pointers here in the original Obj-C function above?
}
}
推荐答案
Swift 相当于传递一个指针(变量的地址)是一个输入参数":
The Swift equivalent of passing a pointer (the address of a variable) is an "inout parameter":
func RGBtoHSV(r : Float, g : Float, b : Float, inout h : Float, inout s : Float, inout v : Float) {
let rgbMin = min(r, g, b)
let rgbMax = max(r, g, b)
let delta = rgbMax - rgbMin
v = rgbMax
s = delta/rgbMax
h = Float(0.0) // Replace by your actual calculation
}
这将被称为
let r : Float = 0.3
let g : Float = 0.5
let b : Float = 0.7
var h : Float = 0.0
var s : Float = 0.0
var v : Float = 0.0
RGBtoHSV(r, g, b, &h, &s, &v)
println([h, s, v])
但是在 Swift 中有一种更好的方法可以返回多个值:你可以返回一个元组:
But in Swift there is a better way to return multiple values: You can return a tuple:
func RGBtoHSV(r : Float, g : Float, b : Float) -> (h : Float, s : Float, v : Float) {
let rgbMin = min(r, g, b)
let rgbMax = max(r, g, b)
let delta = rgbMax - rgbMin
let v = rgbMax
let s = delta/rgbMax
let h = Float(0.0) // Replace by your actual calculation
return (h, s, v)
}
这将被称为
let r : Float = 0.3
let g : Float = 0.5
let b : Float = 0.7
let (h, s, v) = RGBtoHSV(r, g, b)
println([h, s, v])
注意,在 iOS 上你可以简单地使用 UIColor
类来转换RGB 和 HSV (== HSB) 之间:
Note that on iOS you can simply use the UIColor
class to convert
between RGB and HSV (== HSB):
func RGBtoHSV(r : CGFloat, g : CGFloat, b : CGFloat) -> (h : CGFloat, s : CGFloat, v : CGFloat) {
var h : CGFloat = 0.0
var s : CGFloat = 0.0
var v : CGFloat = 0.0
let col = UIColor(red: r, green: g, blue: b, alpha: 1.0)
col.getHue(&h, saturation: &s, brightness: &v, alpha: nil)
return (h, s, v)
}
(或使用来自在 Swift 中将 UIColor 转换为十六进制(网页颜色)的最佳/最短方法是什么?)
这篇关于将 Obj-C RGBtoHSV() 函数转换为 Swift.min = MIN( r, MIN(g, b ));等等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!