Different possible methods for generating a colour randomly in JS

In this post, I will be summarising several possible methods for generating a colour randomly in JavaScript.
So, Save this article !
As you might be aware of how colours are represented, i.e, Hexadecimal code with a ‘#’ prefix –> #RRGGBB
The code f…


This content originally appeared on DEV Community and was authored by Prakhar Tandon

In this post, I will be summarising several possible methods for generating a colour randomly in JavaScript.
So, Save this article !
As you might be aware of how colours are represented, i.e, Hexadecimal code with a '#' prefix --> #RRGGBB
The code for Black --> #000000
and for white --> #ffffff
Hence higher the values, more the colour will be lighter and vice-versa.
Here's the different methods:

  • Method 1 In this approach, simply take a string of all possible hexadecimal characters, then choose from it randomly and concatenate them to form a 6 digit hex code.
const s = "0123456789ABCDEF";
function getRandomColor() 
{
    let col = "#";
    let temp =0;
    for(let i=0;i<6;++i)
    {
       temp = Math.floor( Math.random() * s.length ); // generates number between 0-15
       col = col + s[temp];
    }
return col;
}

for generating lighter/Darker colours only, we can use sLight or sDark respectively.

const sLight="789ABCDEF";
const sDark="01234567";
  • Method 2 Similar to the first one but here instead of predefined string, we can use toString(16) to convert to HexaDecimal.
function getRandomColor(){
    let color = "#";
    for(let i=0;i<6;++i)
        color += (Math.floor( Math.random() * 16 ).toString(16) );
    return color;
}
  • Method 3 We can use the following ES6 approach :
const getRandomHex = from => to => () =>
    Math.floor(Math.random() * (to - from) + from).toString(16);

const getRangedRandomColor = from => to => () =>
    `#${[...Array(6)].map(getRandomHex(from)(to)).join("")}`;

const getRandomColor = getRangedRandomColor(0x0)(0xf);
const getRandomColorLight = getRangedRandomColor(0x7)(0xf);
const getRandomColorDark = getRangedRandomColor(0x0)(0x7);

Thanks to @lukeshiru for this one, and you can find the detailed explaination for this in the comments section of my previous article here

  • Method 4
function getRandomColor() {
    function c() {
      var hex = Math.floor(Math.random()*256).toString(16);
      return ("0"+String(hex)).substr(-2); // pad with zero
    }
    return "#"+c()+c()+c();
}

substr(-2) means take last two characters of the string.

  • Method 5 This one is a great one liner for the same I found on StackOverflow.
function getRandomColor() {
    return '#'+(Math.random().toString(16)+'00000').slice(2,8);
}

Well these were some of my picks, if you want to dive in more, you can have a look at this thread on StackOverFlow.

You can save this article for future references and comment down your opinions as well !

You can follow me on:
Twitter
GitHub


This content originally appeared on DEV Community and was authored by Prakhar Tandon


Print Share Comment Cite Upload Translate Updates
APA

Prakhar Tandon | Sciencx (2021-12-18T18:17:43+00:00) Different possible methods for generating a colour randomly in JS. Retrieved from https://www.scien.cx/2021/12/18/different-possible-methods-for-generating-a-colour-randomly-in-js/

MLA
" » Different possible methods for generating a colour randomly in JS." Prakhar Tandon | Sciencx - Saturday December 18, 2021, https://www.scien.cx/2021/12/18/different-possible-methods-for-generating-a-colour-randomly-in-js/
HARVARD
Prakhar Tandon | Sciencx Saturday December 18, 2021 » Different possible methods for generating a colour randomly in JS., viewed ,<https://www.scien.cx/2021/12/18/different-possible-methods-for-generating-a-colour-randomly-in-js/>
VANCOUVER
Prakhar Tandon | Sciencx - » Different possible methods for generating a colour randomly in JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/18/different-possible-methods-for-generating-a-colour-randomly-in-js/
CHICAGO
" » Different possible methods for generating a colour randomly in JS." Prakhar Tandon | Sciencx - Accessed . https://www.scien.cx/2021/12/18/different-possible-methods-for-generating-a-colour-randomly-in-js/
IEEE
" » Different possible methods for generating a colour randomly in JS." Prakhar Tandon | Sciencx [Online]. Available: https://www.scien.cx/2021/12/18/different-possible-methods-for-generating-a-colour-randomly-in-js/. [Accessed: ]
rf:citation
» Different possible methods for generating a colour randomly in JS | Prakhar Tandon | Sciencx | https://www.scien.cx/2021/12/18/different-possible-methods-for-generating-a-colour-randomly-in-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.