Simple Color Picker Using jQuery

In this tutorial we will try to create a simple color picker using jQuery. jQuery is a fast, reliable kind of cross-platform javascript library. It is designed to simplify the traditional way of coding in javascript. This application can also help you decide on what color will you use on designing your on website. So let's now do the coding. Creating the Mark Up To display the color picker will need to create the mark up language, to do that open any kind of text editor(notepad++, etc). Then copy/paste the provided code below and name it 'index.html'
  1. <!DOCTYPE html>
  2. <html lang = "en">
  3. <head>
  4. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css"/>
  5. <meta charset = "UTF-8" name = "viewport" content = "width=device-width, initial-scale=1" />
  6. </head>
  7. <nav class = "navbar navbar-default">
  8. <div class = "container-fluid">
  9. <div class = "navbar-header">
  10. <a href = "https://sourcecodester.com" class = "navbar-brand">Sourcecodester</a>
  11. </div>
  12. </div>
  13. </nav>
  14. <div class = "row">
  15. <div class = "col-md-3"></div>
  16. <div class = "col-md-6 well">
  17. <h3 class = "text-primary">Simple Color Picker Using Jquery</h3>
  18. <hr style = "border-top:1px dotted #000;"/>
  19. <div class = "col-md-6" style = "border:1px solid #000; height:300px; transition: background 100ms;" id = "result">
  20. </div>
  21. <div class = "col-md-5 pull-right">
  22. R<input type = "range" min = "0" value = "0" max = "255" step = "1" id = "red"/>
  23. <br />
  24. <input type = "number" value = "0" min = "0" max = "255" size = "4" id = "txt_red" />
  25. <br /><br />
  26. G<input type = "range" min = "0" value = "0" max = "255" step = "1" id = "green"/>
  27. <br />
  28. <input type = "number" value = "0" min = "0" max = "255" size = "4" id = "txt_green" />
  29. <br /><br />
  30. B<input type = "range" min = "0" value = "0" max = "255" step = "1" id = "blue"/>
  31. <br />
  32. <input type = "number" value = "0" min = "0" max = "255" size = "4" id = "txt_blue" />
  33. <br /><br />
  34. Hex
  35. <br />
  36. <input type = "text" id = "hex"/>
  37. </div>
  38. </div>
  39. </div>
  40. </body>
  41. <script src = "js/jquery-3.1.1.js" ></script>
  42. <script src = "js/script.js" ></script>
  43. </html>
Adding the jQuery Script To add the features of jQuery to HTML form, copy/paste the provided code below and name it 'script.js'
  1. $(document).ready(function(){
  2.  
  3. /* Range RGB */
  4. $input_range = $('input[type="range"]');
  5. $input_range.change(function(){
  6. $red = $('#red').val();
  7. $('#txt_red').val($red);
  8. $green = $('#green').val();
  9. $('#txt_green').val($green);
  10. $blue = $('#blue').val();
  11. $('#txt_blue').val($blue);
  12. $('#result').css('background', 'rgb(' + $red +', ' + $green + ', ' + $blue + ')');
  13. $rgb = 'rgb(' + $red + ', ' + $green + ', ' + $blue + ')';
  14. $hex = rgb2hex();
  15. $('#hex').val($hex);
  16. });
  17. /* End*/
  18.  
  19. /* Textbox RGB*/
  20. $input_text = $('input[type="number"]');
  21. $input_text.change(function(){
  22. $txt_red = $('#txt_red').val();
  23. $('#red').val($txt_red);
  24. $txt_green = $('#txt_green').val();
  25. $('#green').val($txt_green);
  26. $txt_blue = $('#txt_blue').val();
  27. $('#blue').val($txt_blue);
  28. $('#result').css('background', 'rgb(' + $txt_red +', ' + $txt_green + ', ' + $txt_blue + ')');
  29. $rgb = 'rgb(' + $txt_red + ', ' + $txt_green + ', ' + $txt_blue + ')';
  30. $hex = rgb2hex();
  31. $('#hex').val($hex);
  32. });
  33. /* End*/
  34.  
  35. /* Input Hex*/
  36. $('#hex').change(function(){
  37. $txt_hex = $(this).val();
  38. hexToRgb($txt_hex);
  39. });
  40. /* End */
  41.  
  42. /* rgb2hex*/
  43. function rgb2hex(rgb) {
  44. var hexDigits = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
  45. rgb = $rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
  46. function hex(x) {
  47. return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
  48. }
  49. return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
  50. }
  51. /* End */
  52.  
  53. /* hex2rgb*/
  54. function hexToRgb(h)
  55. {
  56. var r = parseInt((cutHex(h)).substring(0,2),16), g = parseInt((cutHex(h)).substring(2,4), 16), b = parseInt((cutHex(h)).substring(4,6),16)
  57. return document.getElementById('txt_red').value = r, document.getElementById('txt_green').value = g, document.getElementById('txt_blue').value = b, document.getElementById('red').value = r, document.getElementById('green').value = g, document.getElementById('blue').value = b, document.getElementById('result').style.background = 'rgb(' + r + ', ' + g + ', ' + b + ')';
  58. }
  59. function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
  60. /* End */
  61. });
There you have it we created a simple color picker app using jquery. I hope that this tutorial help you build your very own application. For more updates and tutorial just kindly visit this site. Enjoy Coding!!

Tags

Add new comment