0

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.

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 translation attempt

// 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?
        }
    }
Edward
  • 241
  • 5
  • 15

1 Answers1

4

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
}

This would be called as

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])

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)
}

And this would be called as

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])

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)
}

(or use the various extension/convenience methods from What is the best/shortest way to convert a UIColor to hex (web color) in Swift?)

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • @Edward: `RGB -> UIColor -> HSB` would be a good alternative, as Leonardo and I suggested in the comments. I don't know how fast it is, you'll have to test it yourself. Note that HSB is the same as HSV, and the A component can simply be set to 1.0. – I posted this mainly to address your question about "pointer variables". – Martin R Apr 22 '15 at 21:20
  • Ok I am using Martin's last block example with UIColor but I have to change the calculation a little bit. `rgbMax` and `delta` no longer exist so how would I change it up? Screenshot here: i.imgur.com/m5epGpo.png Bbtw, What's the advantage of using UIColor? – Edward Apr 23 '15 at 02:45
  • Hi. Ok so it seems my placement of the function and method call is correct now but something in my syntax is giving me `cannot invoke 'RGBtoHSV' with an argument list of type '(Float,Float,Float)'` http://i.imgur.com/sAckG6h.png – Edward Apr 25 '15 at 04:15