Click here to Skip to main content
15,881,898 members
Articles / Web Development / HTML
Article

Windows Like Choose Color Dialog

Rate me:
Please Sign up or sign in to vote.
4.45/5 (17 votes)
1 Dec 20052 min read 59.6K   1.5K   32   15
How to implement a Windows like Choose Color dialog with HTML and JavaScript.

Image 1

Introduction

I was searching the internet for some JavaScript source for a Windows like Choose Color dialog (thanks to Google for making my work as a programmer easier) but I couldn't find one. May be I have to improve my search skills :). And because I needed it, I had to write it myself :(. This is the solution that I came up with, may be it is not the best, but it works. I have tested it with IE, Mozilla and Netscape, and it seems to work fine! So enjoy it, but be careful, I am not the best programmer in the universe :). So let me know if there are any errors!

Background

I used the Windows idea for a color dialog because people are used to that kind of a dialog. So the first thing I did was to find the Hue Sat Lum to RGB conversion method and luckily Microsoft has had published their implementation of it in their website, which at the beginning, I was thinking was not working properly because of the floating point rounding operations, but it turned out that if all the operations are forced to be in integers, everything seems to work quite pretty.

Using the code

There are two files that do the work:

  • color_conv.js - which contains the functionality.
  • color_dialog.htm - which contains the user interface.
  • pix - the folder which contains the slide images.

I hope the code is easy to use. You have to write your own callback function in your HTML which will invoke the ColorDialog.

JavaScript
function OnChangeColor(color,param){
    if(color){
        if(param== ... ){
        }else if(param== ... ){
        }
    }
}

This function will be called on closing the dialog, and color will contain the value of the color (e.g.: '#ff00ff') that was chosen, and param is the user defined value to identify the call. It can be whatever the user sets when invoking the dialog.

The dialog is called by:

JavaScript
fnShowChooseColorDlg(color,param,path);

where:

  • color is the color that we set for the dialog to be displayed initially.
  • param is the user parameter returned when we choose the color to identify the call.
  • path is the relative path to color_dialog.htm which represents the user interface of the color dialog.

That's why you should also include the JavaScript file which implements that function.

HTML
<script language="javascript" src="color_conv.js"></script>

So enjoy using this code if you understand anything :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIt is not work in Google crom Pin
Member 77497308-Jul-13 2:40
Member 77497308-Jul-13 2:40 
GeneralRe: It is not work in Google chrome Pin
Veselin Tenev6-Aug-13 0:21
Veselin Tenev6-Aug-13 0:21 
QuestionYordan Georgiev Pin
alben4ik7-Feb-10 11:12
alben4ik7-Feb-10 11:12 
GeneralAccept button not working on Firefox Pin
nILaRT13-Oct-08 0:11
nILaRT13-Oct-08 0:11 
GeneralRe: Accept button not working on Firefox Pin
Veselin Tenev13-Oct-08 5:20
Veselin Tenev13-Oct-08 5:20 
GeneralRe: Accept button not working on Firefox Pin
nILaRT13-Oct-08 6:30
nILaRT13-Oct-08 6:30 
GeneralRe: Accept button not working on Firefox Pin
Veselin Tenev13-Oct-08 6:52
Veselin Tenev13-Oct-08 6:52 
GeneralRe: Accept button not working on Firefox Pin
nILaRT13-Oct-08 20:57
nILaRT13-Oct-08 20:57 
Fixed it, was my bad, i had to use a lot of color dialogs and made a function so i didn't had to create a function for every textBox with the literal name of the control:

function OnChangeColor(color,objV){
if(color){
objV.value = color;
objV.style.backgroundColor = color;
}
}

Unafortunately firefox had a problem with that objV as if it was undefined, i decided to pass the literal name directly from the Link and use getElementById:

function OnChangeColor(color,name){
if(color){
document.getElementById(name).value = color;
document.getElementById(name).style.backgroundColor = color;
}
}

And it's working now!

Thanks a lot for the help, nice code tho Big Grin | :-D
GeneraldialogArgument Pin
MostafaHamdy7-Feb-07 1:17
MostafaHamdy7-Feb-07 1:17 
GeneralRe: dialogArgument Pin
Veselin Tenev7-Feb-07 23:05
Veselin Tenev7-Feb-07 23:05 
GeneralWOW YOU are so Great!! Pin
veronyiu30-Nov-06 17:32
veronyiu30-Nov-06 17:32 
GeneralOptimizition session Pin
Yoava33330-Nov-05 5:36
Yoava33330-Nov-05 5:36 
GeneralWow Pin
User 5502629-Nov-05 23:53
User 5502629-Nov-05 23:53 
GeneralSuggestion Pin
shaul_ahuva28-Nov-05 1:49
shaul_ahuva28-Nov-05 1:49 
GeneralUpdate Pin
Veselin Tenev26-Nov-05 9:06
Veselin Tenev26-Nov-05 9:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.