Creating Custom Color Palette Using jQuery, HTML5, CSS

Introduction

 
Today I came across a requirement to integrate a color palette into my application. When I searched for that, unfortunately, I could not find any solution. So I thought of creating a custom color palette tool. This article explains how to do this. I will use this tool in my high chart application. I hope you will like it. Please see the following image if you do not know what a color palette is.
 
 
Please see this article in my blog
 
Background
 
I am working on a dashboard application, where you can find many charts, maps and grids and so on. If you work with a chart, you will definitely know the word series. Previously we were applying the color for every series separately, in other words, the user must select the color for each series or it will take some default color. Your user may think this process is too much work. Here I am providing a custom color palette control.
 
Why
 
Instead of applying colors separately, we can just select a palette so that the colors set in the selected palette will be applied automatically as in Excel.
 
Using the code
 
I am using jquery-1.9.1.js for the implementation in my application, you can get this file from the attached application.
 
Once you include the jQuery file, please create an HTML5 page as shown below.
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5. </head>  
  6. <body>  
  7.   
  8. </body>  
  9. </html>  
Include the jQuery reference as in the following:
  1. <script src="jquery-1.9.1.js"></script>  
Now to replace our body tag as shown below:
  1. <body>  
  2.     <header style="text-align:center"> Custom Color Palette - SibeeshPassion</header>  
  3.      
  4.     <br />  
  5.     <br />  
  6.     <br />  
  7.     <br />  
  8.     <br />  
  9.     <br />  
  10.     Change Series Color:  
  11.     <label id="changecolor" title="Change Color"><u><i>Change Color</i></u></label>  
  12.     <ul class="containerul">  
  13.         <li>  
  14.             <p class="boxcontainer">  
  15.                 Color Palette I :  
  16.                 <br />  
  17.                 <span class="simplebox" title="#F29F98" style="background-color: #F29F98"></span>  
  18.                 <span class="simplebox" title="#F27C72" style="background-color: #F27C72"></span>  
  19.                 <span class="simplebox" title="#F15C4E" style="background-color: #F15C4E"></span>  
  20.                 <span class="simplebox boxspecialone" title="#F03B2A" style="background-color: #F03B2A"></span>  
  21.                 <span class="simplebox" title="#F12815" style="background-color: #F12815"></span>  
  22.                 <span class="simplebox" title="#FF1601" style="background-color: #FF1601"></span>  
  23.                 <input type="checkbox" class="boxcheckbox" />  
  24.             </p>  
  25.         </li>  
  26.         <li>  
  27.             <p class="boxcontainer">  
  28.                 Color Palette II :  
  29.                 <br />  
  30.                 <span class="simplebox" title="#A3F0BE" style="background-color: #A3F0BE"></span>  
  31.                 <span class="simplebox" title="#7FEBA5" style="background-color: #7FEBA5"></span>  
  32.                 <span class="simplebox" title="#48EB81" style="background-color: #48EB81"></span>  
  33.                 <span class="simplebox boxspecialone" title="#25EB6B" style="background-color: #25EB6B"></span>  
  34.                 <span class="simplebox" title="#14E85E" style="background-color: #14E85E"></span>  
  35.                 <span class="simplebox" title="#03E552" style="background-color: #03E552"></span>  
  36.                 <input type="checkbox" class="boxcheckbox" />  
  37.             </p>  
  38.         </li>  
  39.         <li>  
  40.             <p class="boxcontainer">  
  41.                 Color Palette III :  
  42.                 <br />  
  43.                 <span class="simplebox" title="#F48A8A" style="background-color: #F48A8A"></span>  
  44.                 <span class="simplebox" title="#F57777" style="background-color: #F57777"></span>  
  45.                 <span class="simplebox" title="#FA2A2A" style="background-color: #FA2A2A"></span>  
  46.                 <span class="simplebox boxspecialone" title="#FC0909" style="background-color: #FC0909"></span>  
  47.                 <span class="simplebox" title="#EF0505" style="background-color: #EF0505"></span>  
  48.                 <span class="simplebox" title="#FF0000" style="background-color: #FF0000"></span>  
  49.                 <input type="checkbox" class="boxcheckbox" />  
  50.             </p>  
  51.         </li>  
  52.         <li>  
  53.             <p class="boxcontainer">  
  54.                 Color Palette IV :  
  55.                 <br />  
  56.                 <span class="simplebox" title="#C3A1E9" style="background-color: #C3A1E9"></span>  
  57.                 <span class="simplebox" title="#AF7AEC" style="background-color: #AF7AEC"></span>  
  58.                 <span class="simplebox" title="#A25CF2" style="background-color: #A25CF2"></span>  
  59.                 <span class="simplebox boxspecialone" title="#8D32F5" style="background-color: #8D32F5"></span>  
  60.                 <span class="simplebox" title="#811CF5" style="background-color: #811CF5"></span>  
  61.                 <span class="simplebox" title="#790AF9" style="background-color: #790AF9"></span>  
  62.                 <input type="checkbox" class="boxcheckbox" />  
  63.             </p>  
  64.         </li>  
  65.     </ul>  
  66.     <br />  
  67.     <br />  
  68.     <br />  
  69.     <br />  
  70.     <br />  
  71.     <br />  
  72.     <br />  
  73.     <br />  
  74.     <br />  
  75.     <br />  
  76.     <br />  
  77.     <br />  
  78.     <br />  
  79.     <br />  
  80.     <br />  
  81.     <br />  
  82.     <br />  
  83.     <br />  
  84.     Here colors will be changed whenever you select the palette :)   
  85.     <div id="checker" class="outputcontainer">  
  86.     </div>  
  87. </body>  
Here what I am doing is, I am creating a ul element and inside that I am creating some p elements with span. Please see that in the preceding HTML element section.
 
Once you are done with the elements, what would be the next step? Yeah, we need to style them. Please include the following styles.
  1. <style>  
  2.         .simplebox {  
  3.             cursor: pointer;  
  4.             border: 1px solid #f0f8ff;  
  5.             float: left;  
  6.             margin: 1px;  
  7.             width: 20px;  
  8.             height: 20px;  
  9.         }  
  10.   
  11.         .simpleboxdynamic {  
  12.             cursor: pointer;  
  13.             border: 1px solid #f0f8ff;  
  14.             float: left;  
  15.             margin: 1px;  
  16.             width: 99.3%;  
  17.             height: 27px;  
  18.             padding: 5px;  
  19.             border-radius: 3px;  
  20.             text-align: center;  
  21.         }  
  22.   
  23.         .boxspecialone {  
  24.             border: 1px solid #ff00ff;  
  25.         }  
  26.   
  27.         .boxcontainer {  
  28.             border: 1px solid #ccc;  
  29.             width: 165px;  
  30.             position: relative;  
  31.             height: 50px;  
  32.             padding: 2px;  
  33.         }  
  34.   
  35.         .boxcheckbox {  
  36.             float: right;  
  37.             margin-top: 6px;  
  38.         }  
  39.   
  40.         .outputcontainer {  
  41.             border: 1px solid #ccc;  
  42.             width: 100%;  
  43.             position: relative;  
  44.             height: 247px;  
  45.             margin-top: 5px;  
  46.         }  
  47.   
  48.         .maincontainer {  
  49.             display: none;  
  50.         }  
  51.   
  52.         .containerul {  
  53.             position: absolute;  
  54.             width: 190px;  
  55.             list-style: none;  
  56.             margin: 4px;  
  57.             /*border: 2px solid #29b8e5;*/  
  58.             -webkit-border-radius: 5px;  
  59.             -moz-border-radius: 5px;  
  60.             border-radius: 5px;  
  61.             background-color: #ffffff;  
  62.             overflow-y: auto;  
  63.             overflow-x: hidden;  
  64.             display: block;  
  65.             margin-left: 0px;  
  66.             z-index: 200;  
  67.             height: 305px;  
  68.             display: none;  
  69.             left: 60px;  
  70.         }  
  71.  
  72.         #changecolor {  
  73.             cursor: pointer;  
  74.         }  
  75. </style>  
