How to generate a random color in JavaScript


In this post, let's see how to generate a random color using Javascript

Installation

To generate random colors we use a package called randomcolor
Run the below command to install the package.

npm i randomcolor

or

yarn add randomcolor

Basic usage

let randomColor = require('randomcolor'); 
let color = randomColor(); 

The color variable will contain a random hex code.

Options

You can customize the method by adding more parameters.

Some are,

luminosity – Controls the luminosity of the generated color.

count – Specifies the number of colors to generate.

hue – Controls the hue of the generated color.

Examples

randomColor({
   count: 5,
   hue: 'red'
});