How to Convert Hex to RGB Color Code in JavaScript

How to Convert Hex to RGB Color Code in JavaScript

Introduction

In this tutorial we will create a How to Convert Hex to RGB Color Code in JavaScript. This tutorial purpose is to teach you how to convert the hex code into rgb code. This will tackle all the basic function that will convert color code value. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that need rgb color for your backgrounds. I will give my best to provide you the easiest way of creating this program Convert hex to RGB. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display html forms and button for conversion. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a href="https://sourcecodester.com" class="navbar-brand">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">How to Convert Hex to RGB Color Code</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-8">
  17.                         <div class="form-inline">
  18.                                 <h3>RGB CODE</h3>
  19.                                 <label>R:</label>
  20.                                 <input type="number" id="r" class="form-control" style="width:20%;" min="0" max="255" readonly="readonly"/>
  21.                                 <label>G:</label>
  22.                                 <input type="number" id="g" class="form-control" style="width:20%;" min="0" max="255" readonly="readonly"/>
  23.                                 <label>B:</label>
  24.                                 <input type="number" id="b" class="form-control" style="width:20%;" min="0" max="255" readonly="readonly"/>
  25.                         </div>
  26.                 </div>
  27.                 <div class="col-md-3">
  28.                         <h3>HEX CODE</h3>
  29.                         <input type="text" id="hex" class="form-control" placeholder="#"/>
  30.                         <br />
  31.                         <button type="button" class="btn btn-primary btn-block" onclick="convert()">Convert</button>
  32.                 </div>
  33.         </div>
  34. <script src="script.js"></script>
  35. </body>
  36. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will convert your hex code into rgb code. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function hexToRgb(h){
  2.         var r = parseInt((cutHex(h)).substring(0,2),16);
  3.         var g = parseInt((cutHex(h)).substring(2,4), 16);
  4.         var b = parseInt((cutHex(h)).substring(4,6),16);
  5.         return document.getElementById('r').value = r, document.getElementById('g').value = g, document.getElementById('b').value = b;
  6. }
  7. function cutHex(h){
  8.         return (h.charAt(0)=="#") ? h.substring(1,7):h
  9. }
  10.  
  11. function convert(){
  12.         var hex=document.getElementById('hex');
  13.        
  14.         if(hex.value.length == 6){
  15.                 hexToRgb("#"+hex.value);
  16.         }else{
  17.                 alert("Must enter 6 length number!");
  18.         }
  19.        
  20. }

In the code above we created 3 methods that will handle and convert the entered hex code. The first method we create is called cutHex(), this function will break down the string format by pair. Next is hexToRgb() this function will now convert your given hex code into RGB color code. Last is is what we called convert(), this function will display the already converted color code into your html content.

Output:

The How to Convert Hex to RGB Color Code in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Convert Hex to RGB Color Code in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment