Search code examples
objective-cswiftrgbuicolorhsv

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.

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

Solution

  • 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?)