JavaScript - Convert RGB to Hex

Submitted by: 
Language: 
Visitors have accessed this post 186 times.

In this tutorial we will create a Convert RGB to Hex using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. This code can be used as your calculator to any mathematical problem. So Let's do the coding...

Getting started:

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

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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">JavaScript - Convert RGB to Hex</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-9">
  17. <div class="form-inline">
  18. <h4>RGBA</h4>
  19. <label>R:</label>
  20. <input type="number" id="r" class="form-control" style="width:20%;" min="0" max="255"/>
  21. <label>G:</label>
  22. <input type="number" id="g" class="form-control" style="width:20%;" min="0" max="255"/>
  23. <label>B:</label>
  24. <input type="number" id="b" class="form-control" style="width:20%;" min="0" max="255"/>
  25. <button type="button" class="btn btn-primary" onclick="convert()"><span class="glyphicon glyphicon-arrow-right"></span> Convert</button>
  26. </div>
  27. </div>
  28. <div class="col-md-3">
  29. <h4>HEX</h4>
  30. <input type="text" id="hex" class="form-control" readonly="readonly"/>
  31. </div>
  32. </div>
  33. <script src="js/script.js"></script>
  34. </body>
  35. </html>

Creating the Script

This code contains the script of the application. This code will convert the rbg into hex for better use in the layout. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.

  1. function convert(){
  2. var r = document.getElementById("r").value;
  3. var g = document.getElementById("g").value;
  4. var b = document.getElementById("b").value;
  5. var hex = document.getElementById("hex");
  6.  
  7. if(r == "" || g == "" || b == ""){
  8. alert("Please enter something!");
  9. }else{
  10. if(r > 255 || g > 255 || b > 255){
  11. alert("The given value is too large enough to convert!");
  12. }else{
  13. var rgb= "rgb("+r+", "+g+", "+b+")";
  14. hex.value = rgb2hex(rgb);
  15. }
  16. }
  17. }
  18.  
  19.  
  20.  
  21. function rgb2hex(rgb){
  22. rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
  23. return (rgb && rgb.length === 4) ? "#" +
  24. ("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
  25. ("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
  26. ("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
  27. }

There you have it we successfully created a Convert RGB to Hex using 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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.