I am trying to decode this bit of code that I found in Raphael.js source (it converts from HSL color to RGB color, this is only part of the function):
var R, G, B, X, C;
h = (h % 360) / 60;
C = 2 * s * (l < .5 ? l : 1 - l);
X = C * (1 - abs(h % 2 - 1));
R = G = B = l - C / 2;
h = ~~h;
R += [C, X, 0, 0, X, C][h];
G += [X, C, C, X, 0, 0][h];
B += [0, 0, X, C, C, X][h];
Now I know what it does h = ~~h(basically the floor is a room with a few key differences), but I can’t understand how much I can understand it:
R += [C, X, 0, 0, X, C][h];
Why does it create an array and then reference [h]it? Does it find the value that the variable his equal outside the set of values? (but why would he do this if he already knows the meaning h?) I have never seen anything like it, and if in this case I consider it unusually smart.