In this article, we will create a RGB color generator using HTML, CSS, and JavaScript. Using RGB color generator, we can construct all the colors from the combination of Red, Green, Blue colors. Each color is represented by the range of decimal numbers from 0 to 255 (256 levels for each color). So, the total number of available colors is 256 x 256 x 256, or 16,777,216 possible colors.
Approach:
- Create an input type range slider for each color.
- Set the min and max value of the slider as 0 and 255 respectively.
- Get the value of each color and store it in the three variables.
- Use rgb() function to generate the color by giving value of three colors as parameters.
index.html
| <!DOCTYPE html> <html>  Â<head>     <metacharset="utf-8">     <title>RGB Colour Generator</title>  Â    <linkrel="stylesheet"type="text/css"        href="style.css"> </head>  Â<body>     <h1>RGB Colour Generator</h1>      Â    <divclass="main">         Result:<inputtype="text"id="box"            value="rgb(255,255,255)">  Â        <!--  Range slider for red colour -->        Red:<inputtype="range"id="red"            value="255"min="0"max="255">  Â        <!-- Range slider for green colour -->        Green:<inputtype="range"id="green"            value="255"min="0"max="255">  Â        <!-- Range slider for blue colour -->        Blue:<inputtype="range"id="blue"            value="255"min="0"max="255">     </div>  Â    <scriptsrc="script.js"></script> </body>  Â</html>  | 
style.css
| body {     margin: 0;     padding: 0;     display: grid;     place-items: center;     height: 100vh; }  Â.main {     height: 400px;     width: 250px;     background: #333;     border-radius: 10px;     display: grid;     place-items: center;     color: #fff;     font-family: verdana; }  Â#box {     height: 40px;     width: 80%;     border: none;     outline: none;     outline: none;     border-radius: 50px;     text-align: center; }  Â Â/* CSS property for slider */input[type="range"] {     -webkit-appearance: none;     height: 10px;     width: 80%;     border-radius: 50px;     outline: none; }  Â/* CSS property for slider thumb */input[type="range"]::-webkit-slider-thumb {     -webkit-appearance: none;     height: 25px;     width: 25px;     background: #fff;     border-radius: 50%;     cursor: pointer; }  Â.main #red{     background: linear-gradient(90deg, #000, red); }  Â.main #green{     background: linear-gradient(90deg, #000, green); }  Â.main #blue{     background: linear-gradient(90deg, #000, blue); }  | 
script.js
| functionmyColour() {  Â    // Get the value of red color     varred = document.getElementById('red').value;  Â    // Get the value of green color     vargreen = document.getElementById('green').value;  Â    // Get the value of blue color     varblue = document.getElementById('blue').value;  Â    // rgb() function is used to create the color     // from red, green and blue values     varcolour = 'rgb('+ red + ','+ green + ','+ blue + ')';  Â    // Change background colour with the      // color obtained by rgb function     document.body.style.backgroundColor = colour;  Â    // Set the obtained rgb() colour code in the     // input text field having id=box       document.getElementById('box').value = colour;  Â}  Â// On changing red range slider myColour() // function is called   document.getElementById('red')     .addEventListener('input', myColour);  Â// On changing green range slider myColour() // function is called document.getElementById('green')     .addEventListener('input', myColour);  Â// On changing blue range slider myColour() // function is called document.getElementById('blue')     .addEventListener('input', myColour);  | 
Output:


 
                                    








