RGB 彩虹函数

这个函数用于彩虹RGB生成 [0-255, 0-255, 0-255]

原理参照这个图片:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

/*reference page http://academe.co.uk/2012/04/arduino-cycling-through-colours-of-the-rainbow/*/

function rainbow(num){
var num = num || 12;
var step = 1 / (num/3);
var colors = [];

for(var i =0; i<num; i++){
var r = channelColor(i*step*2 + 2);
var g = channelColor(i*step*2);
var b = channelColor(i*step*2 + 4);
colors.push([r*255, g*255, b*255]);
}

function channelColor(num) {//0-1 inclement; 1-3 keep top, 3-4 declement, 4-6 keep bot;
/*outPut 0-1*/
while(num >= 6){
num = num-6
}
if(num >=0 && num <1)
return num;
if(num >=1 && num <=3)
return 1;
if(num >3 && num <4)
return 1-(num%1);
if(num >=4 && num <6)
return 0;
}

return colors;
}