randomizer-pro-js
Version:
A JavaScript package to generate random colors and numbers.
37 lines (32 loc) • 940 B
JavaScript
function getRandomHexaColor() {
const letters = "0123456789ABCDEF";
let color = "#";
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function getRandomRgbColor() {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return `rgb(${r}, ${g}, ${b})`;
}
function getRandomRgbaColor(alpha = 1.0) {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}
function getRandomHslColor() {
const h = Math.floor(Math.random() * 361);
const s = Math.floor(Math.random() * 101);
const l = Math.floor(Math.random() * 101);
return `hsl(${h}, ${s}%, ${l}%)`;
}
module.exports = {
getRandomHexaColor,
getRandomRgbColor,
getRandomHslColor,
getRandomRgbaColor,
};