How we can create an RGB slider using JavaScript and HTML range input? Solution: See this JavaScript RGB Slider With HTML Range Input, Get HEX Value.
Previously I have shared a random color generator program that changes color randomly on click, but this is an RGB Slider. Basically, the RGB is color combination R stands for red, G stands for green, and the B stands for blue. And together these 3 colors make another color by increasing and decreasing their values.
Today you will learn to create a program for creating or get HEX value of colors. Basically, there are 3 range sliders for RGB values: first for R, second for G, and third for B. And we can generate a color by sliding them because after together merging these colors can create any color. When you will slide the range then the background color will also change according to slide values. Also, you can see the generated color’s HEX value downside of the sliders.
So, Today I am sharing JavaScript RGB Slider With HTML Range Input. There I have used HTML inputs and some other attribute to creating the range slider and labels, and functions are based on pure JavaScript. I did not use any library or framework for creating this program, if we use one of them then work will very easy and we can save some time.
If you are thinking now how this RGB slider actually is, then see the preview given below.
Preview Of Get Color’s HEX Value Program
See this video to getting an idea of how this program looks like.
Now you can see this visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
- Bootstrap Responsive Menu Bar
- Show & Hide Password Input
- CSS jQuery Budget Slider
- JavaScript Dual Sliding Panels
JavaScript RGB Slider With HTML Range Source Code
Before sharing source code, talk a little bit about it. First I have created 3 fields for 3 different sliders using HTML <fieldset> tag. Inside each fieldset tag I have placed an Input, an Output, and a label tag. The input tag is for range slider with <input type="range"> and the output tag is for show sliders value, and label for showing R, G, B.
Now using CSS I have placed all the elements in the right place, as you can see in the preview. With CSS I have managed the position, font-style, color, etc basic things. And also styled the HEX field for seeing properly values in any background color. There CSS has very less part because functions are powered by JS.
JavaScript handling here the main feature of the program. JS fetched the inputs and outputs values using document.querySelector command. And changing the values according to slides value using addEventListener command (info). For creating the hex value I have added all slides values with after converting the values using some commands.
Left all other things you will understand after getting the codes, I can’t explain all in writing. For creating this program you have to create 3 files. First for HTML, second for CSS, and the third for JavaScript. Follow the steps to creating this without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<!DOCTYPE html> <!-- Code By Webdevtrick ( https://webdevtrick.com ) --> <html lang="en" > <head> <meta charset="UTF-8"> <title>RGB Color Slider | Webdevtrick.com</title> <link rel="stylesheet" href="style.css"> </head> <body> <fieldset> <label for="r">R</label> <input type="range" min="0" max="255" id="r" step="1" value="0"> <output for="r" id="outputR">0</output> </fieldset> <fieldset> <label for="g">G</label> <input type="range" min="0" max="255" id="g" step="1" value="0"> <output for="g" id="outputG">0</output> </fieldset> <fieldset> <label for="b">B</label> <input type="range" min="0" max="255" id="b" step="1" value="0"> <output for="b" id="outputB">0</output> </fieldset> <output id="hexVal">#000000</output> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ @import url(https://fonts.googleapis.com/css?family=Roboto:700); body{ background-color: rgb(0,0,0); text-align: center; padding: 40px 0 0 0; margin-top: 15%; } fieldset{ border: none; } output{ display: inline-block; min-width: 2.5em; } label, output{ padding: 2px 9px; border-radius: 3px; font-family: 'Roboto', sans-serif; color: #000; font-size: 1.1em; } input[type=range] { width: 200px; } label[for=r], output[for=r]{ background-color: #f00; } label[for=g], output[for=g]{ background-color: #0f0; } label[for=b], output[for=b]{ background-color: #00f; } #hexVal{ margin-top: 25px; min-width: 4.5em; font-size: 3em; background: rgba(255,255,255,.3); } |
function.js
The final step, create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
// Code By Webdevtrick ( https://webdevtrick.com ) var body = document.body, r = document.querySelector('#r'), g = document.querySelector('#g'), b = document.querySelector('#b'), outputR = document.querySelector('#outputR'), outputG = document.querySelector('#outputG'), b_out = document.querySelector('#b_out'), hexVal_out = document.querySelector('#hexVal'); function setColor(){ var r_hexVal = parseInt(r.value, 10).toString(16), g_hexVal = parseInt(g.value, 10).toString(16), b_hexVal = parseInt(b.value, 10).toString(16), hexVal = "#" + pad(r_hexVal) + pad(g_hexVal) + pad(b_hexVal); body.style.backgroundColor = hexVal; hexVal_out.value = hexVal; } function pad(n){ return (n.length<2) ? "0"+n : n; } r.addEventListener('change', function() { setColor(); outputR.value = r.value; }, false); r.addEventListener('input', function() { setColor(); outputR.value = r.value; }, false); g.addEventListener('change', function() { setColor(); outputG.value = g.value; }, false); g.addEventListener('input', function() { setColor(); outputG.value = g.value; }, false); b.addEventListener('change', function() { setColor(); b_out.value = b.value; }, false); b.addEventListener('input', function() { setColor(); b_out.value = b.value; }, false); |
That’s It. Now you have successfully created JavaScript RGB Slider With HTML Range Input, Get HEX Value Program. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.
Nice
Great
I found a little bug in there – Your code contained
outputR = document.querySelector(‘#outputR’),
outputG = document.querySelector(‘#outputG’),
b_out = document.querySelector(‘#b_out’),
But the problem is, that it don’t show a value for blue, so i changed the .js code and here is what it should look like
var body = document.body,
r = document.querySelector(‘#r’),
g = document.querySelector(‘#g’),
b = document.querySelector(‘#b’),
outputR = document.querySelector(‘#outputR’),
outputG = document.querySelector(‘#outputG’),
outputB = document.querySelector(‘#outputB’),
hexVal_out = document.querySelector(‘#hexVal’);
function setColor(){
var r_hexVal = parseInt(r.value, 10).toString(16),
g_hexVal = parseInt(g.value, 10).toString(16),
b_hexVal = parseInt(b.value, 10).toString(16),
hexVal = “#” + pad(r_hexVal) + pad(g_hexVal) + pad(b_hexVal);
body.style.backgroundColor = hexVal;
hexVal_out.value = hexVal;
}
function pad(n){
return (n.length<2) ? "0"+n : n;
}
r.addEventListener('change', function() {
setColor();
outputR.value = r.value;
}, false);
r.addEventListener('input', function() {
setColor();
outputR.value = r.value;
}, false);
g.addEventListener('change', function() {
setColor();
outputG.value = g.value;
}, false);
g.addEventListener('input', function() {
setColor();
outputG.value = g.value;
}, false);
b.addEventListener('change', function() {
setColor();
outputB.value = b.value;
}, false);
b.addEventListener('input', function() {
setColor();
outputB.value = b.value;
}, false);
But anyways a nice little program 🙂
if I want to add reset button in this code whose functionality is to reset all the rgb value and bring back the range slicer to zero how can I do that?
VERY GOOD 👍