Skip to main content

Arcadis is Hiring!!!

 ARCADIS IS HIRING!!! Company : Arcadia Position : Software Engineer Experience : Bs/Ms Degree : BS in CS Apply Link: Apply Here

Random Colour Generator Using JavaScript

 Randomized color Using JavaScript 

Did you ever feel tired of writing long random colors for different <div> or <span> just to test something simple?

So here is a simple solution. The following snippet generates a random color in hexadecimal format.

    var randomColor = '#'+Math.floor(Math.random()*16777215).toString(16); 
    //generates a random color -> #56eec7

That's it!🥳 You can place this in a function and call the function everytime you need a random color.


function generateRandomColor() { var randomColor =
 '#'+Math.floor(Math.random()*16777215).toString(16); return randomColor; //random color will be freshly served } document.body.style.backgroundColor = generateRandomColor() // -> #e1ac94 someDiv.style.color = generateRandomColor() // -> #34c7aa


1.Why the number 16777215?

  • Well this needs a little bit of Math. We all know that the colors range from #000000(pitch black) to #ffffff(pure white).
  • The number of colors that exist from black to white as per rgb values are 16777216.
  • This can be calculated simply by using permutation & combination formula [result = m to the power of n => 16 to power of 6 => 16777216]
  • However our ultimate goal is to convert the number into hexadecimal format and 16777216 converts to 1000000 and 16777215 converts to ffffff. Hence we proceed with 167777215 as the highest number for hexadecimal conversion.

2.Randomness

  • As we need some randomness in our output we are multiplying our magic number with Math.random() which returns floating number in range from inclusive of 0 to exclusive of 1.

  • 3.Hexadecimal conversion

    • Now we are in the endgame, the last part of the code. To convert a number to hexadecimal format string , we have a beautiful method toString() which accepts the number that tells to which format it has to convert.
    • As we are converting to string of hexa-decimal format and hence we pass 16 as the argument as follows.

Lets have a look Code Now!!

HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Page Title</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <link rel='stylesheet' type='text/css' media='screen' href='main.css'>
    <script src='main.js'></script>
</head>
<body>
    <main>
        <div id="texto">#ffffff</div>
        <button onclick="gerarCorAleatorial()" class="btn">
            CLICK HERE
            </button>
            </main>
</body>
</html>

CSS:

*{
    margin0;
    padding0;
    box-sizingborder-box;
    font-family'Poppins'sans-serif;

}
body{
    height100vh;
    displayflex;
    justify-contentcenter;
    align-itemscenter;
}
main{
    width350px;
    height350px;
    background#00142c;
    colorwhite;
    displayflex;
    justify-contentcenter;
    align-itemscenter;
    padding20px;
    flex-directioncolumn;
    font-weight500;   
}
button{
    outlinenone;
    margin-top:  20px;
    width100%;
    height50px;
    backgroundrgb(7219134);
    color#00142c;
    font-weight700;
    cursorpointer;
    transitionall 0.2s;
}
button:hover{
    filterbrightness(0.8);
}



JAVASCRIPT:

function gerarCorAleatorial(){
    var randomColor ='#' + Math.floor (Math.random () * 16777215).toString (16);
    document.body.style.backgroundColor = randomColor;

    var text = document.getElementById('texto');
    text.innerText = randomColor;

OUTPUT:



Comments

Popular posts from this blog

Let's Start with the Internet Marketing | Affiliate Marketing

Digital Marketing is  that the act of promoting and mercantilism merchandise and services by leverage online selling ways like social media selling, search selling, and email marketing. At a high level, digital marketing refers to advertising delivered through digital channels like search engines, websites, social media, email, and mobile apps. Using these online media channels, digital marketing is that the method by which companies endorse goods, services, and brands. Consumers heavily depend upon digital means to research products. as an example, Think with Google marketing insights found that 48% of consumers start their inquiries on search engines, while 33% look to brand websites and 26% search within mobile applications. While modern-day digital marketing is an unlimited system of channels to which marketers simply must onboard their brands, advertising online is way more complex than the channels alone....

Hewlett Packard Enterprise

CMS INTERN JOB   At Hewlett Packard Enterprise, we bring together the brightest minds to create breakthrough technology solutions that advance the way people live and work. What sets us apart? Our people and our relentless dedication to helping our customers make their mark on the world. We are a team of doers, dreamers and visionaries; inspired by our purpose and driven by our strategy. We live by our three values: partner, innovate and act. Our legacy inspires us as we forge ahead, always pushing to discover what’s next. Every day is a new opportunity to advance and grow ourselves, our company and the industry. Some people call it an obsession, we call it a way of life. SOME INFORMATIONS AND BASIC REQUIREMENTS REGARDING THE JOB: Job ID: 1104914 Date Posted: 10/26/2021 Primary Location: Bangalore, Karnataka Other Locations: Bangalore, Karnataka, India,Chennai,TAMILNADU, India Job Category: Graduate Program and Internships Schedule: Full time Shift: No shift premium (India) Respons...