HTML5 Color Picker

Introduction

 
HTML5 is changing more than just websites, it's changing the capabilities of browsers, as well. In this article, we are going to understand working with the color changer in HTML5. We know that HTML is Hyper Text Markup Language and is used to display data in a browser. CSS is the acronym for Cascading Style Sheet that is used for design. CSS defines how HTML elements are to be displayed. Styles are normally saved in external .css files. Normally HTML is saved with a .html file extension.
 
Here we will make use of a few JavaScript and some styles along with the HTML code.
 
Just go through the steps to know how to create this application.
 
Step 1 : Open a HTML editor or Visual studio.
 
Open File menu ->select new ->Choose Website then.
 
0000.jpg
 
This is where we will create an HTML5 application.
  • Go to Solution Explorer
  • Right Click on the Application name
  • Select Add-->add new item
  • Now in this window that opens up select an HTML page or new Web form.
st1.gif
 
Step 2: In this section, we will create the style for the media and create the .css on the media screen. Put the given script in the Head section of the HTML or in between the <head>--</head>
 
Script for the style tag is as follows:
  1. < style media = "screen" > body {  
  2.   background: #555;color:# 555;font: 300 100.01 % /1.2"Segoe UI","Helvetica Neue",Helvetica,"Arial Unicode",Arial,sans-serif;margin:0 30px;}#content{width:762px;height:507px;position:relative;}  
  3.   h1 {  
  4.    font - weight: 300;  
  5.    font - size: 3 em;  
  6.    position: absolute;  
  7.    right: 10 px;  
  8.    bottom: 50 px;  
  9.   }  
  10.   p {  
  11.    font - size: 2 em;  
  12.   }#  
  13.   benefits {  
  14.    margin - top: 350 px;  
  15.   }#  
  16.   copy a {  
  17.    color: #666;text-decoration:none;}# picker2 {  
  18.     width: 300 px;position: absolute;top: 250 px;left: 50 % ;margin - left: -150 px;  
  19.    }#  
  20.    benefits {  
  21.     margin - bottom: 0;  
  22.    }#  
  23.    output {  
  24.     background: #eee;position: absolute;font - size: 30 px;bottom: 10 px;left: 20 px;font - family: monospace;margin - top: -20 px;width: 197 px;  
  25.    }#  
  26.    copy {  
  27.     position: absolute;right: 10 px;bottom: 10 px;margin: 0;font - size: .6 em;color: #666;}# values {  
  28.      position: absolute;left: 51 px;top: 366 px;font - size: .7 em;width: 132 px;height: 66 px;  
  29.     } < /style>  
Step 3: In this section, we need to work on some JavaScript over here. For understanding the full working of JavaScript download the attached .rar file and run the color picker application.
  1. < script >  
  2.  colors(function() {  
  3.   var out1 = document.getElementById("output"),  
  4.    dd = document.getElementById("dd"),  
  5.    dg = document.getElementById("dg"),  
  6.    db = document.getElementById("db"),  
  7.    dh = document.getElementById("dh"),  
  8.    dl = document.getElementById("dl"),  
  9.    ds = document.getElementById("ds"),  
  10.    dd.innerHTML = clr.r;  
  11.   dg.innerHTML = clr.g;  
  12.   db.innerHTML = clr.b;  
  13.   dh.innerHTML = dl.innerHTML = Math.round(clr.h * 360) + "°";  
  14.   out.onkeyup = function() {  
  15.    al.color(this.value);  
  16.    ml.color(this.value);  
  17.   };   
  18.  }); <  
  19. /script>  
Step 4 : In this section, we are going to become familiar with the body part of HTML scripting. In this part, we will create the table for the purpose of getting the combination of colors while we are changing the pointer.
  1. <body>  
  2.     <form id="form1" runat="server">  
  3.         <div id="content" style="background-color: #FFFFFF; top: 20px; left: 12px;">  
  4.             <h1 id="h1" style="font-family: Verdana; width: 413px; height: 57px; color: #008080; background-color:  
  5. #FFFF00;">   Color Changer</h1>  
  6.             <table id="values">  
  7.                 <tr>  
  8.                     <th>R</th>  
  9.                     <td id="vr"></td>  
  10.                     <th>H</th>  
  11.                     <td id="vh"></td>  
  12.                     <th>H</th>  
  13.                     <td id="vh2"></td>  
  14.                 </tr>  
  15.                 <tr>  
  16.                     <th>G</th>  
  17.                     <td id="vg"></td>  
  18.                     <th>S</th>  
  19.                     <td id="vs"></td>  
  20.                     <th>S</th>  
  21.                     <td id="vs2"></td>  
  22.                 </tr>  
  23.             </table>  
  24.             <input type="text" id="output" value="#eeeeee"   
  25.                 style="background-color: #FFFFFF; color: #FF0000;">  
  26.                 <asp:Label ID="Label1" runat="server"   
  27.                 style="top: 473px; left: 591px; position: absolute; height: 19px; width: 73px; background-color:  
  28. #FFFFFF"   
  29.                 Text="DWIJ"></asp:Label>  
  30.             </div>  
  31.             <div id="picker2"></div>  
  32.         </form>  
  33.     </body>  
Step 5: Output Press F5.
 
Note:
For the accurate output of HTML5 applications, you must have the Google Chrome browser on your PC.
 
Here are your outputs:
 
color1.gif
 
After changing the pointer
 
color2.gif
 
Again changing the pointer.
 
color3.gif
 
Resource
 
Some of the useful resources are :
 
Basics of Colors in XAML Silverlight
Photo Browser and Wallpaper Changer
Color Picker in XAML Silverlight
Colors, Fonts, and Text in GDI+