RGB Color Generator Slider App using JavaScript Source Code Free Download

Language

This simple project is called RGB Color Generator Slider App. It is a simple web application that generates an RGB Color when changing the values of Input Sliders. This application was developed using HTML, CSS, and Pure JavaScript. It has a simple user interface using a custom stylesheet and contains simple functionalities. Here, I will be providing the said application source code and the zip file copy for free download.

What is RGB Color?

The RGB stands for Red, Green, and Blue colors. RGB Colors is a combined red, green, and blue color that can result in multiple other colors. A color can be created by combining the said primary colors ranging from 0 - 255 each color.

How does the RGB Color Generator Slider App work?

The RGB Color Generator Slider App is a straightforward application that contains 3 different HTML input ranges that indicate the Red, Green, and Blue colors (primary colors) and each range input can be set between 0-255 value. When changing the slider values, the RGB Color generation will be executed and will preview the color to the provided preview palette and a text field. In addition, users can also copy the generated RGB Color by simply clicking the Generated RGB text Field.

Technologies

Here is the list of Technologies I used to develop this application:

  • MS VS Code Editor
  • HTML
  • CSS
  • Pure JavaScript

Source Code Scripts

Here are the source code scripts that I created that result in a Simple RGB Color Generator Slider App:

HTML

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <meta charset="UTF-8">
  4.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>JavaScript - RGB Generator Slider</title>
  7.     <link rel="preconnect" href="https://fonts.googleapis.com">
  8.     <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9.     <link rel="stylesheet" href="style.css">
  10. </head>
  11.     <div id="block-container">
  12.         <div class="slider-item">
  13.             <label for="redSlider"><b>R</b></label>
  14.             <input type="range" min="0" max="255" value="55" id="redSlider">
  15.         </div>
  16.         <div class="slider-item">
  17.             <label for="greenSlider"><b>G</b></label>
  18.             <input type="range" min="0" max="255" value="112" id="greenSlider">
  19.         </div>
  20.         <div class="slider-item">
  21.             <label for="blueSlider"><b>B</b></label>
  22.             <input type="range" min="0" max="255" value="191" id="blueSlider">
  23.         </div>
  24.         <div id="result-container">
  25.             <div id="color-preview"></div>
  26.             <div id="color-text-preview">rgb(55, 112, 191)</div>
  27.         </div>
  28.     </div>
  29.     <script src="script.js"></script>
  30. </body>
  31. </html>

CSS

  1. @import url('https://fonts.googleapis.com/css2?family=Ubuntu:ital,wght@0,300;0,400;0,500;0,700;1,300;1,400;1,500;1,700&display=swap');
  2.  
  3. html,
  4. body{
  5.     margin:unset;
  6.     padding: unset;
  7.     width: 100%;
  8.     height: 100%;
  9. }
  10.  
  11. * {
  12.     font-family:'Ubuntu', sans-serif;
  13. }
  14.  
  15. body{
  16.     background-color: #03001C;
  17.     display: flex;
  18.     flex-direction: column;
  19.     align-items: center;
  20.     justify-content: center;
  21. }
  22.  
  23. #block-container{
  24.     width: 400px;
  25.     height: 200px;
  26.     background-color: #fff;
  27.     border: 1px solid #8d8d8d;
  28.     box-shadow: 0px 0px 5px #c5c5c5b0;
  29.     padding: 2em 1em;
  30. }
  31. .slider-item{
  32.     display:flex;
  33.     width: 100%;
  34.     align-items: center;
  35.     margin: 1em 0;
  36. }
  37. .slider-item label{
  38.     width:50px;
  39.     text-align: center;
  40.     font-weight: 700;
  41. }
  42. .slider-item input[type="range"]{
  43.     flex-grow: 1;
  44. }
  45. #result-container{
  46.     display: flex;
  47.     align-items: center;
  48.     justify-content: space-evenly;
  49. }
  50.  
  51. #color-preview{
  52.     height: 75px;
  53.     width: 75px;
  54.     border:1px solid black;
  55.     box-shadow: 0px 0px 3px #00000028;
  56.     background-color: rgb(55, 112, 191);
  57. }
  58. #color-text-preview{
  59.     width: 150px;
  60.     padding: .75em 0;
  61.     border-radius: 5px;
  62.     background-color: #205295;
  63.     color: #fff;
  64.     font-weight: 500;
  65.     text-align: center;
  66.     cursor: pointer;
  67. }
  68.  
  69. #redSlider,
  70. #greenSlider,
  71. #blueSlider{
  72.     width: 100%;
  73. }

JavaScript

  1. // Color Preview Container
  2. const previewPallete = document.getElementById('color-preview')
  3. // Generated RGB Text Container
  4. const previewText = document.getElementById('color-text-preview')
  5. // Red Slider
  6. const redSlider = document.getElementById('redSlider')
  7. // Green Slider
  8. const greenSlider = document.getElementById('greenSlider')
  9. // Blue Slider
  10. const blueSlider = document.getElementById('blueSlider')
  11.  
  12.  
  13. // Combining Slider Values to generate the RGB Color
  14. const generateRGB = () =>{
  15.     // Getting the Red slider Value
  16.     var r = redSlider.value
  17.     // Getting the Green slider Value
  18.     var g = greenSlider.value
  19.     // Getting the Blue slider Value
  20.     var b = blueSlider.value
  21.  
  22.     // Combine Slider Values as RGB Color
  23.     var generatedColor = `rgb(${r}, ${g}, ${b})`;
  24.  
  25.     // Set the background color of the preview palette with combined values
  26.     previewPallete.style.backgroundColor = generatedColor
  27.     // Set the RGB text Field with combined values
  28.     previewText.innerText = generatedColor
  29. }
  30.  
  31. // trigger RGB Color Generation when slider values has changed
  32. redSlider.addEventListener('change', generateRGB)
  33. greenSlider.addEventListener('change', generateRGB)
  34. blueSlider.addEventListener('change', generateRGB)
  35.  
  36. // Copy the Combined Slider Values (RGB Color) to clipboard
  37. previewText.addEventListener('click', e=> {
  38.         e.preventDefault()
  39.         // Get the RGB Color
  40.         var _text = previewText.innerText
  41.         // Create a textarea where to temporarily store the string to copy
  42.         var textarea = document.createElement('textarea')
  43.         // set the RGB Color as the value of textarea
  44.         textarea.innerHTML = _text
  45.         // Append the textarea to the document body
  46.         document.body.appendChild(textarea)
  47.         // Select the textarea value
  48.         textarea.select()
  49.         // Execute Copy Commandd
  50.         document.execCommand('copy')
  51.         //Remove the temporary text area
  52.         textarea.remove()
  53.         // Notify user that the text has been copied to clipboard using alert dialog
  54.         alert("RGB Color has been copied to clipboard.")
  55.     }
  56. )

Snapshots

Here are some sample snapshots of the RGB Color Generator Slider App

Sample #1

RGB Color Generator Slider App

Sample #2

RGB Color Generator Slider App

Sample #3

RGB Color Generator Slider App

DEMO VIDEO

I have also provided the complete source code zip file of this RGB Color Generator Slider App on this website and it is free to download. The download button can be found below this article's content. Feel free to download and modify the source code the way you desire.

How to Run?

  1. Download the source code zip file on this website.
  2. Extract the source code zip file.
  3. Locate the index.html file in the source code folder.
  4. Browse the index.html file on your preferred browser

That's it! I hope this RGB Color Generator Slider App using JavaScript Source Code will help you with what you are looking for and will be useful for your current and future web applications.

Explore more on this website for more Tutorials and Free Source Codes.

Enjoy =)

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment