开发者

javascript to objective-c conversion

开发者 https://www.devze.com 2023-01-30 04:32 出处:网络
I know nothing about javascript and need help int开发者_如何转开发erpreting the following: { var d = max - min;

I know nothing about javascript and need help int开发者_如何转开发erpreting the following:

{
    var d = max - min;
    s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
    switch(max){
      case r: h = (g - b) / d + (g < b ? 6 : 0); break;
      case g: h = (b - r) / d + 2; break;
      case b: h = (r - g) / d + 4; break;
    }
}

Could someone please explain what this means so I can code an equivalent in Objective-C ?

Thank you!

or to make my question even more to the point. could you please translate (to human language) the following line for me ?

s = l > 0.5 ? d / (2 - max - min) : d / (max + min);


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.


This is actually very straightforward, all you have to do is change var to float.


Seems to be a fragment from an algorithm to assign new RGB colors to something. Can't tell without any info about the values max, min. d is probably "distance".

var s is assigned something with a ternary ?: operator, but s is not used again in the block-


I'd say it means that whomever you inherited that code from needs to learn how to name variables properly.

0

精彩评论

暂无评论...
验证码 换一张
取 消