Open In App

How to create RGB color generator using HTML CSS and JavaScript ?

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
2 Likes
Like
Report

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:

  1. Create an input type range slider for each color.
  2. Set the min and max value of the slider as 0 and 255 respectively.
  3. Get the value of each color and store it in the three variables.
  4. Use rgb() function to generate the color by giving value of three colors as parameters.
index.html
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>RGB Colour Generator</title>

    <link rel="stylesheet" type="text/css" 
        href="style.css">
</head>

<body>
    <h1>RGB Colour Generator</h1>
    
    <div class="main">
        Result:<input type="text" id="box"
            value="rgb(255,255,255)">

        <!--  Range slider for red colour -->
        Red:<input type="range" id="red" 
            value="255" min="0" max="255">

        <!-- Range slider for green colour -->
        Green:<input type="range" id="green" 
            value="255" min="0" max="255">

        <!-- Range slider for blue colour -->
        Blue:<input type="range" id="blue" 
            value="255" min="0" max="255">
    </div>

    <script src="script.js"></script>
</body>

</html>
style.css script.js

Output:


How to create RGB color generator using HTML CSS and JavaScript ?
Next Article

Similar Reads