1 out of 4 rated this helpful - Rate this topic

ChooseColorDlg method

[This documentation is preliminary and is subject to change.]

Opens the system color-selection dialog box.

Syntax

var rgbColor = HtmlDlgSafeHelper.ChooseColorDlg(initColor);

Standards information

There are no standards that apply here.

Parameters

initColor [in, optional]

Type: VARIANT

A Variant that specifies the RGB value of the initial color selected in the color-selection dialog box. The value is specified as 0xrrggbb where rr is the red hex value, gg is the green hex value, and bb is the blue hex value. For a complete list of colors, see Color Table.

rgbColor [out, retval]

Type: VARIANT

A Variant that specifies the decimal value of the color chosen in the color-selection dialog box. The value must be converted to its hexadecimal equivalent and is specified as 0xrrggbb.

Return value

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Type: VariantA Variant that specifies the decimal value of the color chosen in the color-selection dialog box. The value must be converted to its hexadecimal equivalent and is specified as 0xrrggbb.

Remarks

There is no way to persist custom colors in this dialog box.

The return value of this method must be converted from decimal to hexadecimal before it can be used to change the collor of an object. The hexadecimal number must contain six digits to represent the RRGGBB schema required by the color table. For numbers with less than six numbers, concatenate zeros to the beginning of the number. For more information, see the Color Table.

ChooseColorDlg was introduced in Microsoft Internet Explorer 6.

Examples

The following example shows how to call the color dialog box. The return value of the method is converted to a hexadecimal value and applied to the font color.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/editing/DialogHelper/colordialogshowme.htm


//This variable must have global scope for the callColorDlg function to persist the chosen color
var sInitColor = null;
function callColorDlg(){
//if sInitColor is null, the color dialog box has not yet been called
if (sInitColor == null) 
	var sColor = dlgHelper.ChooseColorDlg();
else
//call the dialog box and initialize the color to the color previously chosen
	var sColor = dlgHelper.ChooseColorDlg(sInitColor);
//change the return value from a decimal value to a hex value and make sure the value has 6
//digits to represent the RRGGBB schema required by the color table
	sColor = sColor.toString(16);
if (sColor.length < 6) {
  var sTempString = "000000".substring(0,6-sColor.length);
  sColor = sTempString.concat(sColor);
}
	document.execCommand("ForeColor", false, sColor);
	
//set the initialization color to the color chosen
	sInitColor = sColor;
	
}

 

 

Build date: 3/8/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
By default this code wont work on IE 8.0 because of security issues
I am using this color picker on my forms. When I tried it with IE 8.0 it did not work. Looking in to it it worked as I have added the site to "trusted sites" list. This is obviously may not be the desired solution as it might rise security issues, but this is the fastest solution that I can think of.
initColor

The sample code is correct, and the description at the top of this page is incorrect.

initColor must be specified as a six character (rrggbb) String. So, for green you must specify "00ff00", and not "0x00ff00", nor 0x00ff00 (integer value).

Cancel button
If the user chooses Cancel, the inital colour passed to the dialog is returned. If no initial colour is passed to the dialog and the user chooses cancel, 0 is returned. There is no way of knowing if the user chose black, or cancel. In most cases this is not a problem, but if no initial colour was passed to the dialog, because the current colour is transparent (for a background), then if the user selects cancel it is not possible to determine if the action should be "Don't change the colour from transparent", or "change the colour to black". Care needs to be taken in this circumstance.
<OBJECT> element required.

For this code to work, you need to add:

<OBJECT id=dlgHelper CLASSID="clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b" width="0px" height="0px"></OBJECT>

to the body of your html.