flo-utils
Version:
31 lines (26 loc) • 990 B
JavaScript
/**
* @name randomColor
* @link https://www.jianshu.com/p/54fc0fce46cc
* @param {string} [type='16#'] (16#, rgba, 'hsla')
* @param {number} [alpha=1] 透明度
*/
function randomColor() {
var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '16#';
var alpha = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
if (type === 'rgba') {
var r = Math.floor(Math.random() * 255);
var g = Math.floor(Math.random() * 255);
var b = Math.floor(Math.random() * 255);
return "rgba(".concat(r, ",").concat(g, ",").concat(b, ",").concat(alpha, ")");
}
if (type === '16#') {
return "#".concat( // eslint-disable-next-line no-bitwise
"00000".concat((Math.random() * 0x1000000 << 0).toString(16)).substr(-6));
}
if (type === 'hsla') {
var colorAngle = Math.floor(Math.random() * 360);
return "hsla(".concat(colorAngle, ",100%,50%,").concat(alpha, ")");
}
return '';
}
export default randomColor;