Skip to content

Instantly share code, notes, and snippets.

@ciamac-da
Last active June 24, 2021 14:58
Show Gist options
  • Save ciamac-da/312bcac5b8dff18fc2282cdf29740baa to your computer and use it in GitHub Desktop.
Save ciamac-da/312bcac5b8dff18fc2282cdf29740baa to your computer and use it in GitHub Desktop.
This is how to to generate a random color HEX code
const decToHex = (number) => {
return number.toString(16).padStart(2, "0")
}
const getRandomNumber = (min, max) => {
min = Math.ceil(min)
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const generateRandomColor = () => {
let hexCode = '#';
for (let i = 0; i < 3; i++) {
hexCode += decToHex(getRandomNumber(0, 255));
}
return console.log(hexCode);
}
generateRandomColor()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment