s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
the expression ? value1 : value2
operator evaluates the expression
and, if it's true, returns value1
, else returns value2
.
so in that particular case, it will evaluate l > 0.5
, if that's true it'll set s
to d / (2 - max - min)
, else it'll set s
to d / (max + min)
EDIT in response to comment:
I don't really know a lot of Javascript at all, but as for the switch
statement, it seems to be using variable cases. That's not allowed in Objective-C (or C, for that matter). But it seems to be trying to find which of the r
, g
or b
variables is equal to the max
variable...
In Objective-C (or C, or C++) a switch
statement can only use constant case values, like this:
switch (myVariable)
{
case 0:
//do something if myVariable == 0
break;
case 1:
//do something if myVariable == 1
break;
case 2:
//do something if myVariable == 2
break;
}
I'm gonna take a guess here and say that in javascript with variable cases, it would probably be equivalent to something like
if (max == r)
h = (g - b) / d + (g < b ? 6 : 0);
else if (max == g)
h = (b - r) / d + 2;
else if (max == b)
h = (r - g) / d + 4;
if anyone who knows more about javascript could confirm that for us, it would be great.