A somewhat painful triple-nested ternary operator

后端 未结 9 1566
北恋
北恋 2020-12-30 20:12

I went looking through Raphael.js\'s source code to find out how he converted RGB values to HSB. I found out the function he did it in and I was in the process

9条回答
  •  时光取名叫无心
    2020-12-30 20:31

    Yes, it's right (apart from capitalisation differences). Yet, it may be cleaner written without any parentheses, readable as elseif:

    if (C == 0)
        h = null;
    else if (V == r)
        h = (g - b) / C;
    else if (V == g)
        h = (b - r) / C + 2;
    else
        h = (r - g) / C + 4;
    

提交回复
热议问题