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