Our next step is writing the scripts. Please see that below.
  1. <script>  
  2.         $(document).ready(function () {  
  3.             $("#changecolor").click(function () {  
  4.                 $(".containerul").slideToggle(1000);  
  5.             });  
  6.             $(".boxcheckbox").click(function () {  
  7.                 $("#checker").html('');  
  8.                 //checked true only for the current  
  9.                 $(".boxcheckbox").prop("checked"false);  
  10.                 $(this).prop("checked"true);  
  11.                 //bind colors start  
  12.                 var colorCount = 0;  
  13.                 var spans = $(this).parent().find('span');  
  14.                 var test = '<div></div>';  
  15.                 $.each(spans, function (key, value) {  
  16.                     var color = $(value).attr('title');  
  17.                     var p = '<p style="background-color:' + color + '" class="simpleboxdynamic"> Color Key :' + key + '</p>';  
  18.                     $("#checker").append(p);  
  19.                 });  
  20.             });  
  21.         });  
  22. </script>  
In the script, I am calling the checked event of the check boxes inside the p element and whenever a user selects one, I am finding the parent colors and applying that to the output UI.
 
That is all. Cool, we have done it.
 
Complete HTML
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title>Custom Color Palette - SibeeshPassion</title>  
  5.     <style>  
  6.         .simplebox {  
  7.             cursor: pointer;  
  8.             border: 1px solid #f0f8ff;  
  9.             float: left;  
  10.             margin: 1px;  
  11.             width: 20px;  
  12.             height: 20px;  
  13.         }  
  14.   
  15.         .simpleboxdynamic {  
  16.             cursor: pointer;  
  17.             border: 1px solid #f0f8ff;  
  18.             float: left;  
  19.             margin: 1px;  
  20.             width: 99.3%;  
  21.             height: 27px;  
  22.             padding: 5px;  
  23.             border-radius: 3px;  
  24.             text-align: center;  
  25.         }  
  26.   
  27.         .boxspecialone {  
  28.             border: 1px solid #ff00ff;  
  29.         }  
  30.   
  31.         .boxcontainer {  
  32.             border: 1px solid #ccc;  
  33.             width: 165px;  
  34.             position: relative;  
  35.             height: 50px;  
  36.             padding: 2px;  
  37.         }  
  38.   
  39.         .boxcheckbox {  
  40.             float: right;  
  41.             margin-top: 6px;  
  42.         }  
  43.   
  44.         .outputcontainer {  
  45.             border: 1px solid #ccc;  
  46.             width: 100%;  
  47.             position: relative;  
  48.             height: 247px;  
  49.             margin-top: 5px;  
  50.         }  
  51.   
  52.         .maincontainer {  
  53.             display: none;  
  54.         }  
  55.   
  56.         .containerul {  
  57.             position: absolute;  
  58.             width: 190px;  
  59.             list-style: none;  
  60.             margin: 4px;  
  61.             /*border: 2px solid #29b8e5;*/  
  62.             -webkit-border-radius: 5px;  
  63.             -moz-border-radius: 5px;  
  64.             border-radius: 5px;  
  65.             background-color: #ffffff;  
  66.             overflow-y: auto;  
  67.             overflow-x: hidden;  
  68.             display: block;  
  69.             margin-left: 0px;  
  70.             z-index: 200;  
  71.             height: 305px;  
  72.             display: none;  
  73.             left: 60px;  
  74.         }  
  75.   
  76.         #changecolor {  
  77.             cursor: pointer;  
  78.         }  
  79.     </style>  
  80.     <script src="jquery-1.9.1.js"></script>  
  81.     <script>  
  82.         $(document).ready(function () {  
  83.             $("#changecolor").click(function () {  
  84.                 $(".containerul").slideToggle(1000);  
  85.             });  
  86.             $(".boxcheckbox").click(function () {  
  87.                 $("#checker").html('');  
  88.                 //checked true only for the current  
  89.                 $(".boxcheckbox").prop("checked", false);  
  90.                 $(this).prop("checked", true);  
  91.                 //bind colors start  
  92.                 var colorCount = 0;  
  93.                 var spans = $(this).parent().find('span');  
  94.                 var test = '<div></div>';  
  95.                 $.each(spans, function (key, value) {  
  96.                     var color = $(value).attr('title');  
  97.                     var p = '<p style="background-color:' + color + '" class="simpleboxdynamic"> Color Key :' + key + '</p>';  
  98.                     $("#checker").append(p);  
  99.                 });  
  100.             });  
  101.         });  
  102.     </script>  
  103. </head>  
  104. <body>  
  105.     <header style="text-align:center"> Custom Color Palette - SibeeshPassion</header>  
  106.      
  107.     <br />  
  108.     <br />  
  109.     <br />  
  110.     <br />  
  111.     <br />  
  112.     <br />  
  113.     Change Series Color:  
  114.     <label id="changecolor" title="Change Color"><u><i>Change Color</i></u></label>  
  115.     <ul class="containerul">  
  116.         <li>  
  117.             <p class="boxcontainer">  
  118.                 Color Palette I :  
  119.                 <br />  
  120.                 <span class="simplebox" title="#F29F98" style="background-color: #F29F98"></span>  
  121.                 <span class="simplebox" title="#F27C72" style="background-color: #F27C72"></span>  
  122.                 <span class="simplebox" title="#F15C4E" style="background-color: #F15C4E"></span>  
  123.                 <span class="simplebox boxspecialone" title="#F03B2A" style="background-color: #F03B2A"></span>  
  124.                 <span class="simplebox" title="#F12815" style="background-color: #F12815"></span>  
  125.                 <span class="simplebox" title="#FF1601" style="background-color: #FF1601"></span>  
  126.                 <input type="checkbox" class="boxcheckbox" />  
  127.             </p>  
  128.         </li>  
  129.         <li>  
  130.             <p class="boxcontainer">  
  131.                 Color Palette II :  
  132.                 <br />  
  133.                 <span class="simplebox" title="#A3F0BE" style="background-color: #A3F0BE"></span>  
  134.                 <span class="simplebox" title="#7FEBA5" style="background-color: #7FEBA5"></span>  
  135.                 <span class="simplebox" title="#48EB81" style="background-color: #48EB81"></span>  
  136.                 <span class="simplebox boxspecialone" title="#25EB6B" style="background-color: #25EB6B"></span>  
  137.                 <span class="simplebox" title="#14E85E" style="background-color: #14E85E"></span>  
  138.                 <span class="simplebox" title="#03E552" style="background-color: #03E552"></span>  
  139.                 <input type="checkbox" class="boxcheckbox" />  
  140.             </p>  
  141.         </li>  
  142.         <li>  
  143.             <p class="boxcontainer">  
  144.                 Color Palette III :  
  145.                 <br />  
  146.                 <span class="simplebox" title="#F48A8A" style="background-color: #F48A8A"></span>  
  147.                 <span class="simplebox" title="#F57777" style="background-color: #F57777"></span>  
  148.                 <span class="simplebox" title="#FA2A2A" style="background-color: #FA2A2A"></span>  
  149.                 <span class="simplebox boxspecialone" title="#FC0909" style="background-color: #FC0909"></span>  
  150.                 <span class="simplebox" title="#EF0505" style="background-color: #EF0505"></span>  
  151.                 <span class="simplebox" title="#FF0000" style="background-color: #FF0000"></span>  
  152.                 <input type="checkbox" class="boxcheckbox" />  
  153.             </p>  
  154.         </li>  
  155.         <li>  
  156.             <p class="boxcontainer">  
  157.                 Color Palette IV :  
  158.                 <br />  
  159.                 <span class="simplebox" title="#C3A1E9" style="background-color: #C3A1E9"></span>  
  160.                 <span class="simplebox" title="#AF7AEC" style="background-color: #AF7AEC"></span>  
  161.                 <span class="simplebox" title="#A25CF2" style="background-color: #A25CF2"></span>  
  162.                 <span class="simplebox boxspecialone" title="#8D32F5" style="background-color: #8D32F5"></span>  
  163.                 <span class="simplebox" title="#811CF5" style="background-color: #811CF5"></span>  
  164.                 <span class="simplebox" title="#790AF9" style="background-color: #790AF9"></span>  
  165.                 <input type="checkbox" class="boxcheckbox" />  
  166.             </p>  
  167.         </li>  
  168.     </ul>  
  169.     <br />  
  170.     <br />  
  171.     <br />  
  172.     <br />  
  173.     <br />  
  174.     <br />  
  175.     <br />  
  176.     <br />  
  177.     <br />  
  178.     <br />  
  179.     <br />  
  180.     <br />  
  181.     <br />  
  182.     <br />  
  183.     <br />  
  184.     <br />  
  185.     <br />  
  186.     <br />  
  187.     Here colors will be changed whenever you select the palette :)   
  188.     <div id="checker" class="outputcontainer">  
  189.     </div>  
  190. </body>  
  191. </html>  
Output
 
 

Conclusion

 
I hope you liked this article. Please provide your valuable suggestions.