渐变色值

rgb to hex

1
2
3
4
function rgbToHex(r, g, b) {
var hex = ((r<<16) | (g<<8) | b).toString(16);
return "#" + new Array(Math.abs(hex.length-7)).join("0") + hex;
}


hex to rgb

1
2
3
4
5
6
7
function hexToRgb(hex) {
var rgb = [];
for(var i=1; i<7; i+=2){
rgb.push(parseInt("0x" + hex.slice(i,i+2)));
}
return rgb;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let ratio = Math.random();
let i = 100;
let colorList = [];

function getGradientColor(ratio) {
let r = Math.cos(ratio) * 127 + 128;
let g = Math.cos(ratio + 2 * Math.PI / 3) * 127 + 128;
let b = Math.cos(ratio + 4 * Math.PI / 3) * 127 + 128;
return '#' + (r << 16 | g << 8 | b).toString(16);
}

while (i--) {
colorList.push(getGradientColor(ratio));
ratio += Math.PI * 2 / 50;
}